Hi, is there any way to send a simple text(string value) without using WriteLN and ReadLN?
The ReadLn
function needs LF char and when I'm sending a text message via winsock to Indy, ReadLn
cannot get the message correctly and some exceptions will be happen continuously.
views:
122answers:
2Does your text have any termination character? If it does, then you can set ATerminator parameter of ReadLn, so that instead of LF character, it will terminate reading when it reaches your own defined termination character.
If there is no defined termination character, but you know the maximum length of the text, you can set AMaxLineLength parameter of ReadLn. That will cause ReadLn to accept only text with length less than AMaxLineLength.
If there is no defined termination character, and you do not know the maximum length of received text, you can use ReadStream with its AReadUntilDisconnect parameter set to True, instead of using ReadLn. Of course ReadStream reads the data as a stream of bytes. You can assign a TStringStream to it, so that you can have the received bytes as string. AReadUntilDisconnect parameter tells ReadStream to continue reading received data until the client disconnects. So if you set this parameter, your client should disconnect as soon as the text is sent to server.
If you don't wish to use a termination character (WriteLn/ReadLn do it this way), then write the number of bytes you wish to send first to the socket, and then write out, or read that many bytes on the other side. You could also use a different termination character than newline, which you have duly pulled out of a hat. Perhaps Chr(251), that's a nice one.
In the end, I'll bet you go back to WriteLn/ReadLn. Because it's simple, expected, and it works.