I am trying to connect to a 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 site, for example google.com or w3schools.com, my program freezes when I call socket()for port numbers other than 80. Since these websites do not work on ports 81-90, it should throw an exception, but instead it is blocked. For port 80, it works fine. When I try to connect to the apache server installed on my machine, it does not block any port numbers and gives me a rejection of Connection failure, which is an obvious behavior. So why is this happening. Thanks in advance.