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

2

6

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
Zizith answered 8/6, 2013 at 3:36 Comment(1)
Hello! Checkout the new answer.Monegasque
M
6

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.

Monegasque answered 21/8, 2013 at 6:17 Comment(3)
So you mean the whole packet's size or just the data in the datagram packet?Zizith
@jinoh67 I'm not quite sure about it, since man-pages don't say it explicitly, but I'm going to find it out experimentally today.Monegasque
@jinoh67 I've just checked it: on my Debian 7 FIONREAD returns the size of data contained in datagram (datagram message size).Monegasque
M
3

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.

Massimo answered 8/6, 2013 at 8:17 Comment(4)
So you mean 3 is correct for FIONREAD, and 2 (length of packet) for recvfrom? (I use non-blocking socket, of course)Zizith
I meant what I said, and it applies to both UDP and TCP using the wording I provided.Massimo
I am not sure that you answer is correct for a UDP socket. Do you have any references to FIONREAD returns the total number of bytes that can be read without blocking. I've posted an answer with a reference to a different information.Monegasque
@Monegasque We're both right, see edit.Massimo

© 2022 - 2024 — McMap. All rights reserved.