Thread: Question about reading from socket

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    66

    Question about reading from socket

    Hi.

    I`m working on a socket server written in c, where I have to read (and parse) the header informations sent by a browser. The problem is that because of the possible POST and GET variables I don`t know exactly how long the header string will be so I can`t declare the read buffer statically (for ex. char buf[512]). I thought about two possibilities:

    1. read the bytes from the socket one by one, and reallocate the space for the buffer each time.
    2. allocate say 128 or 256 bytes for the buffer from the start, and increase it if necessary.


    What do you think, what would be the best way to do this? Any suggestion is welcome.


    Thank you.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Something more like #2. Calling realloc repeatedly for individual bytes is silly. Read into an array of fixed size, like 1024. Base your malloc and realloc on how many bytes read returns so you can avoid any over-allocating. Just make sure to use a temporary pointer to store realloc's return value in case it fails for some reason. Otherwise, you lose the pointer to your original data.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Generally for input buffers on socket networking I allocate at least, 16k... of course you're not going to do that with char buf[16384]... you'll be using malloc().

  4. #4
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    Look into incremental parsing; just because you don't know how long the message will be or how many reads it may take doesn't mean you can't make use of it.

    Say you are going to be copying "POST" into a buffer for later perusal. You don't necessarily need to continually repeat a `READ->COPY->ENLARGE' cycle for every byte in the message only to allocate additional memory for storing the "POST" portion of the message and copying that portion when you have already found the "POST" portion you conditionally copy to the buffer set aside for that information.

    Soma

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading multiple lines from a socket
    By newbie30 in forum C Programming
    Replies: 4
    Last Post: 08-26-2009, 02:45 AM
  2. UDP socket partial reading
    By mynickmynick in forum Networking/Device Communication
    Replies: 0
    Last Post: 03-25-2009, 11:43 AM
  3. Reading from UDP socket on specific interface
    By uriel in forum Networking/Device Communication
    Replies: 2
    Last Post: 11-29-2007, 11:09 PM
  4. Slight problem with socket reading!!!
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 02-15-2006, 09:55 AM
  5. reading from the open socket then writing to a file...
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-06-2002, 04:15 PM