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

  • SEARCH
  • Home
  • 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 8011467
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T19:01:23+00:00 2026-06-04T19:01:23+00:00

I Im writing a port scanner in java as an educational exercise and I

  • 0

I Im writing a port scanner in java as an educational exercise and I have run into a problem.
My program works fine when im scanning the ports on my local machine, however when i try to scan for ports on foriegn computers the process just freezes. Here is the code for my scan method:

public void scan(InetAddress ad, int start, int end){
    for(int i=start; i<=end; i++){
        try{
            socket = new Socket(ad, i);
            System.out.println("Port "+i+"is open");
        }catch(Exception e){
            System.out.println("Port "+i+"is not open");
        }
    } 
}

So far it is working when the host name is my loopback address or just “localhost”, it even works with my IP Address. But when I for instance resolve the IP for http://www.google.com or even give it the IP address of my other computer on the same network, the program will freeze after scanning the first port.

I am resolving the IP address like this:

inetAddressObject = InetAddress.getByName(hostNameString);

Can anyone tell me if I am missing a big difference between scanning ports on a local machine and scanning ports on a foreign host? Or is there just something simple going wrong with my code ?

  • 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-06-04T19:01:24+00:00Added an answer on June 4, 2026 at 7:01 pm

    The way you are calling the Socket constructor the connection will be made right away. The problem is that if the connection times out then you will have to wait a while (OS default). The way you should do it is the following:

    Socket s = new Socket();
    s.connect(new InetSocketAddress(ad, i), timeout);
    

    The separate reason is why it is timing out. This is because your machine is likely behind a firewall of some sorts and is unrelated to your code example.

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

Sidebar

Related Questions

I am writing a socket program with Java and have a host that clients
I am writing a port scanner in Java. I am using a for loop
I have tried writing primitive activity for scanning USB port and displaying basic info
I'm writing a program in C that binds to a port < 1024. I'd
I'm writing a program to check to see if a port is open in
I'm writing a port scanner to detect web services running on the local network.
I am writing a program to send and receive data via a serial port
I am writing a custom p2p program that runs on port 4900. In some
I have a simple HTML server program that listens on port 8000 for a
i m writing a code for port scanner so i need to send a

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.