如题,我做一个服务端,用于显示客户端发送过来的信息,程序自身不退出,开一线程,用于接收数据。但是发现如果后一次接收的数据量少于前一次的数据量的话,会出现数据重复的情况。如第一次接收了1111111111,第二次接收22222,实际上显示的是2222211111,并非我需要的22222,该如何解决?
我试着用temp来处理,但是没有效果。感觉数据是在缓冲区就已经是2222211111了,不知是不是这样,希望知道的指点一下。 string temp = ""; Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
IPEndPoint iep = new IPEndPoint(IPAddress.Any, 2008);
socket.Bind(iep);
ep = (EndPoint)iep;
byte[] bytes = new byte[1024];
while (true)
{
socket.ReceiveFrom(bytes, ref ep);
receiveData = System.Text.Encoding.Unicode.GetString(bytes);
//receiveData = receiveData.TrimEnd('\u0000');
if (temp != receiveData)
{
temp = receiveData;
SetMessageLabel(temp);
temp = "";
}
}
我试着用temp来处理,但是没有效果。感觉数据是在缓冲区就已经是2222211111了,不知是不是这样,希望知道的指点一下。 string temp = ""; Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
IPEndPoint iep = new IPEndPoint(IPAddress.Any, 2008);
socket.Bind(iep);
ep = (EndPoint)iep;
byte[] bytes = new byte[1024];
while (true)
{
socket.ReceiveFrom(bytes, ref ep);
receiveData = System.Text.Encoding.Unicode.GetString(bytes);
//receiveData = receiveData.TrimEnd('\u0000');
if (temp != receiveData)
{
temp = receiveData;
SetMessageLabel(temp);
temp = "";
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货