3

我正在使用 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,所以这可能是一些移植问题

谢谢你。

4

1 回答 1

3

我不喜欢回答自己的问题,但根据 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;  
于 2013-08-05T10:38:58.230 回答