C# socket 异步接收 问题
各位大神:
C# 写的异步客户端的接收回调
public void readCallback(IAsyncResult ar) {
StateObject state = (StateObject) ar.AsyncState;
Socket handler = state.WorkSocket;
int read = handler.EndReceive(ar);
if (read > 0) {
state.sb.Append(Encoding.ASCII.GetString(state.buffer,0,read));
handler.BeginReceive(state.buffer,0,StateObject.BufferSize, 0,
new AsyncCallback(readCallback), state);
} else {
if (state.sb.Length > 1) {
string content = state.sb.ToString();
Console.WriteLine("Read {0} bytes from socket.\n Data : {1}",
content.Length, content);
}
}
}
问题如下:如果服务端发送过来的数据没有自定义的包头和包尾,长度不定,且缓冲区设置小于接收包长度,也就是需要再次或多次读取,怎么保证读取的数据完整和不重复?
求思路和方法,多谢指点感激不尽。