7

I know little about sockets but so far I haven't had much of a problem. I'm actually stuck on how to know when the other side finished sending msgs. What I have so far is a while loop on the server side which reads from the socket 'til has nothing left (or that is what is supposed to do). This is the code:

char c[1024]; //buffer
inst much;
while(much = read(sockfd, &c, 1024) > 0) {
   printf("read %d, clientSays> %s\n", much, c);
}
printf("reading, finished\n");

So, on the client side, I send a "hello world" message which is actually display on the server console, but it doesn't print the "reading finishes" message so I suppose that it gets stuck waiting for another message.

I though that the read function would return 0 when there was nothing else to read, but I guess that's not the case

So, what am I doing wrong?


Update

Actually, after reading your answers and going through the code a little bit, I realized that that's what protocols are for.

I should know before hand when one side has finished sent and when the other side should start writing. Maybe adding a last char to let know that I finished sending, or a prefixed size for the message.

Thanks for your answers.

2 Answers 2

8

Your sample expects read() to return 0, which will happen when client close connection. If you have to keep connection open but know where the end of the message is, you can either prefix each message with its length or add an end of message marker at the end. There is nothing built in into the sockets API to assist you.

1

I know this is a kinda sad/inefficient way to do it, but back in the day I used a char delimiter to know when to stop reading a socket.

Covers head with hands

5
  • 1
    There's really nothing wrong with that. There's really only three methods to know where the end of a message is: 1) signal it by closing the whole connection; 2) signal it with an end-of-message marker; or 3) know how long the message is (either by sending the length first, or having it specified as part of the protocol). ASCII even includes two control codes for delimiting message (STX and ETX).
    – caf
    Nov 9, 2010 at 2:21
  • Wow, for so long I was too embarrassed to show people my game networking code (when they asked)... because I always thought that what I was doing was 'noob'. Thanks buddy!
    – Ben
    Nov 9, 2010 at 2:27
  • jajajaja, well know I'm going to do the same. I didn't know about the ascii delimeters, thanx.
    – gvalero87
    Nov 9, 2010 at 2:30
  • XML is an important special case of (2) and/or (3): a self-describing protocol.
    – user207421
    Nov 9, 2010 at 9:32
  • Many protocols use newlines for end-of-message markers, although you have to look out for carriage returns too.
    – Steve-o
    Nov 13, 2010 at 15:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.