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