Possible Duplicate:
Sockets: Discove开发者_开发百科r port availability using Java
hi,
how can i check the particular system port is avliable or not by using java.
Thanks Murali
Bind to it, see if you get an exception, release it again.
Use this code snippet:
int minPortNumber = Integer.parseInt(args[0]); int maxPortRange = Integer.parseInt(args[1]);
for (int i = minPortNumber; i <= maxPortRange; i++) {
try {
Socket ServerSok = new Socket("127.0.0.1", i);
System.out.println("Port number in use: " + i);
ServerSok.close();
} catch (Exception e) {
System.out.println("Port number not in use: " + i);
}
}
if the port number is use the socket connection will be opened else it will throw exception.
精彩评论