开发者

particular system port avliable checking by using java [duplicate]

开发者 https://www.devze.com 2023-01-23 17:36 出处:网络
This question already has answers here: Closed 12 years ago. Possible Duplicate: Sockets: Discove开发者_开发百科r port availability using Java
This question already has answers here: Closed 12 years ago.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消