Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java exception while attempting socket connection

Tags:

java

sockets

I am trying to make a socket connection to an ip:port and it all works correctly on one of my computers. The client first connects to the server with a socket connection to log in; after that it expects incoming connections from the server and they are successful.

But, when I tried it on another computer, the client made the first connection to log in successfuly, and then waited for incoming connections. On the server side where the connections would be initiated I got this message:

java.net.NoRouteToHostException

and the incoming connection failed.

Why would that happen? Could it be a firewall issue?

s = new Socket(id, 4446);
BufferedWriter out1 = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
BufferedReader in1 = new BufferedReader(new InputStreamReader(s.getInputStream()));

thats the 46-th line

java.net.NoRouteToHostException: No route to host: connect
        at java.net.PlainSocketImpl.socketConnect(Native Method)
        at java.net.PlainSocketImpl.doConnect(Unknown Source)
        at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
        at java.net.PlainSocketImpl.connect(Unknown Source)
        at java.net.SocksSocketImpl.connect(Unknown Source)
        at java.net.Socket.connect(Unknown Source)
        at java.net.Socket.connect(Unknown Source)
        at java.net.Socket.<init>(Unknown Source)
        at java.net.Socket.<init>(Unknown Source)
        at sample_server.doComms.run(doComms.java:46)
        at java.lang.Thread.run(Unknown Source)
like image 403
Evan Avatar asked Mar 16 '26 00:03

Evan


2 Answers

As stated in the API:

Signals that an error occurred while attempting to connect a socket to a remote address and port. Typically, the remote host cannot be reached because of an intervening firewall, or if an intermediate router is down.

You most likely have a firewall or something blocking the connection. Try disabling one if you have one real quick, and see how that works. If it doesn't work, it's something else (the full stacktrace may help as comments on your question say).

like image 89
Alex Coleman Avatar answered Mar 18 '26 13:03

Alex Coleman


'No route to host' means that your host doesn't even know how to contact the target. It's a network connectivity issue. Not a firewall issue.

like image 42
user207421 Avatar answered Mar 18 '26 12:03

user207421