[英]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中建立TIdTCPServer
和TIdTCPClient
之间的通信,这就是我的过程的样子:
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的TIdTCPClient
和TIdTCPServer
)?
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: 从我的角度来看,有两种可能性:
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
设置为正确的值,则实际上将不会连接到预期的服务器。
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.