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

6
  • 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. Commented Nov 20, 2011 at 8: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() Commented Nov 20, 2011 at 8:08
  • 1
    Why on earth are you trying to specify the client-side port for a client socket? Commented Nov 20, 2011 at 8:51
  • And why on earth are you specifying the local address? Commented Nov 21, 2011 at 0: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. Commented Jun 2, 2016 at 14:44

2 Answers 2

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.)

Sign up to request clarification or add additional context in comments.

Comments

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.

2 Comments

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 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.