Thread: bytes lost with partial read in UDP

  1. #1
    Alessio Stella
    Join Date
    May 2008
    Location
    Italy, Bologna
    Posts
    251

    bytes lost with partial read in UDP

    With a UDP (DGRAM) socket
    if i read() n bytes and the packet size is m bytes > n bytes
    do i loose the m-n bytes?

    I tried and i think as a default the answre is yes

    But is there an option I can set that prevents this?

    Here pread(3): read from file - Linux man page I found
    A read() from a STREAMS file can read data in three different modes: byte-stream mode, message-nondiscard mode, and message-discard mode. The default shall be byte-stream mode. This can be changed using the I_SRDOPT ioctl() request, and can be tested with I_GRDOPT ioctl(). In byte-stream mode, read() shall retrieve data from the STREAM until as many bytes as were requested are transferred, or until there is no more data to be retrieved. Byte-stream mode ignores message boundaries.

    In STREAMS message-nondiscard mode, read() shall retrieve data until as many bytes as were requested are transferred, or until a message boundary is reached. If read() does not retrieve all the data in a message, the remaining data shall be left on the STREAM, and can be retrieved by the next read() call. Message-discard mode also retrieves data until as many bytes as were requested are transferred, or a message boundary is reached. However, unread data remaining in a message after the read() returns shall be discarded, and shall not be available for a subsequent read(), getmsg(), or getpmsg() call.
    but it seems valid only for TCP sockets and not for UDP sockets
    isn't it?

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    You can use the MSG_PEEK option which allows to read the reception buffer without discarding it (man recv for details)
    The MSG_PEEK flag causes the receive operation to
    return data from the beginning of the receive queue without removing that
    data from the queue. Thus, a subsequent receive call will return the
    same data.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    In this sort of situation, it is common to use a buffer size of 1500 bytes. This is the MTU of the ethernet protocol, so you can be sure that you will be able to read in all the data for each packet.

  4. #4
    Alessio Stella
    Join Date
    May 2008
    Location
    Italy, Bologna
    Posts
    251
    Quote Originally Posted by bithub View Post
    In this sort of situation, it is common to use a buffer size of 1500 bytes. This is the MTU of the ethernet protocol, so you can be sure that you will be able to read in all the data for each packet.
    you mean 9000 as i am working with jumbo frames?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Page File counter and Private Bytes Counter
    By George2 in forum Tech Board
    Replies: 0
    Last Post: 01-31-2008, 03:17 AM
  2. Shared class members over Dll Boundary
    By Elysia in forum C++ Programming
    Replies: 19
    Last Post: 11-13-2007, 01:43 PM
  3. Reading Certain number of bytes from a file
    By (TNT) in forum Windows Programming
    Replies: 6
    Last Post: 01-14-2002, 08:35 PM
  4. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 10:52 AM
  5. Replies: 1
    Last Post: 12-31-2001, 05:04 PM