Delphi idtcpserver idtcpclient tstringstream 到字符串

Delphi idtcpserver idtcpclient tstringstream to string

我尝试将字符串从 tidtcpserver 发送到 Delphi 中的 tidtcpclient,但是当我发送字符串时,客户端什么也没收到。我没有收到任何错误。我正在使用 tstringstream 因为我想发送一个 base64 字符串(writeln/readln 不能 send/receive 那么多文本) 这是 idtcpserver1 中的发送代码:

procedure TForm1.sendtext(index:integer; txt:string);
var
  StrStream:tStream;
  List: TList;
  AContext: TIdContext;
begin
  txt := trim(txt);
  strstream := TMemoryStream.Create;
  strstream.Write(PAnsiChar(txt)^, Length(txt));
  strstream.Position := 0;
  List := idTcpServer1.Contexts.LockList;
  AContext := TIdContext(List[index]);
  AContext.Connection.IOHandler.write(StrStream);
  idTcpServer1.Contexts.UnLockList;
end;

这是idtcpclient1中读取的代码

procedure TIndyClient.Execute;
var
StrStream:tStringStream;
receivedtext:string;
begin
StrStream := tstringstream.Create;
form1.IdTCPClient1.IOHandler.readstream(StrStream);
receivedtext := strstream.DataString;
if receivedtext = '' = false then
begin
  showmessage(receivedtext);
end;
end;

有谁知道我做错了什么吗?

您犯了一个非常常见的新手错误,即不匹配 IOHandler.Write(TStream)IOHandler.ReadStream() 调用。默认情况下,ReadStream()希望流数据前面有流大小,但Write(TStream)默认不发送流大小。

试试这个:

procedure TForm1.sendtext(index: integer; const txt: string);
var
  StrStream: TMemoryStream;
  List: TList;
  AContext: TIdContext;
begin
  strStream := TMemoryStream.Create;
  try
    WriteStringToStream(StrStream, Trim(txt), enUTF8);
    List := idTcpServer1.Contexts.LockList;
    try
      AContext := TIdContext(List[index]);
      AContext.Connection.IOHandler.Write(StrStream, 0, True);
    finally
      idTcpServer1.Contexts.UnlockList;
    end;
  finally
    StrStream.Free;
  end;
end;

procedure TIndyClient.Execute;
var
  StrStream: TMemoryStream;
  receivedtext: string;
begin
  StrStream := TMemoryStream.Create;
  try
    Form1.IdTCPClient1.IOHandler.ReadStream(StrStream, -1, False);
    StrStream.Position := 0;
    receivedtext := ReadStringFromStream(StrStream, enUTF8);
  finally
    StrStream.Free;
  end;
  if receivedtext <> '' then
  begin
    // ShowMessage() is not thread-safe...
    MessageBox(0, PChar(receivedtext), PChar(Application.Title), MB_OK);
  end;
end;

或者,因为现在正在发送流大小,接收代码可以使用 ReadString() 而不是 ReadStream():

procedure TIndyClient.Execute;
var
  receivedtext: string;
begin
  with Form1.IdTCPClient1.IOHandler do
  begin
    // you can alternatively set the IOHandler.DefStringEncoding
    // instead of passing the encoding as a parameter...
    receivedtext := ReadString(ReadLongInt, enUTF8);
  end;
  if receivedtext <> '' then
  begin
    // ShowMessage() is not thread-safe...
    MessageBox(0, PChar(receivedtext), PChar(Application.Title), MB_OK);
  end;
end;

顺便说一句,WriteLn()/ReadLn() 没有任何长度限制,只要有可用内存,它们就可以处理大字符串(并且发送的文本中没有任何换行符)。您可能对 ReadLn() 受到 IOHandler.MaxLineLength 的约束感到困惑,默认设置为 16K 个字符,但您可以绕过它:

procedure TForm1.sendtext(index: integer; const txt: string);
va
  List: TList;
  AContext: TIdContext;
begin
  List := idTcpServer1.Contexts.LockList;
  try
    AContext := TIdContext(List[index]);
    // you can alternatively set the IOHandler.DefStringEncoding
    // instead of passing the encoding as a parameter...
    AContext.Connection.IOHandler.WriteLn(Trim(txt), enUTF8);
  finally
    idTcpServer1.Contexts.UnlockList;
  end;
end;

procedure TIndyClient.Execute;
var
  receivedtext: string;
begin
  // you can alternatively set the IOHandler.DefStringEncoding
  // and IOHandler.MaxLineLength instead of passing them as parameters...
  receivedtext := Form1.IdTCPClient1.IOHandler.ReadLn(LF, -1, MaxInt, enUTF8);
  if receivedtext <> '' then
  begin
    // ShowMessage() is not thread-safe...
    MessageBox(0, PChar(receivedtext), PChar(Application.Title), MB_OK);
  end;
end;