如何将IP地址分配给Delphi Tokyo 10.2中的IdTCPServer
我搜索了很多网站,但没有找到我要找的。
我正在使用以下代码:
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, IdContext, IdBaseComponent, IdComponent,
IdCustomTCPServer, IdTCPServer, Vcl.StdCtrls;
type
TForm1 = class(TForm)
IdTCPServer1: TIdTCPServer;
Memo1: TMemo;
procedure IdTCPServer1Execute(AContext: TIdContext);
procedure FormCreate(Sender: TObject);
procedure IdTCPServer1Connect(AContext: TIdContext);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
IdTCPServer1:=IdTCPServer1.Create(nil);
IdTCPServer1.DefaultPort:=50000;
IdTCPServer1.OnExecute:=IdTCPServer1Execute;
IdTCPServer1.Active:=true;
end;
procedure TForm1.IdTCPServer1Connect(AContext: TIdContext);
begin
memo1.Lines.Add(AContext.Connection.Socket.Binding.PeerIP);
end;
procedure TForm1.IdTCPServer1Execute(AContext: TIdContext);
begin
//message is shown when connection occurs//
showmessage(AContext.Connection.Socket.ReadLn());
end;
end.
最佳答案
抱歉,如果您不这么认为,但我使用类似的代码将IP分配给tcpserver:
...
IdTCPServer1.Bindings.Clear;
IdTCPServer1.Bindings.Add.IP := GetIpAddress();
IdTCPServer1.Bindings.Add.Port := GetPort();
...