delphi - 使用自定义终止符通过 indy 读取字符串

标签 delphi indy freepascal lazarus

我正在使用 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/

相关文章:

delphi - 目标多字节代码页 : how to convert MemoryStream to String 中不存在 Unicode 字符的映射

c - 帕斯卡字符串在内存中是如何表示的?

freepascal - 自由帕斯卡子范围

Delphi 7 ISAPI WebBroker 文件上传

delphi - Delphi 2006 - XE 中是否有新的并行编程功能?

delphi - Indy 在不处理 302 的情况下获取响应文本

android - 如果 Android 中的 HttpGet 操作持续时间过长,如何避免出现错误 10053 (WSAECONNABORTED)?

delphi - FPC 3.0 和 InterlockedCompareExchange

delphi - JVCL安装

delphi - 从 Excel 导入数据