Continuous socket stream,

This is a discussion on Continuous socket stream, within the C Programming forums, part of the General Programming Boards category; Hello, Im writting a socket-based program that connects to a server, which sends a continous stream of data through a ...

  1. #1
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459

    Continuous socket stream,

    Hello,

    Im writting a socket-based program that connects to a server, which sends a continous stream of data through a TCP socket... Which I which to read line-by-line.

    Now what is the best way I can read each line of this stream?

    I've thought of something like:
    Code:
    char c;
    char * line;
    unsigned long int totalSize = 0;
    /* while we're getting data */
    while(recv(sock, &c, 1, 0) > 0)
    {
        totalSize++;
        line = realloc(totalSize);
        /* add c to line */
       /* check for new line */
    }
    Or is there some other way, because memory usage could get very high if a new line never comes?

    Thanks in Advance
    Last edited by zacs7; 05-07-2007 at 10:29 PM.

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    reading byte-byte and then calling realloc on each char - brrrr.... I wouldn't do it...

    I'd make a static buffer of say 2048 bytes...
    Read up to this limit, then look for the end-line char in the received data and process this part...
    the last part - without end-line I will move to some dynamically resizeing buffer.
    And repeate the read...

    This approach makes smaller number of IO operations and reallocations needed...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Day Dreamer
    Join Date
    Apr 2007
    Posts
    45
    reallocating each time!
    That definitely is not advisable.

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    I know, thats why I asked

    Thanks for the suggestion vart, works like a charm

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stream Server
    By ch4 in forum Networking/Device Communication
    Replies: 18
    Last Post: 06-29-2009, 03:09 PM
  2. socket programming question, closing sockets...
    By ursula in forum Networking/Device Communication
    Replies: 2
    Last Post: 05-31-2009, 05:17 PM
  3. Sockets: send/receive continuous stream of data?
    By diddy02 in forum Networking/Device Communication
    Replies: 1
    Last Post: 08-09-2008, 12:52 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. stream socket problem
    By WL in forum C Programming
    Replies: 2
    Last Post: 10-01-2001, 11:07 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21