reading multiple lines from a socket

This is a discussion on reading multiple lines from a socket within the C Programming forums, part of the General Programming Boards category; Hi; I want to be able to send multiple writes over a socket and be able to read the multiple ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    58

    reading multiple lines from a socket

    Hi;

    I want to be able to send multiple writes over a socket and be able to read the multiple data. Now i can write the data no problem but when i read it, it seems to get messed up, and i put an counter in the loop and its value is 7 even though i only send 4 pieces of info over, here is the code

    Code:
    //cleint
    while(fgets(line, sizeof line, fp) )
         write(sockfd, line, strlen(line)+1));
    
    
    //server
    while(read(new_fd, buffer, strlen(buffer) )
           printf("%s\n", buffer);

    Any ideas?

    Thanks

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,600
    Why are you using strlen(buffer) on the server side? That value will be constantly changing. Pass the actual size of the buffer. Check the return value of read to get the actual bytes read (and come to think of it, check the return value of write to make sure the data was sent).

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    58
    ok, i have it working, was the lenght of the strlen of buffer, so why was the strlen causing the problem?

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,600
    Well think about it. Let's say you receive "\0" (one byte) in one call. The next time around, the buffer "size" (eg: strlen(buffer)) is going to be 0. See?

  5. #5
    Registered User
    Join Date
    Aug 2009
    Posts
    58
    Yes i do, that makes total sense.

    Thanks a mill.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with a loop reading lines of a file
    By PAragonxd in forum C++ Programming
    Replies: 5
    Last Post: 09-14-2008, 12:57 AM
  2. Reading multiple data types to file
    By Wicket in forum C++ Programming
    Replies: 2
    Last Post: 04-05-2008, 10:14 AM
  3. Slight problem with socket reading!!!
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 02-15-2006, 08:55 AM
  4. Reading Lines from a file
    By phoenix-47 in forum C++ Programming
    Replies: 3
    Last Post: 12-13-2005, 02:32 PM
  5. Multiple lines on an Edit box
    By RubenJ in forum Windows Programming
    Replies: 3
    Last Post: 09-20-2001, 02:51 PM

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