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?
You code should try to become a client rather than a server when testing an active TCP/UDP port either locally or remotely.
Take this current nestat -ano result:
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 445orPortScan 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”:
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:
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
BindExceptionwhen trying to establish a server code on the same port which is already active.To check whether the server port has been established, use: