1

I'm writing a simple test Java program, using Socket to test which port is available for TCP service on my local machine. Here is my code:

for (int i = 0; i < 1024; i++) {
    try {
        //test availability
        InetAddress addr = InetAddress.getLocalHost();
        Socket s = new Socket(addr, i);
        System.out.println("There is a server on port " + i + "of localhost.");
    } catch (IOException e){
        e.printStackTrace();
        break;
    }
}

and when running I got:

java.net.ConnectException: connect: Address is invalid on local machine, or port is not valid on remote machine
        at java.net.DualStackPlainSocketImpl.connect0(Native Method)
        at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
        at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
        at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
        at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
        at java.net.PlainSocketImpl.connect(Unknown Source)
        at java.net.SocksSocketImpl.connect(Unknown Source)
        at java.net.Socket.connect(Unknown Source)
        at java.net.Socket.connect(Unknown Source)
        at java.net.Socket.<init>(Unknown Source)
        at java.net.Socket.<init>(Unknown Source)
        at main.PortScanner.main(PortScanner.java:19)

I'm new to Socket. And do I need to start a Socket server to response for my quest? Or what should I really do to scan my ports? Thanks for your answer.

MagicFingr
  • 479
  • 6
  • 20
  • check this [link](http://stackoverflow.com/questions/15105357/address-is-invalid-on-local-machine-on-windows-8-only) – Yogesh Patil Apr 22 '14 at 09:18
  • Thanks @yogx, I tried to run it by: java -Djava.net.preferIPv4Stack=true main.PortScanner, as administrator also, but still get the same error messages. Which is, "ConnectException: connect: Address is invalid on local machine, or port is not valid on remote machine" for the first iteration and "ConnectException: Connection refused: connect" exception for the rest of the loop. – MagicFingr Apr 22 '14 at 11:09

2 Answers2

1

You are trying to establish a client connection to port 0 which is not possible. If you would bind the serverside socket to port 0, the system would auto-assign a free port above 1024. Thus a server socket would never listen to port 0. Unfortunately I could not find a documentation about this. It might be that other operating systems behave differently.

Your second question: If nothing is listening on a port then you will get a connect exception.

Moh-Aw
  • 2,976
  • 2
  • 30
  • 44
  • Thanks. I shouldn't tried to connect to port 0, I think that's how the first exception msg come. But when I'm trying to connect to port 1~1024, I still get: "java.net.ConnectException: Connection refused: connect". What should I do to scan my local preserved ports? – MagicFingr Apr 22 '14 at 12:48
  • take a look at the answer from Bruno Reis here: http://stackoverflow.com/questions/11547082/fastest-way-to-scan-ports-with-java – Moh-Aw Apr 22 '14 at 12:57
0

Do you run this program as root or Administrator ? If not, only these users can open a socket with a port between 0 and 1024.

Kartoch
  • 7,610
  • 9
  • 40
  • 68
  • Thanks. I tried to run it as admin. But still not working. I got a "ConnectException: connect: Address is invalid on local machine, or port is not valid on remote machine" for the first iteration and "ConnectException: Connection refused: connect" exception for the rest of the loop. BTW, I'm using windows x64, and I ran it in cmd.exe. – MagicFingr Apr 22 '14 at 09:11