如何使用 Indy 建立双向 TCP 连接?

How to make a bidirectional TCP connection using Indy?

我使用 TIdTCPClientTIdTCPServer(使用我自己的协议),我想建立一个 TCP 连接,两台计算机都可以在其中发送命令和读取重播。当然,一个是server,一个是client,但是在建立连接之前,两者都必须是"servers and clients"。我不知道如何更清楚地解释这个......主要是计算机"A"会向计算机"B"发送命令,但有时,一些事件发生在计算机"B"上并且必须是立即与计算机通信 "A"。因此,每台计算机都应该一直监听命令,但同时它必须能够在事件发生时发送一些命令。

发送命令很简单:只需向套接字写入一些内容即可。但是为了能够重放它们,应用程序必须从套接字读取,如果它读取,它就不能写入....我搜索了互联网,我找到了 here and here 一些类似问题的答案,其中是说应该用2个线程,一个写,一个读。但是我不明白我怎么能在 2 个线程中使用同一个对象,即连接套接字...如果一个线程读取的内容被另一个线程更改了怎么办(基本线程问题)?

我已经使用以下代码对聊天应用程序进行了一些测试,它似乎工作正常,但我不确定这是否是正确的方法...我的意思是,创建连接并之后将套接字对象传递给两个线程,一个用于读取,一个用于写入。

constructor TReadingThread.Create(ASocket: TIdIOHandlerSocket);
begin
  FSocket := ASocket;
  inherited Create(False);
end;

procedure TReadingThread.Execute;
var
  cmd: string;
begin
  while not Terminated do
  begin
    cmd := FSocket.ReadLn;
    Trim(cmd);
    if cmd <> '' then
    Synchronize(
    procedure
    begin
      Form1.Display.Lines.Add(cmd);
    end);
  end;
end;

procedure TForm1.BConnectClick(Sender: TObject);
begin
 TCPClient.Connect;
end;

procedure TForm1.InputKeyPress(Sender: TObject; var Key: Char);
begin
 if Key=#13 then begin
  TCPClient.Socket.WriteLn(Input.Text);
  Input.Clear;
  Key:=#0;
 end;
end;

procedure TForm1.TCPClientConnected(Sender: TObject);
begin
 readthread:= TReadingThread.Create(TCPClient.Socket);
 Display.Lines.Add('Server: Connected.');
end;

computer "A" will send commands to computer "B", but sometimes, some events happen on the computer "B" and must be communicated immediately to computer "A". So, every computer should listen all the time for commands, but in the same time it must be able to send some commands when the events are happening.

当 A 向 B 发送命令时,反之亦然,它无法知道收到的下一条消息是对命令的响应还是未经请求的事件。所以它不能只是发送命令并等待响应,因为它可能同时接收其他消息。您将不得不异步读取消息并在消息到达时逐条处理它们。

为了帮助促进这一点,每条消息都必须标识它是什么(命令、响应或事件)。发送命令时,发送方应在其中包含一个用户定义的值,该值会在响应中回显。这将允许发送者将响应与命令相关联,甚至可以同时执行多个命令。未经请求的事件不会有用户定义的值,因为它们没有得到响应。

这样,事件就可以随时发送,当收到命令时,它可以与其他任何东西并行处理,并在准备就绪时发回响应(即使其他命令顺序不对) ,如果需要的话)。

For sending commands it's simple: just write something to the socket. But to be able to replay them, the application must read from socket, and if it read, it can't write....

是的,可以。如果线程被阻止做其他事情,你就不能在同一个线程中执行它。

I searched the internet and I found here and here some answers to similar questions, in which is said that should be used 2 threads, one for writing and one for reading.

是的,这是一种解决方案,考虑到它的阻塞特性,您最终可能会在 Indy 中使用这种解决方案。但它不是一般使用套接字时的唯一解决方案,还有更好的解决方案(重叠I/O等)。

But I don't understand how cand I use the same object, the connection socket, in 2 threads...

为什么您认为它不能共享?套接字具有单独的入站和出站缓冲区。一个线程可以在读取入站数据的同时另一个线程正在写入出站数据。他们不会互相干扰。

What if one thread reads something that is changed by the other (the basic thread problem) ?

写入端工作时读取端没有任何更改,反之亦然。

你要注意的是多个线程同时从套接字读取,或者多个线程同时写入套接字。如果线程之间没有足够的同步,那是不安全的。但是一个线程读取一个线程写入是非常安全的,无需同步两个线程。

I have made some tests with a chat application with the following code and it seems it's working fine but I'm not sure if this is the correct way to do it... I mean, to create a connection and after that pass the socket object to two threads, one for read and one for write.

很好。只需确保读取线程在连接对象被销毁之前终止。