简体   繁体   English

无法使用TIdTCPClient接收来自TIdTCPServer的响应

[英]Unable to receive response from TIdTCPServer using TIdTCPClient

I want to establish a communication between TIdTCPServer and TIdTCPClient in delphi and this is how my procedures look : 我想在delphi中建立TIdTCPServerTIdTCPClient之间的通信,这就是我的过程的样子:
1.Server side : 1,服务器端

procedure TMainForm.IdTCPServer1Execute(AContext: TIdContext);
var
  clientReq, clientName : String;
begin
  clientReq := AContext.Connection.IOHandler.ReadLn(); // client sends request
  clientName := extractClientName(clientReq);  
  AContext.Connection.IOHandler.WriteLn('Hello ' + clientName);
end;

2.Client side : 2.客户端:

procedure TMainForm.btnTestClientClick(Sender: TObject);
var
  testTCP : TIdTCPClient;
  clientReq, serverResponse : String;
begin
  testTCP := TIdTCPClient.Create;
  try
    testTCP.Host := wantedHost;
    testTCP.Port := wantedPort;
    testTCP.Connect;
    clientReq := 'Hello, my Name is user1.';
    testTCP.IOHandler.WriteLn(clientReq);
    try
      serverResponse := testTCP.IOHandler.ReadLn();
    except on e : Exception do begin
      ShowMessage('Error reading response =' + e.Message);
    end;
    end;
  finally
    FreeAndNil(testTCP);
  end;
end;

I connect to the server but than my application freezes when I try to receive the response from the server OnExecute event with my TCPClient.IOHandler.ReadLn method. 我连接到服务器,但是当我尝试使用TCPClient.IOHandler.ReadLn方法从服务器的OnExecute事件接收响应时,应用程序冻结。 Can anyone help me fix my code or show me a working example of what I'm trying to do (with Indy's TIdTCPClient and TIdTCPServer ) ? 谁能帮助我修复代码或向我展示我正在尝试做的工作示例(使用Indy的TIdTCPClientTIdTCPServer )?

There is nothing wrong with the code you have shown, so the problem has to be in the code you have not shown. 您显示的代码没有错,因此问题必须出在您未显示的代码中。 The way I see it, there are two possibilities: 从我的角度来看,有两种可能性:

  1. If you are not setting wantedHost and/or wantedPort to the correct values, you would not actually be connecting to your expected server. 如果未将wantedHost和/或wantedPort设置为正确的值,则实际上将不会连接到预期的服务器。

  2. If extractClientName() is getting stuck internally and not exiting, the server would not be sending any response. 如果extractClientName()在内部卡住并且没有退出,则服务器将不会发送任何响应。 One way that could happen is if you are running the client and server in the same process, and extractClientName() syncs with the main thread, but the main thread is blocked waiting on the client and cannot process the sync, so a deadlock occurs. 可能发生的一种方式是,如果您在同一进程中运行客户端和服务器,并且extractClientName()与主线程同步,但是主线程在客户端上等待被阻塞,无法处理同步,因此会发生死锁。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用TIdTCPServer / TIdTCPClient组件的Delphi中的数据丢失 - Data loss in Delphi using TIdTCPServer/TIdTCPClient components 使用Delphi XE2中的TidTCPClient和TidTCPServer发送和接收流 - Send and Receive Stream with TidTCPClient and TidTCPServer in Delphi XE2 如何将多线TStrings数据从TIdTCPServer传递到TIdTCPClient - How to pass multilined TStrings data from a TIdTCPServer to TIdTCPClient Delphi TidTCPServer和TidTCPClient传输记录 - Delphi TidTCPServer and TidTCPClient transferring a record Indy TIdTCPServer TIdTCPClient数据交换 - Indy TIdTCPServer TIdTCPClient data exchange 使用TIdTcpClient接收超时的部分数据 - Receive partial data with timeout using TIdTcpClient 如何在TIdTcpClient和TIdTcpServer之间建立简单的连接以将字符串从客户端发送到服务器? - How to set up a simple connection between TIdTcpClient and TIdTcpServer to send a string from client to server? ¿我如何从tidtcpclient和tidtcpserver发送和接收字符串并创建聊天? - ¿How can I send and recieve strings from tidtcpclient and tidtcpserver and to create a chat? TIdTCPClient Android 未连接到本地 PC 上的 TIdTCPServer - TIdTCPClient Android not connecting to TIdTCPServer on local PC 如何在Android / IOS上将TIdTCPClient连接到TIdTCPServer? - How to connect TIdTCPClient to TIdTCPServer on Android/IOS?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM