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

标签 multithreading sockets delphi tcp indy

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

发送命令很简单:只需向套接字写入一些内容。但是为了能够重播它们,应用程序必须从套接字读取,如果它读取,它就不能写入....我在互联网上搜索并找到了here。和 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.

那完全没问题。只需确保在销毁连接对象之前终止读取线程即可。

关于multithreading - 如何使用 Indy 建立双向 TCP 连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45762707/

相关文章:

c - 如何正确使用 SO_KEEPALIVE 选项来检测另一端的客户端是否已关闭?

ruby - Ruby:您可以发送套接字作为参数吗?

c++ - 如何在 Linux 中读取/dev/ttyUSB0 设备?

delphi - 使用 Indy 提高传输速度

c# - 基于CompareExchange的互锁实现是否应该使用SpinWait?

java - Java 中的管道或交换输入/输出流

java - 了解 java 线程转储中的行

regex - Delphi - TPerlRegEx/RegExBuddy 问题

Delphi XE 8 - 如何解压缩 gzip 文件?

iphone - Objective-C中的静态变量和多个线程