我在动态创建一些 INDY 通信时遇到问题。我以前没有使用过 Indy,我无法正确设置服务器。我收到错误 10061。我在防火墙中添加了一条规则以允许此应用程序和端口。如果我使用 TServerSocket 和 TClientSocket 它可以工作,但我需要使用 Indy。
此代码不起作用
procedure TForm3.Button3Click(Sender: TObject);
var
temp : TIdSocketHandles;
begin
fIdTCPServer1 := TIdTCPServer.Create(nil);
fIdTCPClient1 := TIdTCPClient.Create(nil);
temp := TIdSocketHandles.Create(fIdTCPServer1);
temp.Add.IP := 'xxx.xx.xxx.xx';//'localhost'; //gave error when tryingn to use local host
temp.Add.Port := 20200;
fIdTCPServer1.Bindings := temp;
fIdTCPServer1.OnExecute := IdTCPServer1Execute;
fIdTCPServer1.StartListening();
fIdTCPClient1.Port := 20200;
fIdTCPClient1.Host := 'xxx.xx.xxx.xx';//'localhost';
fIdTCPClient1.Connect; //error 10061 here
fIdTCPClient1.IOHandler.WriteLn('Some message');
end;
procedure TForm3.IdTCPServer1Execute(AContext: TIdContext);
var
inStr : String;
begin
inStr := AContext.Connection.IOHandler.ReadLn;
ShowMessage('Server received : ' + inStr);
end;
此代码有效
procedure TForm3.Button1Click(Sender: TObject);
begin
serverSocket1 := TServerSocket.Create(nil);
clientSocket1 := TClientSocket.Create(nil);
serverSocket1.Port := 20200;
serverSocket1.OnClientRead := ServerSocket1ClientRead;
serverSocket1.Active := True;
clientSocket1.Port := 20200;
clientSocket1.Host := 'localhost';
clientSocket1.Active := True;
clientSocket1.Socket.SendText('Some message');
end;
procedure TForm3.ServerSocket1ClientRead(Sender: TObject; Socket: TCustomWinSocket);
var
inStr : String;
begin
inStr := Socket.ReceiveText();
ShowMessage('Server received : ' + inStr);
end;