Yes, you can use one TIdTCPServer
to manage multiple ports at the same time. However, on the client side, you still need 3 separate client components to connect to different ports.
Create 3 entries in the TIdTCPServer.Bindings
collection, one for each local IP / port you want to listen to, where the TIdSocketHandle.Port
property will be equivalent to the TServerSocket.Port
property. TServerSocket
does not support binding to a specific IP (although this can be done with some manual work), but the TIdSocketHandle.IP
property is used for this purpose, where an empty string is equivalent to INADDR_ANY
.
In the TIdCPServer.OnConnect
, TIdCPServer.OnDisconnect
and TIdCPServer.OnExecute
events, you can use the TIdContext.Binding.IP
and TIdContext.Binding.Port
properties to distinguish which binding the calling socket is connected to.
Common to this is support for SSL and non-SSL clients on different ports, such as protocols such as POP3 and SMTP, which support implicit and explicit SSL / TLS on different ports. TIdHTTPServer
does this to support HTTP
and HTTPS
links on the same server (you can use TIdHTTPServer.OnQuerySSLPort
to configure which ports use SSL / TLS rather than).
For instance:
procedure TForm1.StartButtonCick(Sender: TObject); begin IdTCPServer1.Active := False; IdTCPServer1.Bindings.Clear; with IdTCPServer1.Bindings.Add do begin IP := ...; Port := 2000; end; with IdTCPServer1.Bindings.Add do begin IP := ...; Port := 2001; end; with IdTCPServer1.Bindings.Add do begin IP := ...; Port := 2002; end; IdTCPServer1.Active := True; end; procedure TForm1.IdTCPServer1Execute(AContext: TIdContext); begin case AContext.Binding.Port of 2000: begin // do something... end; 2001: begin // do something else... end; 2002: begin // do yet something else ... end; end; end;
source share