简体   繁体   English

使用Indy(Delphi)发送和接收简单的字符串值

[英]Send and Receive a simple string value with Indy (Delphi)

is there any way to send a simple text(string value) without using WriteLN and ReadLN? 有什么方法可以在不使用WriteLN和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. ReadLn函数需要LF字符,当我通过Winsock向Indy发送文本消息时, ReadLn无法正确获取消息,并且某些异常将连续发生。

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. 如果您不想使用终止符(WriteLn / ReadLn这样做),则将要发送的字节数首先写入套接字,然后写出,或者在另一侧读取那么多字节。 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. 也许Chr(251),这是一个不错的选择。

In the end, I'll bet you go back to WriteLn/ReadLn. 最后,我敢打赌您会回到WriteLn / ReadLn。 Because it's simple, expected, and it works. 因为它很简单,可以预期并且可以正常工作。

Does 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. 如果是这样,则可以设置ReadLn的ATerminator参数,以便当到达自己定义的终止字符时,它将终止LF字符而不是LF字符。

If there is no defined termination character, but you know the maximum length of the text, you can set AMaxLineLength parameter of ReadLn. 如果没有定义的终止符,但是您知道文本的最大长度,则可以设置ReadLn的AMaxLineLength参数。 That will cause ReadLn to accept only text with length less than AMaxLineLength. 这将导致ReadLn仅接受长度小于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. 如果没有定义的终止字符,并且您不知道接收到的文本的最大长度,则可以将ReadStream的AReadUntilDisconnect参数设置为True,而不使用ReadLn。 Of course ReadStream reads the data as a stream of bytes. 当然,ReadStream将数据作为字节流读取。 You can assign a TStringStream to it, so that you can have the received bytes as string. 您可以为其分配一个TStringStream,以便可以将接收到的字节作为字符串。 AReadUntilDisconnect parameter tells ReadStream to continue reading received data until the client disconnects. AReadUntilDisconnect参数告诉ReadStream继续读取接收到的数据,直到客户端断开连接为止。 So if you set this parameter, your client should disconnect as soon as the text is sent to server. 因此,如果设置此参数,则客户端应在文本发送到服务器后立即断开连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM