Java instance checks if port is occupied

Source: Internet
Author: User

127.0.0. 1 represents the main principle of the machine is: socket socket = new socket (address,port); #address代表主机的IP地址, Port for port number if a socket is established for a specific port number for the host, the port of that host is in use. New Socket  (address,port); #address代表主机的IP地址, port represents the port number if a socket is created for a specific port number for the host,   Indicates that the port of the host is in use.
Importjava.io.IOException; Importjava.net.InetAddress; ImportJava.net.Socket; Importjava.net.UnknownHostException;  Public classMain {//Start--end is the range of ports to be detected    Static intStart=0; Static intend=1024;  Public Static voidMain (String args[]) { for(inti=start;i<=end;i++) {System.out.println ("View" +i); if(islocalportusing (i)) {System.out.println ("Port" +i+ "is already in use"); }        }    }    /*** Test If the native port is used *@paramPort *@return     */     Public Static BooleanIslocalportusing (intPort) {          BooleanFlag =true; Try {            //returns True if the port is still in use, otherwise returning false,127.0.0.1 represents the nativeFlag = isportusing ("127.0.0.1", Port); } Catch(Exception e) {}returnFlag; }      /*** * Test the port port of hosts host for use *@paramHost *@paramPort *@throwsunknownhostexception*/      Public Static BooleanIsportusing (String host,intPortthrowsunknownhostexception{BooleanFlag =false; InetAddress Address=Inetaddress.getbyname (host); Try{Socket Socket=NewSocket (Address,port);//Create a socket connectionFlag =true; } Catch(IOException e) {}returnFlag; }  }

Java instance checks if port is occupied

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.