I’ve got a weird problem with udp sockets. First of all, let me provide you with the code:
Server side:
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();
Client side:
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;
}
...
}
The problem is: when I do sending from server to client first time everything works perfectly, but when I do it second ( and so on) time – client receives nothing. What can be a reason of that problem and how could I solve it?
Many thanks.
Looks like you are not receiving data in the cycle, try the following approach: