Home > Java > javaTutorial > Java Example - Check if a port is in use

Java Example - Check if a port is in use

黄舟
Release: 2017-01-20 11:36:37
Original
1531 people have browsed it

The following example demonstrates how to detect whether the port has been used:

/*
 author by w3cschool.cc
 Main.java
 */import java.net.*;import java.io.*;public class Main {
   public static void main(String[] args) {
      Socket Skt;
      String host = "localhost";
      if (args.length > 0) {
         host = args[0];
      }
      for (int i = 0; i < 1024; i++) {
         try {
            System.out.println("查看 "+ i);
            Skt = new Socket(host, i);
            System.out.println("端口 " + i + " 已被使用");
         }
         catch (UnknownHostException e) {
            System.out.println("Exception occured"+ e);
            break;
         }
         catch (IOException e) {
         }
      }
   }}
Copy after login

The output result of the above code is:

……
查看 17
查看 18
查看 19
查看 20
查看 21
端口 21 
已被使用
查看 22
查看 23
查看 24
……
Copy after login

The above is the Java example - check whether the port has been used , for more related content, please pay attention to the PHP Chinese website (www.php.cn)!



Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template