Java method for determining the local IP Address type

Source: Internet
Author: User

Copy codeThe Code is as follows: package net;

Import java.net .*;

/*
* The getAddress method is similar to the getHostAddress method. The only difference between the getHostAddress method and getHostAddress method is that the getHostAddress method returns a string-type IP address,
* The getAddress method returns an IP address in byte array format.
* In Java, the value range of the byte type is-128? 127. If a byte of the returned IP address is an integer greater than 127, It is a negative number in the byte array.
* Because Java does not have an unsigned byte type, int or long type must be used to display normal IP addresses.
*/
Public class MyIp
{
Public static void main (String [] args) throws Exception
{
InetAddress ia = InetAddress. getByName ("www.cnblogs.com ");
Byte ip [] = ia. getAddress ();
/*
For (byte part: ip)
System. out. print (part + "");
System. out. println ("");
For (byte part: ip)
{
Int newIp = (part <0 )? 256 + part: part;
System. out. print (newIp + "");
}
*/
Int [] array = new int [5];
For (int I = 0; I <ip. length; I ++ ){
Array [I] = (ip [I] <0 )? 256 + ip [I]: ip [I];

}
String str = TellIpType (array [0]);
System. out. println (str );

}
/*
* Determine the IP address type based on the first byte
*/
Public static String TellIpType (int num ){
If (num & lt; 127)
Return "";
Else if (num & lt; 192)
Return "B ";
Else if (num & lt; 224)
Return "C ";
Else if (num & lt; 240)
Return "D ";
Else
Return "E ";
}
}

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.