简体   繁体   中英

I am sending UDP packets from localhost to localhost and runs on packets sometimes drop. How do I stop this and why does it happen?

This is output 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 local host to local host, packets are dropping. This only happens near the beginning. The first 10 or so packets get through, then packets after that drop. 5 to 40 packets in a row. Then packets stop dropping.

Is there any reason that 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 was sending packets too fast and exceeded the windows default receive buffer, which is only 8 KB. Increasing the buffer size fixed the problem.

Check the UDP buffer size configured by default in your OS.

If you find it less, you can explicitly provide a greater value while creating a UDP socket.

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

You may find THIS link pretty useful.

You're probably sending packets too quickly and thus overflowing buffers. You need to implement transmit pacing to make sure the transmitter does not overwhelm the receiver. You will never 100% avoid this -- it's the nature of UDP that it does not provide a delivery guarantee.

As a really quick guess - in the past I've seen this because receive windows are full. Basically your application is not consuming the packets fast enough and the kernel only has so much space reserved - you need to increase the rwin parameter. Or on the other side you are sending them too fast - the params on a Linux box are

net.ipv4.udp_rmem_min = 4096
net.ipv4.udp_wmem_min = 4096

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM