programming-examples/java/Numerical_Problems/java program to check whether a port is being used or not.java
2019-11-15 12:59:38 +01:00

24 lines
665 B
Java

import java.net.*;
import java.io.*;
public class Main {
public static void main(String[] args) {
Socket Skt;
String host = "localhost";
if (args.length gt;0) {
host = args[0];
}
for (int i = 0; i < 1024; i++) {
try {
System.out.println("Looking for "+ i);
Skt = new Socket(host, i);
System.out.println("There is a server on port "+ i + " of " + host);
}
catch (UnknownHostException e) {
System.out.println("Exception occured"+ e);
break;
}
catch (IOException e) {
}
}
}
}