Thread: client/server

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    2

    Angry client/server

    Hi,

    I am currently working on a client server program and when the server sends a packet over - I have the following

    while(( n = read(socket, receive_buffer, BUFLEN)) > 0)
    { receive_buffer[n] = 0;
    fputs(receive_buffer, stdout);
    }


    return;

    where BUFLEN = 100, to receive the info from the server. so this will loop through until I am done reading everything, the problem I am having is it dosen't return when it should. Any ideas what I can do?

    Thanks in advance

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > the problem I am having is it dosen't return when it should.
    When should it return?

    It's my understanding that if you send 100 bytes in a single call that it might arrive as follows
    - a single 100 byte message
    - a single byte, then 99 bytes
    - 50 and 50
    etc etc

    You need to have sufficient information at the receiver to reconstruct (in the worst case) a message a byte at a time. In the case of text strings, this means either sending the length in advance, or sending the \0 at the end.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User Engineer's Avatar
    Join Date
    Oct 2001
    Posts
    125

    Talking

    Try checking for " != -1" instead of ">0" as follows:

    while(( n = read(socket, receive_buffer, BUFLEN)) > 0)

    try:

    while(( n = read(socket, receive_buffer, BUFLEN)) != -1)

    Maybe it won't solve the problem, but it might put you on the correct track.
    1 rule of the Samurai Code: if you have nothing to say, don't say anything at all!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. RE: client/server shared memory problem
    By hampycalc in forum C Programming
    Replies: 0
    Last Post: 03-10-2006, 02:26 PM
  2. Client/server won't connect
    By Lateralus in forum Networking/Device Communication
    Replies: 0
    Last Post: 06-15-2005, 07:48 AM
  3. Client/Server and Multithreading
    By osal in forum Windows Programming
    Replies: 2
    Last Post: 07-17-2004, 03:53 AM
  4. client/server program
    By purple in forum C Programming
    Replies: 3
    Last Post: 11-01-2002, 08:52 PM
  5. Client/Server Programming!!!
    By Silverdream in forum C Programming
    Replies: 4
    Last Post: 03-30-2002, 12:19 AM