请教C#网络编程高手,小弟最近做一个C#的网络项目。网络服务端是用C++做的,已经做好。现在要用C#做一个客户端,需要用Socket发送和接收struct类型的数据包到服务器。小弟已在网上找过很多C#网络编程方面的资料,好像都是用Send(byte[],int,int)这样的方法发送。那位大哥大姐有解决办法或者其他解决方案???
解决方案 »
- webbroswer网页数据自动提交
- 关于StreamWriter的使用
- C#代码与用户交互(web)
- 在.NET中如何在远程机上创建文件夹和文件?
- GridView列隐藏
- 做 c# 做个Web服务器,能不能调用IIS里解析asp的组件解析我的内容。
- 请教有关环境配置的问题啊
- 关于Marshal.GetFunctionPointerForDelegate 函数的问题
- 数据类型问题
- C#怎样才能实现开平房、开立方......
- [C#]我真是笨透了,该怎样把C:\\aaa\\bbb\\a.txt的路径名和文件名分开
- 请问:在Winform 下,我的datagrid 是用数据绑定的,我想取某行某列(我这里要用绑定的列的字段名)的数据怎么办呢?
序列化:
1、定义结构
[Serializable()]
public struct structname
{
...
}
2、序列化
public static byte[] ObjectToByteA(object obj)
{
MemoryStream fs = new MemoryStream();
byte[] tmp = null;
try
{
// 序列化
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(fs, obj);
tmp = fs.ToArray();
}
catch(Exception e)
{
MessageBox.Show(e.ToString(), "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
fs.Close();
}
return tmp;
}
3、反序列化
public static object ByteAToObject(byte[] ba)
{
MemoryStream fs = new MemoryStream();
object obj = null;
try
{
// 反序列化
fs = new MemoryStream(ba);
fs.Position = 0;
BinaryFormatter formatter = new BinaryFormatter();
obj = formatter.Deserialize(fs);
}
catch(Exception e)
{
MessageBox.Show(e.ToString(), "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
fs.Close();
}
return obj;
}
4、using引用
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
2.将字节数组发送之。记住:你发送的永远都是byte[]