Using multiple Indy Server bundles as separate sockets?

I'm still used to Indy, being a multi-threaded socket system with huge capabilities. One of the big things I've seen is how a server socket can have several different bindings. For example, you might have 3 bindings for 3 ports on the same IP address. I am using Indy 10 on Delphi XE2.

I am rebuilding my old system, which uses the old-fashioned TServerSocket and TClientSocket from ScktComps and re-does it with Indy TIdTCPServer and TIdTCPClient . The old system actually consists of 3 completely different server / client sockets at each end, each socket designed for a specific purpose, and works together - just like FTP uses one socket for binary data and another socket for commands.

Is it possible to simulate three separate server / client sockets inside the same component using these bindings? It would be great if I could only declare one server socket with 3 connected ports and the same one on the client, connected to 3 different ports on the server. All I would like to do is to eliminate the need to create 3 separate socket components for the server / client and combine them into one.

+6
source share
1 answer

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; 
+12
source

Source: https://habr.com/ru/post/917562/


All Articles