felipe felipe - 1 month ago 14
Java Question

Java: Port not available (on Windows 7)

Solved:
The problem was a "virus" or something similar that was hijacking the port.
OBS: The virus hijacked the por in a way the port didn't appear on netstat -an or TCPView of ports being used.

I used the AVG antivirus to remove the virus.
Another antivirus didn't work, just AVG.




I did this program to check if a port is available:

package com.test;

import java.io.IOException;
import java.net.DatagramSocket;
import java.net.ServerSocket;

public class Test {
public static void main(String[] args) {
System.out.println("Port available: " + available(62974));
}
public static boolean available(int port) {
ServerSocket ss = null;
try {
ss = new ServerSocket(port);
ss.setReuseAddress(true);
return true;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ss != null) {
try {
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

return false;
}
}


The response is false.

The exception thrown is:

java.net.BindException: Address already in use: JVM_Bind
at java.net.PlainSocketImpl.socketBind(Native Method)
at java.net.PlainSocketImpl.bind(PlainSocketImpl.java:383)
at java.net.ServerSocket.bind(ServerSocket.java:328)
at java.net.ServerSocket.<init>(ServerSocket.java:194)
at java.net.ServerSocket.<init>(ServerSocket.java:106)
at com.dgs.test.Test.available(Test.java:16)
at com.dgs.test.Test.main(Test.java:9)


I'm using this port: 62974

But I check on Console/prompt-dos with:

netstat -ano


The port is not on the list.

I'm not using any firewall, I deactivated it

Am I doing the right thing to see if the port is available?

Is this a "not usable" port?

Answer

You code should try to become a client rather than a server when testing an active TCP/UDP port either locally or remotely.

package com.test;

import java.io.IOException;
import java.net.Socket;

public class PortScan {
    public static void main(String[] args) {
        if (args.length > 0 ){
            System.out.println("Port active: " + available(args[0], args[1]));
        }
        else System.out.println("Usage: PortScan <ip> <port>");
    }

    public static boolean available(String ip, String port) {
        boolean status = false;
        Socket ss = null;
        try {
            ss = new Socket(ip, Integer.parseInt(port));
            status = true; //there is a listening port 
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (ss != null) {
                try {
                    ss.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return status;
    }
}

Take this current nestat -ano result:

  TCP    192.168.72.1:139       0.0.0.0:0              LISTENING       4
  TCP    192.168.184.1:139      0.0.0.0:0              LISTENING       4
  UDP    0.0.0.0:445            *:*                                    4

When there is already a UDP service is active but it is not listening at 127.0.0.1:445 or a TCP service is active and it is listening at 192.168.184.1:139, when you test PortScan 127.0.0.1 445 or PortScan 192.168.184.1 139, it will connect and say "Port active: true":

When there is no service at a given port (probably not active or blocked); let say "127.0.0.1:446", it will catch a "Connection refused" and say "Port active: false":

java.net.ConnectException: Connection refused: connect
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.PlainSocketImpl.doConnect(Unknown Source)
    at java.net.PlainSocketImpl.connectToAddress(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 com.test.PortScan.available(PortScan.java:18)
    at com.test.PortScan.main(PortScan.java:9)
Port active: false

However, you will need to check why it is refusing; is it really not active or is it being blocked by a firewall? It is a tricky problem that you need to find out.

After that, you can code to become a server and try to establish itself on a free/inactive port, like below:

        ServerSocket serverSocket = null;
        Socket socket = null;
        try {
            serverSocket = new ServerSocket(Integer.parseInt(port));
            serverSocket.setReceiveBufferSize(2048);
            serverSocket.setSoTimeout(0);
            serverSocket.setReuseAddress(true);
            socket = serverSocket.accept();
        } catch (NumberFormatException e1) {
            e1.printStackTrace();
        } catch (IOException e1) {
            e1.printStackTrace();
        }

        //do something here in a loop

        try {
            socket.close();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

Update:

Make sure the previous run (that involves the server code part) is closed properly in Eclipse when you are trying to connect again with another run. Check Debug view of Eclipse and close the offending previous run before testing. Otherwise, you will get a BindException when trying to establish a server code on the same port which is already active.

To check whether the server port has been established, use:

netstat -ano | find "62974"