我正在使用 TIdTcpServer 组件来实现基本的服务器端应用程序。我有客户端将字符串发送到服务器,ascii 编码,以字符 #@ 结尾。
例如:
this_is_a_sample#@ thisis_another_sample#@
我的 OnExecute 方法如下:
procedure TLX8511.ProcessEvent(AContext: TIdContext);
var
recv : String;
begin
with AContext.Connection.IOHandler do
begin
CheckForDataOnSource(20);
if not InputBufferIsEmpty then
begin
recv := ReadLn('#@');
WriteLn(recv);
end;
end;
end
但是,当执行 ReadLn 时,我收到一个奇怪的错误:必须指定缓冲区终止符
我做错了什么?
LE:我在 Linux 上使用 Indy 和 lazarus,所以这可能是一些移植问题
谢谢。
最佳答案
我不喜欢回答自己的问题,但根据 David Heffernan 的建议,我自己发现了这个问题。
事实证明,您必须指定编码以避免出现该异常,因此解决方案如下:
procedure TLX8511.ProcessEvent(AContext: TIdContext);
var
recv : String;
encoding : IIdTextEncoding;
begin
encoding := IndyTextEncoding_ASCII;
with AContext.Connection.IOHandler do
begin
CheckForDataOnSource(20);
if not InputBufferIsEmpty then
begin
recv := ReadLn('#@',encoding,encoding);
WriteLn(recv);
end;
end;
end;
关于delphi - 使用自定义终止符通过 indy 读取字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18054919/