Thread: TCP socket message size

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    1

    TCP socket message size

    Hi all,

    I want to make a simple POP3 client in C and Linux and I reached a dead end:
    the responses from the POP3 server differ in length (eg: RETR x).

    Is there a way to query the length of the TCP message before actually reading it so I can assign a buffer large enough to hold the data?

    What I want to do is something similar to:

    int length= ??? //message length
    char *buffer=malloc(length)
    buffer=read(...)
    //processing the message
    free(buffer)

    (I found the MSG_PEEK option but from what I have read so far is doesn't seem like a good option; is there another way?)

    Thank you

  2. #2
    Devil™
    Join Date
    Oct 2007
    Location
    IIT-Kharagpur, India
    Posts
    104
    fscanf on the socket file descriptor until EOF..

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by ibanezul View Post
    Hi all,

    I want to make a simple POP3 client in C and Linux and I reached a dead end:
    the responses from the POP3 server differ in length (eg: RETR x).

    Is there a way to query the length of the TCP message before actually reading it so I can assign a buffer large enough to hold the data?
    No sane way, no. You just need to have some logic which allocates a buffer of some reasonable size, and reallocate it to a larger size if you find that it isn't big enough.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Alternatively, you could read "chunks" of data from the TCP connection, and for example link them together in a linked list, and once you see the end of the block, allocate a suitable buffer, then copy the content of your list.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Sasquatch mog's Avatar
    Join Date
    Dec 2006
    Location
    Caves of Narshe
    Posts
    16
    RFC 1939:

    Each command may be up to 40 characters long.
    Responses may be up to 512 characters long, including the terminating CRLF.

    Responses to certain commands are multi-line. In these cases, which
    are clearly indicated below, after sending the first line of the
    response and a CRLF, any additional lines are sent, each terminated
    by a CRLF pair. When all lines of the response have been sent, a
    final line is sent, consisting of a termination octet (decimal code
    046, ".") and a CRLF pair. If any line of the multi-line response
    begins with the termination octet, the line is "byte-stuffed" by
    pre-pending the termination octet to that line of the response.
    Hence a multi-line response is terminated with the five octets
    "CRLF.CRLF". When examining a multi-line response, the client checks
    to see if the line begins with the termination octet. If so and if
    octets other than CRLF follow, the first octet of the line (the
    termination octet) is stripped away. If so and if CRLF immediately
    follows the termination character, then the response from the POP
    server is ended and the line containing ".CRLF" is not considered
    part of the multi-line response.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  2. Confused about Memory
    By gL_nEwB in forum C++ Programming
    Replies: 22
    Last Post: 06-20-2006, 07:32 PM
  3. Making a script language?
    By Blackroot in forum Game Programming
    Replies: 10
    Last Post: 02-16-2006, 02:22 AM
  4. socket newbie, losing a few chars from server to client
    By registering in forum Linux Programming
    Replies: 2
    Last Post: 06-07-2003, 11:48 AM
  5. TCP client/server problem.
    By WL in forum C Programming
    Replies: 0
    Last Post: 10-15-2001, 12:55 AM