110,570
社区成员
发帖
与我相关
我的任务
分享
//结构体
[Serializable()]
struct ServerData
{
public string file_repospath;
public string type;
}
/// <summary>
/// 反序列化
/// </summary>
/// <param name="data">数据缓冲区</param>
/// <returns>对象</returns>
public static object Deserialize(byte[] data)
{
BinaryFormatter formatter = new BinaryFormatter();
MemoryStream rems = new MemoryStream(data);
data = null;
return formatter.Deserialize(rems);
}
/// <summary>
/// 序列化
/// </summary>
/// <param name="data">要序列化的对象</param>
/// <returns>返回存放序列化后的数据缓冲区</returns>
public static byte[] Serialize(object data)
{
BinaryFormatter formatter = new BinaryFormatter();
MemoryStream rems = new MemoryStream();
formatter.Serialize(rems, data);
return rems.GetBuffer();
}
static void Main(string[] args)
{
//发送库信息到服务端
ServerData m_data;
m_data.type = "A";
m_data.iscalflag = false;
m_data.completeflag = false;
m_data.file_path = "3";
m_data.pre_file_content = "4";
m_data.file_repospath = "1111111111";
m_data.file_content = "222222222222222";
//客户端调试调试序列换与反序列化,正常
Byte[] nsendBytes = Serialize(m_data);
Object oObjTemp = Deserialize(nsendBytes);
ServerData ceshi = (ServerData)oObjTemp;
System.Console.WriteLine(ceshi.file_repospath);
System.Console.WriteLine(ceshi.file_content);
//
TcpClient mClient = new TcpClient("10.78.20.113", 12096);
//发到服务端
if (mClient != null)
{
System.Console.WriteLine("按任意键发送数据");
System.Console.ReadKey();
NetworkStream ns = mClient.GetStream();
Byte[] sendBytes = Serialize(m_data);
ns.Write(sendBytes, 0, sendBytes.Length);
ns.Flush();
}
//结构体
[Serializable()]
struct ServerData
{
public string file_repospath;
public string type;
}
/// <summary>
/// 反序列化
/// </summary>
/// <param name="data">数据缓冲区</param>
/// <returns>对象</returns>
public static object Deserialize(byte[] data)
{
BinaryFormatter formatter = new BinaryFormatter();
MemoryStream rems = new MemoryStream(data);
data = null;
return formatter.Deserialize(rems);
}
/// <summary>
/// 序列化
/// </summary>
/// <param name="data">要序列化的对象</param>
/// <returns>返回存放序列化后的数据缓冲区</returns>
public static byte[] Serialize(object data)
{
BinaryFormatter formatter = new BinaryFormatter();
MemoryStream rems = new MemoryStream();
formatter.Serialize(rems, data);
return rems.GetBuffer();
}
//接收部分如下:
byte[] bytes = new byte[2048];
int bytesread = 0;
byte[] mSumBytes = null;
//StringBuilder m_CompleteMessage = new StringBuilder();
do
{
bytesread = ns.Read(bytes, 0, bytes.Length);
mSumBytes = AddBytes(mSumBytes, bytes, bytesread);
}
while (ns.DataAvailable);
ns.Flush();
//序列化结构体对象
Object ObjTemp = Deserialize(mSumBytes);//这里错了
//叠加2个byte[]:b1实际字符是满的,b2的实际字符是b2_real_data_size
static byte[] AddBytes(byte[] b1, byte[] b2, int b2_real_data_size)
{
if (b1 == null)
{
byte[] newbyte = new byte[b2_real_data_size];
Array.Copy(b2, 0, newbyte, 0, b2_real_data_size);
return newbyte;
}
else
{
int bytelong = b1.Length + b2_real_data_size;
byte[] addsum = new byte[bytelong];
Array.Copy(b1, 0, addsum, 0, b1.Length);
Array.Copy(b2, 0, addsum, b1.Length, b2_real_data_size);
return addsum;
}
}