简体   繁体   中英

Java socket blocks on connection to a server

I am trying to connect to server using a Java socket. I am trying to connect from port 80 to 90

int port;
Socket clientsocket;
String hostname = "www.google.com";

for(port = 80;port<=90; port++){
  try{
    clientsocket = new Socket(hostname,port);
    System.out.println("Connection at port " + port + "\t" + clientsocket.isConnected());
    clientsocket.close();
  }
  catch(Exception e){
    System.out.println(e.getMessage());
  }
}

When I try to connect to any website like google.com or w3schools.com my program hangs on the socket() call for port numbers except 80. Since those websites are not serving on ports 81-90 it should raise exception but instead it gets blocked. For port 80 it works fine. When I try to connect to the apache server installed on my machine, it doesn't block for any port number and gives me Connection refused error which is the obvious behavior. So why is it happening. Thanks in advance.

When I try to connect to any website like google.com or w3schools.com my program hangs on the socket() call for port numbers except 80. Since those websites are not serving on ports 81-90 it should raise exception but instead it gets blocked.

This is almost certainly not Java's doing.

When you invoke the Socket(String, int) constructor, the JVM asks the OS to attempt to establish a connection to the IP address corresponding to the supplied name, using the supplied port number. Assuming that we're talking TCP/IP, the OS sends off a TCP 'SYN' message, and waits for a response:

  • If the response is a 'SYN-ACK', it proceeds to establish the connection as per the protocol; see http://en.wikipedia.org/wiki/Transmission_Control_Protocol#Connection_establishment .

  • If the response is an 'RST' (reset), the connect fails and this results in a Java "connection refused" exception. (This is typically what happens if the 'SYN' makes it to the remote server, only to discover that there is no application "listening" on the port you tried to connect on.)

  • If the response is an ICMP message of some kind (eg ICMP destination unreachable), this typically results in an immediate failure of the connection request, and a Java exception.

  • If there is no response, the OS tries again, and again, and again. Depending on the Java default connect timeout (or the explicit timeout), this process could continue for a long time.

So what is most likely happening is that something is filtering the 'SYN' messages on funky ports, and simply throwing them away. It could be the local firewall software on your PC, firewall software in your gateway, or your ISP's network, or software in the remote system you are attempting to talk to. Or this could be happening to the 'SYN-ACK' message coming back.

Either way, the blocking / timeout behavior is inherent to TCP/IP networking, and it is impossible to accurately diagnose at either the OS or Java levels. You simply need to adjust your expectations. (Or set a shorter connect timeout ...)

For this case, and any case:

         /**
         * Create end point and initialize connection with custom timeout for refused or server offline
         */
        try{
            InetSocketAddress endPoint = new InetSocketAddress(serverAddress, portNumber);
            Socket socket = new Socket();
            socket.connect(endPoint, 10000);
        } catch(Exception e){
            //... Handle connection error
        }

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