简体   繁体   中英

Java exception while attempting socket connection

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)

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM