我对 udp 套接字有一个奇怪的问题。首先,让我为您提供代码:
服务器端:
UdpClient udpClient = new UdpClient(UdpPort);
udpClient.Connect(pendingClient.IpEndPoint, pendingClient.UdpPort);
foreach (int frameNumber in pendingClient.FramesToSend)
{
byte[] frameBytes = ...
udpClient.Send(frameBytes, frameBytes.Length);
}
udpClient.Close();
客户端:
using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
{
socket.Bind(new IPEndPoint(IPAddress.Any, UdpPort));
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 1000);
Console.WriteLine("Receiving frames");
while (!_sendingCompleted)
{
byte[] receive = new byte[Constants.FrameSize + Frame.HeaderSizeBytes];
try
{
socket.Receive(receive);
}
catch (SocketException exception)
{
if (exception.SocketErrorCode == SocketError.TimedOut)
{
break;
}
throw;
}
...
}
问题是:当我第一次从服务器发送到客户端时,一切正常,但是当我第二次(等等)发送时 - 客户端什么也没收到。该问题的原因可能是什么,我该如何解决?
非常感谢。