0
votes

In java to establish TCP connection, We required to create socket. I use below constructor to create socket. public Socket(InetAddress address, int port,InetAddress localAddr,int localPort) throws IOException

According to java API, localAddress should be a valid InetAddress or null. When i provided 0.0.0.0 as local address it will work correctly to establish connection on local machine e.g. 27.0.0.1 and also on remote machine.

To connect on local machine, Socket use 127.0.0.1 and for remote machine, socket use P1P1 interface address as local address. I don't know how java manages to find appropriate IP address for connection. I look into java API but doesn't found any thing.

1
Thanks jigar, I think 0.0.0.0 means "The Unspecified Address" e.g. it will try on all interface to communicate. Correct me if i wrongharsh patel

1 Answers

0
votes

Java doesn't use anything. It passes what you supply directly to the operating system. I don't believe 0.0.0.0 is a valid local address for a connected socket, but if it is INADDR_ANY, which means 'use any local IO address'.

There are few occasions when you need to specify a local address or port when connecting a TCP socket. Just use the two-argument constructor.