Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does FIONREAD of UDP (datagram) sockets return? [duplicate]

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? (server)

  1. length of client 1's first packet
  2. length of client 1's two packets
  3. length of client 1's two packets + client 2's two packets
  4. length of client 1's first packet + client 2's first packet
  5. other
like image 316
minmaxavg Avatar asked Jun 08 '13 03:06

minmaxavg


People also ask

What is Fionread?

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.

What happens when UDP socket buffer is full?

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.

What is UDP_ mem?

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.

What is UDP socket?

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.


2 Answers

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.

like image 197
Kolyunya Avatar answered Sep 24 '22 15:09

Kolyunya


It is platform-dependent.

  • On some platforms, FIONREAD on a UDP socket returns the size of the first datagram.
  • On others it returns the total number of bytes that can be read without blocking, which is the total number of bytes presently in the socket receive buffer.

See here for further information.

The return value of recvfrom() is the actual number of bytes transferred.

like image 22
user207421 Avatar answered Sep 25 '22 15:09

user207421