3

I am creating socket using socket = new Socket(host, port, InetAddress.getLocalHost(), clientPort);. I want the socket to listen to particular port at client side. But when I use InetAddress.getLocalHost() I get java.net.ConnectException: connect: Address is invalid on local machine, or port is not valid on remote machine.

But when I use InetAddress.getByName("localhost") it works fine. But I require IP address of the machine in server side. So when I use socket.getInetAddress() I want ipadress and not 127.0.0.1.

Can anyone please help. I am using eclipse. Can this be a firewall issue?

Thanks

user207421
  • 305,947
  • 44
  • 307
  • 483
Android_enthusiast
  • 863
  • 5
  • 22
  • 41
  • I do not really understand the issue. Maybe you can post the code? Btw: It's not the socket that listens, it's the `ServerSocket`, the `Socket` connects to a listening server. – home Nov 20 '11 at 08:01
  • sorry for being unclear. I am creating socket using Socket(String host, int port, InetAddress localAddr, int localPort) and this throws me exception if InetAddress is gt using InetAddress.getLocalHost() – Android_enthusiast Nov 20 '11 at 08:08
  • 1
    Why on earth are you trying to specify the client-side port for a client socket? – Donal Fellows Nov 20 '11 at 08:51
  • And why on earth are you specifying the local address? – user207421 Nov 21 '11 at 00:06
  • @EJP specifying the local address is the only way to restrict what network _interface_ the connection is made from. This has legitimate (though uncommon) use cases. – davmac Jun 02 '16 at 14:44
  • @Android_enthusiast you really need to post a more complete sample of what you are doing, if not a full [MCVE]. – davmac Jun 02 '16 at 14:46

2 Answers2

1

You're using the four-argument form of the Socket constructor (really unusual; it's normal to only use the two argument form and let the OS figure out the local address side for itself) so you need to make sure that the two addresses associated with the socket are compatible, i.e., that it is possible to route packets that way. In particular, if either end is localhost, the other end must be too because that address is only ever routed over the loopback network interface.

The simplest fix for you (on the client side) is going to be to switch to using the two-argument constructor, leaving the OS to figure out the rest of it for you; it does a good job. (If the server depends on the client connection coming from a specific port number, that's awful and terribly terribly fragile.)

Donal Fellows
  • 133,037
  • 18
  • 149
  • 215
0

Sounds like confusion over client-side and server-side responsibilities - sounds like you're trying to get two Java applications talking to each other on the same host via TCP/IP, using Java Sockets.

If this is the case you first need to use a ServerSocket in the 'server' application to create a listening socket on all interfaces:

serverSocket = new ServerSocket(4444);

You should then be able to connect to the ServerSocket from the 'client' application by using a Socket to connect to the localhost:

clientSocket = new Socket("localhost", 4444);

The following page(s) looks like they cover this in detail:

All About Sockets

Hope that helps.

Duncan
  • 763
  • 1
  • 6
  • 9
  • I have no confusion. Instead of creating clientSocket = new Socket("localhost", 4444); I am using Socket(String host, int port, InetAddress localAddr, int localPort) – Android_enthusiast Nov 20 '11 at 08:44
  • @Android_enthusiast the question is *why* you are doing that, which you've been asked basically three times and haven't answered. The suspicion is that you are confused into thinking you need to do that when you don't. – user207421 Nov 21 '11 at 09:13