Which one does ioctl
of FIONREAD
return, the next packet's length, or the length of all data in the buffer?
Suppose there is a UDP
server that receives 2 packets from a client 1 and another 2 packets from client 2 after client 1's packet.
Then, what is the value of ioctl
of FIONREAD
,
and what does readfrom
return in that case?
Client 1 : v two packets
++UDP Server got 4 packets <- FIONREAD?
Client 2 : ^ two packets
FIONREAD (SIOCINQ) Gets a pointer to an integer as argument. Returns the size of the next pending datagram in the integer in bytes, or 0 when no datagram is pending.
It'll stay in the socket buffer until you read it or the process exits. Once the socket buffer is full, new packets arriving is dropped.
udp_mem (since Linux 2.6. 25) This is a vector of three integers governing the number of pages allowed for queueing by all UDP sockets. min Below this number of pages, UDP is not bothered about its memory appetite. When the amount of memory allocated by UDP exceeds this number, UDP starts to moderate memory usage.
Description. UDP socket routines enable simple IP communication using the user datagram protocol (UDP). The User Datagram Protocol (UDP) runs on top of the Internet Protocol (IP) and was developed for applications that do not require reliability, acknowledgment, or flow control features at the transport layer.
man udp (7) states:
FIONREAD (SIOCINQ) Gets a pointer to an integer as argument. Returns the size of the next pending datagram in the integer in bytes, or 0 when no datagram is pending. Warning: Using FIONREAD, it is impossible to distinguish the case where no datagram is pending from the case where the next pending datagram contains zero bytes of data. It is safer to use select(2), poll(2), or epoll(7) to distinguish these cases.
So, the answer to your question is: FIONREAD returns the size of the next (first) pending datagram.
It is platform-dependent.
See here for further information.
The return value of recvfrom()
is the actual number of bytes transferred.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With