NoRouteToHostException in Client

19,083

This could only happen, when the port is not accessible because of a firewall in between. This firewall could be anywhere even on the client or the server machine.
ping is to check if the host is accessible which it is but not on port 8001. From ping, it is confirmed that the host is accessible.

Try running the server program on another generally open port such as 80, 443, 8080 etc. The network / firewall administrators generally allow these ports to be open and accessible. Note that for port 80, 443 (any port less than 1024) would require root access to bind to it (on the server that is).

Share:
19,083
RagHaven
Author by

RagHaven

Generalist Software Engineer

Updated on June 04, 2022

Comments

  • RagHaven
    RagHaven almost 2 years

    I am working on a program that involves a server and a client and I am try to listen on a port and then send messages to that port from a client. However, on doing so I get NoRouteToHostException. I made a simple Client and Server application to test if I can send and receive messages for my given server and client.

    Server:

    public class Server{
     public static void main(String [] args) throws  Exception{
       ServerSocket s = new ServerSocket(8001);
       s.accept();
     }
    }
    

    Client:

    public class Client{
      public static void main(String [] args){
        Socket s = new Socket(IP, port);
        PrintWriter p = new PrintWriter(s.getOutputStream(), true);
        p.println("Hello World");
        s.close();
      }
    }
    
    Exception in thread "main" java.net.NoRouteToHostException: No route to host
            at java.net.PlainSocketImpl.socketConnect(Native Method)
            at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:345)
            at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
            at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
            at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
            at java.net.Socket.connect(Socket.java:589)
            at java.net.Socket.connect(Socket.java:538)
            at java.net.Socket.<init>(Socket.java:434)
            at java.net.Socket.<init>(Socket.java:211)
    

    I tried to ping the server and it went through as well. Also, if I run the client on the localhost I don't get an exception. I only get the exception when the client is running on a different system.

    EDIT When running the server on 8080, it works. I tried allowing all incoming connections to the server by doing iptables --policy INPUT ACCEPT, but that still doesn't allow me to listen on port 8001