0

我在动态创建一些 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;
4

2 回答 2

1

您需要设置IP地址和port每个绑定。

// Here you're adding a binding and setting only the IP address
temp.Add.IP := 'xxx.xx.xxx.xx';
// Here you're adding another binding and setting only the port
temp.Add.Port := 20200;

这是正确执行此操作的简单方法:

with fIdTCPServer1.Bindings.Add do
begin 
  IP := '127.0.0.1'; // Use IP address, not host name. Localhost = 127.0.0.1
  Port := 20200;
end;

这是另一种更详细的方法(添加IdSocketHandle到您的使用子句中):

var
  socketHandle: TIdSocketHandle;
begin
  socketHandle := fIdTCPServer1.Bindings.Add;
  socketHandle.IP := '127.0.0.1';
  socketHandle.Port := 20200;
end; 

此外,要启动它,您只需将 Active 设置为 true,如下所示:

fIdTCPServer1.Active := True;
于 2013-10-10T00:27:29.803 回答
0

您的代码存在一些问题。

  1. 您在滥用TIdTCPServer.Bindings,并且您没有正确激活服务器。

  2. 您正在事件中调用ShowMessage()OnExecuteTIdTCPServer它是多线程组件(每个连接的客户端都在自己的线程中运行)并且ShowMessage()不是线程安全的。

  3. 您的代码中有一些潜在的内存泄漏。

试试这个:

procedure TForm3.Button3Click(Sender: TObject);
var
  temp : TIdSocketHandle;
begin
  if fIdTCPServer1 = nil then
  begin
    fIdTCPServer1 := TIdTCPServer.Create(Self);
    temp := fIdTCPServer1.Bindings.Add;
    temp.IP := 'xxx.xx.xxx.xx';//'localhost'; //gave error when tryingn to use local host
    temp.Port := 20200;
    fIdTCPServer1.OnExecute := IdTCPServer1Execute;
    fIdTCPServer1.Active := True;
  end;

  if fIdTCPClient1 = nil then
  begin
    fIdTCPClient1 := TIdTCPClient.Create(Self);
    fIdTCPClient1.Port := 20200;
    fIdTCPClient1.Host := 'xxx.xx.xxx.xx';//'localhost';
  end;

  fIdTCPClient1.Connect;
  fIdTCPClient1.IOHandler.WriteLn('Some message');
end;

procedure TForm3.IdTCPServer1Execute(AContext: TIdContext);
var
  inStr : String;
begin
  inStr := AContext.Connection.IOHandler.ReadLn;
  Windows.MessageBox(0, PChar(inStr), PChar('Server received'), MB_OK);
end;
于 2013-10-10T06:58:19.663 回答