我在動態創建某些INDY通信時遇到問題。我之前沒有使用過Indy,也無法讓服務器正確設置。我得到了錯誤10061.我在防火牆上添加了一條規則以允許此應用程序和端口。它適用於如果我使用TServerSocket和TClientSocket但我需要使用Indy。Indy動態創建服務器客戶端連接錯誤10061
此代碼不能正常工作
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;
謝謝,這是完美的。 – runfastman