Oleg Vazhnev
Oleg Vazhnev

Reputation: 24067

Sockets: sometimes (rarely) packets are lost during receiving

I'm using Socket to receive data from udp multicast. The code is trivial:

s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
....
while (true)
{
    int count = 0;
    try
    {
        count = socket.Receive(byteArray);
    }
    catch (Exception e)
    {
        Console.WriteLine(e.Message);
        return;
    }
    if (count > 0)
    {
        OnNewMessage(new NewMessageEventArgs(byteArray, count));
    }
}

The problem is that sometimes I lose packets. Not too often, ~ once per 2 minutes.

I'm sure that packet is arrived because I can see it in another C++ program launched on the same computer and configured to receive same packets.

Why my program can not catch packets that others can? Why I lose packets? Is it possible that computer is just too slow (or too busy) to receive packets?

I receive about 2 000 packets per second and using Xeon E3 processor, that should be more that enough I think...

Upvotes: 2

Views: 2106

Answers (2)

Marc Gravell
Marc Gravell

Reputation: 1064114

If you are absolutely sure the packet is arriving (and: I must emphasise that this is not guaranteed when using UDP, and 1 packet dropped every two mintes at 2000 packets a second is a better receive rate than you should probably hope for, even for two adjacent machines), then this possibly means that the receive buffer is full at brief moments. Try increasing the ReceiveBufferSize.

Upvotes: 3

pluka
pluka

Reputation: 125

I don't no if is your case, but sometimes your can receive more the one packets in a single byte array of socket.Receive(byteArray). It is due to an optimization of sockets. Check if it's your case, and check your parsing methods.

Upvotes: 0

Related Questions