这是我当前的设置(使用 UDP):
void OnDataReceived(IAsyncResult result)
{
IPEndPoint ep = new IPEndPoint(IPAddress.Any, 0);
byte[] buffer = socket.EndReceive(result, ref ep);
Packet p = new Packet(Encoding.ASCII.GetString(buffer, 0, buffer.Length));
//process packet
socket.BeginReceive(new AsyncCallback(OnDataReceived), socket);
}
我想知道如果我在调用 EndReceive 之后立即调用 socket.BeginReceive 然后处理数据包以获得这样的连续数据包流会发生什么:
void OnDataReceived(IAsyncResult result)
{
IPEndPoint ep = new IPEndPoint(IPAddress.Any, 0);
byte[] buffer = socket.EndReceive(result, ref ep);
socket.BeginReceive(new AsyncCallback(OnDataReceived), socket);
Packet p = new Packet(Encoding.ASCII.GetString(buffer, 0, buffer.Length));
//process packets
}
如果我一调用 BeginReceive 就收到了一个数据包,这是否会与当前的数据包处理发生冲突?
此外,如果这不会发生冲突,更改为 TCP 会导致此功能失效吗?