Deny incoming UDP packet without reading

In some cases, I want to explicitly discard packets waiting on the socket, with minimal overhead. There doesn't seem to be an explicit drop udp buffer system call, but maybe I'm wrong?

The next best way would probably be to pass the recvpacket to a temporary buffer and just drop it. It seems I can’t get 0 bytes, as the person is talking about recv: The return value will be 0 when the peer has performed an orderly shutdown.So, 1 is the minimum in this case.

Is there any other way to handle this?

Just in case, this is not a premature optimization. The only thing this server does is forwarding / sending UDP packets in a certain way - although recvwith len=1not killing me, I would rather just drop the entire queue at a time with some more specific function (hopefully reducing latency).

+5
source share
3 answers

You can disconnect the kernel from UDP packets by setting the UDP receive buffer to 0.

int UdpBufSize = 0;
socklen_t optlen = sizeof(UdpBufSize);
setsockopt(socket, SOL_SOCKET, SO_RCVBUF, &UdpBufSize, optlen);

Whenever you consider it necessary to receive packets, you can set a buffer, for example, 4096 bytes.

+8
source

I would just just drop the entire queue in one go

Since this is UDP, we say here: close(udp_server_socket)and socket () / bind () again?

.

+3

man recv: 0, .

This does not apply to UDP. In UDP, there is no β€œconnection” to close. A return value of 0 is excellent, which means the datagram is not receiving a payload (i.e. only IP and UDP headers.)

Not sure if this helps your problem or not. I really don't understand where you are going with len = 1.

+1
source

All Articles