I send UDP packets from localhost to localhost and sometimes run on packets. How can I stop this and why is this happening?

This is derived from my program.

sending agent update Created new player Identified sending agent update Physics: 2 ticks this frame time= 200 time= 300 ***Packet Dropped: 2:10 *** ***Packet Dropped: 2:11 *** ***Packet Dropped: 2:12 *** ***Packet Dropped: 2:13 *** ***Packet Dropped: 2:14 *** ***Packet Dropped: 2:15 *** ***Packet Dropped: 2:16 *** ***Packet Dropped: 2:17 *** ***Packet Dropped: 2:18 *** ***Packet Dropped: 2:19 *** ***Packet Dropped: 2:20 *** ***Packet Dropped: 2:21 *** time= 400 Physics: 2 ticks this frame time= 500 Physics: 2 ticks this frame 

Sending packets from the local host to the local host, packets are discarded. This only happens at the beginning. The first 10 or so packets go through, and then the packets after this drop. 5 to 40 packets per line. Then the packets stop.

Is there a reason this should happen?

Update:

The following code fixed the problem.

 int buffsize = 65536; // 65536 setsockopt(socket, SOL_SOCKET, SO_RCVBUF, (void*)&buffsize, sizeof(buffsize)); 

I sent packets too fast and exceeded the default Windows receive buffer, which is only 8 KB. Increasing the buffer size fixed the problem.

+7
source share
3 answers

Check the default UDP buffer size on your OS.

If you find this less, you can explicitly provide more value when creating a UDP socket.

  int buffer_size = 4 * 1024 * 1024;
 setsockopt (socket, SOL_SOCKET, SO_RCVBUF, & buffer_size, sizeof (buffer_size));

You can find this link very useful.

+8
source

You are probably sending packets too fast and thus overflowing buffers. You need to implement a transmission scheme to make sure that the transmitter is not suppressing the receiver. You will never 100% avoid this - it is the nature of UDP that it does not provide a delivery guarantee.

+4
source

Like a really quick guess - in the past I saw this because getting windows is full. Basically, your application does not consume packages fast enough, and the kernel has only so much space reserved - you need to increase the rwin parameter. Or, on the other hand, you send them too fast - options in the Linux box

 net.ipv4.udp_rmem_min = 4096 net.ipv4.udp_wmem_min = 4096 
+1
source

All Articles