What's the right way to receive text on a Socket? I've seen several methods. Could someone give me an example?
I am trying to make a chat program. I have another problem.
I only wanna show the received line if it was received completely (with the CRLF in the end). Can someone help me?
Thanks in advance, and sorry for my bad english.
Posted on 2001-11-22 21:13:37 by dilau

there is no one single method
transferring text or binary data is exactly the same thing from the socket's perspective.
It really depends on how you want to handle it and the protocol you have written/are using
Say, you could do something like this, where you prefix the message with it's length:

1) user on peer A enters a 11 characters string on the terminal and presses Enter
2) peer A sends to B something like: 13,"hello world",0Dh,0Ah
3) peer B receives the prefix, allocates a 13 bytes long buffer and loops on recv until it has filled a buffer (depends really if you are using async non-blocking sockets or blocking sockets)
4) once buffer is full it will display the text on user B's terminal

It might look a strange way to handle text, the advantage however being that you can implement non-text commands/transfers this way

Or you can just react to FD_RECV messages (if using async sockets) buffering whatever you get and scanning for the CRLF code (a bit like a telnet terminal)

It's up to you really

Posted on 2001-11-23 03:12:31 by random
Hello there!

When handling strings, as you said, you often need to be able to receive text line by line. That means you must know where the line ends. In the DOS/Windows environment line end is coded as CRLF = 0x0D,0x0A. In UNIX-like systems line end is coded as LF only.
If you are receiving only text, the CR is not very important, so you can scan for LF only. This way you will satisfy the line-end-condition for both Windows- and UNIX-based systems.


Posted on 2001-11-23 06:54:13 by Beg
Very good point, although, to be honest with you, I very much doubt he's going to "port" his chat program to unix ;)
Posted on 2001-11-23 09:37:33 by random
In fact sometimes a single CR is used as line terminator as wel...
The code I use for parsing http messages would have been much easier if everyone just agreed to one standard :rolleyes:. With http messages, this is a bigger problem as firstly, you don't know which type of line terminator the client uses, and secondly, what the message length is. This can be a problem when you receive this:

Someheader: datavalue[CR]

More data may be comming, so you don't know if it's a CR, or a CRLF where the LF has yet to come..
I found the right algorithm for this problem that will work in all cases, unless the client is inconsistent in using line terminators but luckily such a client may be considered a bad client :).
However if you have the size of the data you're going to receive there's no problem with line terminators.

Posted on 2001-11-23 15:59:16 by Thomas
Thanks guys! :)
I'll try it here.
Posted on 2001-11-24 21:36:42 by dilau