byte[] 转换成string 时数据丢失的问题
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
namespace Operate
{
[Serializable]
public class Entity
{
public Entity()
{
}
private string mid;
private string annex;
private string filename;
public string Mid
{
get
{
return mid;
}
set
{
mid = value;
}
}
public string FileName
{
get
{
return filename;
}
set
{
filename = value;
}
}
public string Annex
{
get
{
return annex;
}
set
{
annex = value;
}
}
}
}
上面是一个实体类
foreach (Entity obj in arrayEntity)
{
string strentity = obj.Mid + ";" + obj.FileName + ";" + obj.Annex;//将实体类转换成字符串用socket发送到服务器
if(this.RecvMessage().StartsWith("ok"))
{
this.SendMessage(strentity); //发送到服务器的方法
if (this.RecvMessage().StartsWith("InertAnnexSucessful"))
{
txtState.Text = "服务器成功执行附件: " + obj.FileName + "\r\n" + txtState.Text;
executeAnnex = true;
}
}
}
上面循环发送实体类的字符串
public string recvMessage()
{
byte[] recvBytes = new byte[Int32.Parse(ss)];
int bytes=0;
bytes += s.Receive(recvBytes, recvBytes.Length, 0);
byte[] gb = Encoding.Convert(utf8, gb2312, recvBytes);
string sGb = gb2312.GetString(gb);
}
上面这个是接收消息的方法
public string sendMessage(string mes)
{
byte[] bgb = gb2312.GetBytes(mes);
byte[] butf = Encoding.Convert(gb2312, utf8, bgb);
client.Send(butf);
}
上面这个是发送到服务器的方法
while (true)
{
string ss = this.RecvMessage();
if (ss.StartsWith("Close"))
{
CloseSocketList.Add(s);
break;
}
else
{
this.SendMessage("ok");
byte[] recvBytes = new byte[Int32.Parse(ss)];
int bytes=0;
bytes += s.Receive(recvBytes, recvBytes.Length, 0);//这里可以完全接收到数据
byte[] gb = Encoding.Convert(utf8, gb2312, recvBytes);这个转换就丢失了一些数据了,不知道怎么解决
string sGb = gb2312.GetString(gb);
string[] vv = sGb.Split(';');
int i = 0;
foreach (string ssss in vv)
{
if (i == 0)
{
entity.Mid = ssss;
}
if (i == 1)