Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4325090
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T09:13:13+00:00 2026-05-21T09:13:13+00:00

I am trying to connect to server using a Java socket. I am trying

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T09:13:14+00:00Added an answer on May 21, 2026 at 9:13 am

    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 (e.g. 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 …)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to connect from one server to another using FTP. By using command
I'm trying to connect and fetching source from a SSH server with using PuTTY,
I am trying to connect to SQL Server 2008 server from Java here is
I'm trying to connect a Flash AIR application with a Java server using UDP
I am trying to connect to a server from my app using the following
I am trying to connect the Sql Server 2008 R2 with java using JDBC.I
I am trying to tcp connect to a server machine in java, by using
I am trying to connect to a secure server using Apache Commons HttpClient 3.1
I'm trying to connect to a AS400 server using the .net classes. I have
I'm trying to connect to SQL Server on Ubuntu 9.04 using Ruby. I translated

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.