I have 2 applications running on the same machine, one acting as a TCP server and other as a TCP client. In that machine, I have 2 network interface, so it's mandatory to tell which one, the TCP connection should use. For this purpose, I've been using Bind method because I know that one is 192.168.1.x and other is 10.1.135.x, but my doubt is: When I put the machine IP itself, for instance, 192.168.1.195 this doesn't work, but if I put the gateway IP 192.168.1.1 it Works. The code that doesn't work is
tmpTcp := TTCPBlockSocket.Create;
tmpTcp.Bind('192.168.1.195', '50000');
tmpTcp.Connect('192.168.1.195', '50000');
This piece of code, below, Works
tmpTcp := TTCPBlockSocket.Create;
tmpTcp.Bind('192.168.1.1', '50000');
tmpTcp.Connect('192.168.1.195', '50000');
I saw the description of Bind method but didn't help at all Can you give some direction about this?
Tks in advance
I have 2 applications running on the same machine, one acting as a TCP server and other as a TCP client. In that machine, I have 2 network interface, so it's mandatory to tell which one, the TCP connection should use.
No, it is not mandatory to bind to one interface vs the other. You could, for instance, bind the server to both interfaces by binding it to the wildcard 0.0.0.0
, then the client can use either interface it wants to reach the server. Or, since the client is on the same machine as the server, you could bind the server to 127.0.0.1
instead, which will allow connections only from the local machine and not from the network at all.
my doubt is: When I put the machine IP itself, for instance, 192.168.1.195 this doesn't work
Yes, it does work, and it is the only way to bind a socket to a specific interface on Windows (there are other ways to bind to interfaces on *Nix platforms). You must Bind()
to the actual IP that is assigned to the interface. You cannot bind to an external IP, such as a router/gateway IP.
but if I put the gateway IP 192.168.1.1 it Works.
No, it does not work. You cannot Bind()
to an IP that does not belong to the local machine.
The code that doesn't work is
tmpTcp := TTCPBlockSocket.Create; tmpTcp.Bind('192.168.1.195', '50000'); tmpTcp.Connect('192.168.1.195', '50000');
That fails because you are Bind()
'ing to the same IP/Port that you are then Connect()
'ing to. DON'T DO THAT.
Unless required by protocol/network restrictions (neither of which apply in your case), a TCP client should NOT bind to a specific port, even if it binds to a specific interface. Let the client use an ephemeral port assigned by the OS instead, eg:
tmpTcp := TTCPBlockSocket.Create;
tmpTcp.Bind('192.168.1.195', '0'); // <-- note the '0'!
tmpTcp.Connect('192.168.1.195', '50000');
Per the documentation:
If port value is '0', system chooses itself and conects unused port in the range 1024 to 4096 (this depending by operating system!).
Or better, simply don't Bind()
the client at all, let the OS figure it out for you during Connect()
:
tmpTcp := TTCPBlockSocket.Create;
tmpTcp.Connect('192.168.1.195', '50000');
The OS will implicitly bind the client to an appropriate interface that can reach 192.168.1.195
.
This piece of code, below, Works
tmpTcp := TTCPBlockSocket.Create; tmpTcp.Bind('192.168.1.1', '50000'); tmpTcp.Connect('192.168.1.195', '50000');
Only because the Bind()
will actually fail, since the IP is invalid for the local machine, and then Connect()
will simply bind implicitly to an appropriate local interface as needed.