0

Possible Duplicate:
Sockets: Discover port availability using Java

hi,

how can i check the particular system port is avliable or not by using java.

Thanks Murali

Community
  • 1
  • 1
krishna
  • 97
  • 2
  • 4
  • 6

2 Answers2

1

Bind to it, see if you get an exception, release it again.

Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347
0

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.

Prabhakaran
  • 169
  • 2
  • 12