Reputation: 21
How to assign IP address to IdTCPServer
in delphi tokyo 10.2?
I've search many websites and I haven't found what I am looking for.
I'm using the following code:
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.
Upvotes: 2
Views: 1855
Reputation: 51
Your question is not clear. Which IP address are you referring to when you want to "assign IP address to IdTCPServer" and why are you trying to set it?
Normally a IdTCPServer does not need to know it's own IP address. An IP address of the server is needed by each Client (e.g. the "Public Address") and may not be the same as the address the Server refers to itself as (e.g. If behind a Firewall or Load Balancer). Different clients may use different addresses to refer to the same server.
Remy's answer regarding bindings refers to the ability for a IPServer to serve requests on a subset of the network interfaces present in the Machine. If you make no specification (Very common), you serve the port on every interface (or 0.0.0.0 for IPV4/ ::0 for IPV6). If you do do want all interfaces, then you list the local interface IP Addresses in the Bindings as noted by Remy.
Upvotes: 0
Reputation: 106
Sorry if it's not what you think, but I use a similar code to assign an ip to a TCPServer :
...
IdTCPServer1.Bindings.Clear;
IdTCPServer1.Bindings.Add.IP := GetIpAddress();
IdTCPServer1.Bindings.Add.Port := GetPort();
...
Upvotes: 1