在Server和Client分别定义序列化的Struct,我建议将序列化的Struct改为序列化的Class,这样就肯定能在Server和Client间传输了
解决方案 »
- 点击窗体上的任何地方,包括窗体上的控件,都执行同一个事件处理程序,除了在每个控件的事件都添加相同的事件处理程序,还可以怎么做
- 请教诸位个关于大量变量的写法.
- winform程序问题求助!!!
- 在这提个UI的问题
- WCF,原先运行的很正常的.突然出现[此集合已经包含方案 http 的地址。此集合中每个方案中最多只能包含一个地址。]
- Process.Start的参数问题
- .net最新版本是什么版,vs.net最新版本是什么版本
- 密码问题 谁知道错在那? 感谢
- C#能创建进程外COM组件吗
- 怎样调用脚本写的方法
- 求教高人,在异步socket通信中,当接收数据没有时为什么BeginReceive不能自循环?
- C#程序的内存占用只能让我敬而远之了 :(
using System.Net;
using System.IO;
using System.Net.Sockets;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text ;namespace Server
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
class dropmulticast
{
[Serializable]
public struct Astruct
{
public int seq;//结构的字段最好用public修饰
public char[] name;
public ulong len;
public char[] data;
public byte[] byt ;
}
public static void Main()
{ Socket socket = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);//用tcp协议
IPEndPoint local = new IPEndPoint(Dns.Resolve(Dns.GetHostName()).AddressList[0],8070);//监听8080端口
socket.Bind(local);
socket.Listen(3);//允许3个客户连接
while (true)
{
Socket accept = socket.Accept();//接受连接的客户
BinaryFormatter bf;
bf = new BinaryFormatter();
MemoryStream stream = new MemoryStream();
Astruct ast = new Astruct();
ast.seq = 4;
ast.name = new char[]{'n', 'a', 'm', 'e'};
ast.len = 4;
ast.data = new char[]{'d', 'a', 't', 'a'};
bf.Serialize(stream, ast);
byte[] buff = stream.ToArray();
accept.Send(buff, buff.Length, 0);
accept.Close();
}
//Console.ReadLine() ;
}
}以上是我的Server端代码
int size = Marshal.SizeOf(pAskConnect);
// 使用 GlobalAlloc 配置記憶體區塊
System.IntPtr ptr = Marshal.AllocHGlobal(size);
// 從 Managed 物件封送處理資料到 Unmanaged 記憶體區塊
Marshal.StructureToPtr(pAskConnect, ptr,true);
byte[] ba=new byte[size];
// 從 Managed 陣列將資料複製到 Unmanaged 記憶體指標
Marshal.Copy(ptr, ba, 0, ba.Length );
你为什么把//[Serializable]去掉了,不声明为可序列化的东西,怎么事先serialize呢?加上就好了,不过可能要改为class的。
至少我这里能正确执行。