大家快救救我啊,要不我就崩溃了!!!!!!!这问题困扰了我两天了!!!!!

jiaoling2 2008-07-15 04:01:03
如何发送一个数据结构????
急啊,大家救救我这个小女子啊!!
万分感谢!


namespace PDAPos
{
class UDP
{
public enum TRequestType { RT_Login, RT_Logout, RT_Ping, RT_Info, RT_Block, RT_LockBill, RT_FreeBill, RT_RefreshTable, RT_RefreshBill, RT_RefreshPrter };


public struct TUDPinfo
{

public byte[] bytes;
public string Information;

public int size;
public int BlockPostion;
public int BlockCount;
public int fileAge;
}
public struct TRequest
{
public TRequestType requestType;
public TUDPinfo requestInfo;
}

public TRequest udprequest;
TUDPinfo udprefvInfo;

System.Net.IPAddress[] addresslist = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName().ToString()).AddressList;

public void sendLockBill()
{
udprequest.requestType = TRequestType.RT_LockBill;
//udprequest.requestInfo.Information.Add(Model.UserProfiles.BillID);
udprequest.requestInfo.Information = Model.UserProfiles.BillID;
try
{
string ip = addresslist[1].ToString();
Socket sk = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
IPAddress serverIP = IPAddress.Parse("202.96.128.191");
int udprequestLength = udprequest.ToString().Length;
string login = string.Format("{0},{1}",5,Model.UserProfiles.BillID);
byte[] sendful=new byte[276];
udprequest.requestInfo.bytes = new byte[276];
udprequest.requestInfo.bytes = Encoding.GetEncoding("gb2312").GetBytes(login);
// sendful = Encoding.GetEncoding("gb2312").GetBytes(login);
udprequest.requestInfo.bytes.CopyTo(sendful, 0);
udprequest.requestInfo.bytes = sendful;
IPEndPoint ep = new IPEndPoint(serverIP,6668);
IPEndPoint ep2 = new IPEndPoint(IPAddress.Parse(ip),6661);
sk.Bind(ep2);
sk.SendTo(udprequest.requestInfo.bytes, ep); //我想发送一个数据结构udprequest


sk.Close();
}
catch
{
// MessageBox.Show("系统监控程序无应答!");
}
}
...全文
271 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
dreamsnake 2008-07-17
  • 打赏
  • 举报
回复
如果是本地程序间发送,用系统API也可以的.网上就有例子,消息号必须是COPYDATA常数,然后两边定义相同的结构,然后传送地址指针就可以了,接收方再通过内存复制的API函数(好象是MemCopy吧,需要查查文档)读取出来.当然这是指同一台机器上不同的程序(尤其是和Win32程序通讯时)间通讯的方法.

如果是网络,我觉得就更简单了.最笨的方法,就是一个一个的发送,另一边一个一个的读出来就是了,最多中间多发几个确认标识之类的东西呗. 当然,我是指你没有时间去学明白序列化或消息队列这种东西的情况下.
liyang19860104 2008-07-16
  • 打赏
  • 举报
回复
太牛了,看不懂
super_vb 2008-07-15
  • 打赏
  • 举报
回复
1. 序列化
2. 用Remoting
ppyyhh 2008-07-15
  • 打赏
  • 举报
回复
微软之所以弄出个序列化和反序列化,就是为了适应你这样类似情况的网络传输用的.
先按9楼的意思将结构体序列化,再写入流传输;在接收端反序列化,用同样的结构体来接收即可.
bindsang 2008-07-15
  • 打赏
  • 举报
回复
给个通用型的,接收方不限于.NET程序

public struct TUDPinfo
{

public byte[] bytes;
public string Information;

public int size;
public int BlockPostion;
public int BlockCount;
public int fileAge;

public static TUDPinfo FromArray(byte[] data) // 从数组构造
{
TUDPinfo p = new TUDPinfo();
using (MemoryStream ms = new MemoryStream(data))
{
BinaryReader br = new BinaryReader(ms);
int bytesLen = br.ReadInt32();
if (bytesLen > 0) p.bytes = br.ReadBytes(bytesLen);
int strLen = br.ReadInt32();
if (strLen > 0) p.Information = Encoding.Default.GetString(br.ReadBytes(strLen));
p.size = br.ReadInt32();
p.BlockPostion = br.ReadInt32();
p.BlockCount = br.ReadInt32();
p.fileAge = br.ReadInt32();

return p;
}
}
public byte[] ToArray() // 转成数组
{
using (MemoryStream ms = new MemoryStream())
{
BinaryWriter bw = new BinaryWriter(ms);
if (bytes == null || bytes.Length == 0)
bw.Write(0);
else
{
bw.Write(bytes.Length);
bw.Write(bytes);
}

if (string.IsNullOrEmpty(Information))
{
byte[] strBytes = Encoding.Default.GetBytes(Information);
bw.Write(strBytes.Length);
bw.Write(strBytes);
}

bw.Write(size);
bw.Write(BlockPostion);
bw.Write(BlockCount);
bw.Write(fileAge);
bw.Flush();
return ms.ToArray();
}
}
}
gomoku 2008-07-15
  • 打赏
  • 举报
回复
我必须发个包头过去啊.如果转化成byte数组也可以吗?



// 其他代码在9楼
public byte[] GetSerializedBytes(byte[] header)
{
BinaryFormatter bf = new BinaryFormatter();
using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
{
// 如果需要,这里可以加入包头
ms.Write( header, 0, header.Length);

bf.Serialize(ms, this); //<---系列化成byte数组
ms.Seek(0, System.IO.SeekOrigin.Begin);
return ms.ToArray();
}
}
public static Astruct FromSerilizedBytes(byte[] data, byte[] header)
{
BinaryFormatter bf = new BinaryFormatter();
using (System.IO.MemoryStream ms = new System.IO.MemoryStream(data))
{
// 如果需要,这里可以拆出包头
ms.Read(header, 0, header.Length);

return (Astruct)bf.Deserialize( ms ); //<---从byte数组再转回来
}
}


jiaoling2 2008-07-15
  • 打赏
  • 举报
回复
我必须发个包头过去啊.如果转化成byte数组也可以吗?
kqh0319 2008-07-15
  • 打赏
  • 举报
回复
序列化和编码都不可以
楼主要是不知道,就百度一下。
syc958 2008-07-15
  • 打赏
  • 举报
回复
是要序列化的!
jiaoling2 2008-07-15
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 zhnzzy 的回复:]

先对发送的消息进行编码

C# code

public static byte[] Encode(string codeString)
{
UnicodeEncoding encode = new UnicodeEncoding ( ) ;
return encode.GetBytes(codeString);
}

byte[] sendData =Encode(udprequest.requestInfo);


我进行编码了呀,但是必须是数组的才可以发过去,我郁闷!



sk.SendTo(sendData , ep);
[/Quote]
gomoku 2008-07-15
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 jiaoling2 的回复:]
引用 2 楼 agentianle 的回复:
序列化为字节流,然后发送,接收方反序列化,将其还原

这位高手大哥,能不能具体点啊,给代码参考参考,因为太急了,谢谢!
[/Quote]

这是我刚在另外一个帖子发的例子,你可以参考。
[URL=http://topic.csdn.net/u/20080715/15/4db1f4ab-5930-4ab4-ae05-d46d758a1100.html]请教~如何在局域网中广播结构体~~~[/URL]


uing System.Runtime.Serialization.Formatters.Binary;
//...
[Serializable]
public struct Astruct
{
public int seq;
public char[] name;
public ulong len;
public char[] data;

public byte[] GetSerializedBytes()
{
BinaryFormatter bf = new BinaryFormatter(); //<---
using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
{
bf.Serialize(ms, this); //<---系列化成byte数组
ms.Seek(0, System.IO.SeekOrigin.Begin);
return ms.ToArray();
}
}
public static Astruct FromSerilizedBytes(byte[] data)
{
BinaryFormatter bf = new BinaryFormatter();
using (System.IO.MemoryStream ms = new System.IO.MemoryStream(data))
{
return (Astruct)bf.Deserialize( ms ); //<---从byte数组再转回来
}
}
}
「已注销」 2008-07-15
  • 打赏
  • 举报
回复
ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.VisualStudio.v80.chs/dv_fxserialization/html/832ac524-21bc-419a-a27b-ca8bfc45840f.htm
zhnzzy 2008-07-15
  • 打赏
  • 举报
回复

先对发送的消息进行编码


public static byte[] Encode(string codeString)
{
UnicodeEncoding encode = new UnicodeEncoding ( ) ;
return encode.GetBytes(codeString);
}

byte[] sendData =Encode(udprequest.requestInfo);



sk.SendTo(sendData , ep);
一品梅 2008-07-15
  • 打赏
  • 举报
回复
小女子那么娇柔啊,发问都那么嗲声嗲气的,浑身象酥了一般,像洗了桑拿。通感这一文学表现方式完美地在小女子的发问中实现了.
一品梅 2008-07-15
  • 打赏
  • 举报
回复
[searilizable]
在类前加上这个
yagebu1983 2008-07-15
  • 打赏
  • 举报
回复
代码写的够乱的!!!
序列是一种方法!!!
你把要发送的数据序列化!!!
收到后,再反序列化!!!
jiaoling2 2008-07-15
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 agentianle 的回复:]
序列化为字节流,然后发送,接收方反序列化,将其还原
[/Quote]

这位高手大哥,能不能具体点啊,给代码参考参考,因为太急了,谢谢!
天乐 2008-07-15
  • 打赏
  • 举报
回复
序列化为字节流,然后发送,接收方反序列化,将其还原
kkai189 2008-07-15
  • 打赏
  • 举报
回复
没接触过,帮顶吧,期待高手救你

110,825

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧