I found a few codes to use Java to obtain the native IP address on the Internet, but found that they were not perfect, so I sorted it out. Suddenly I wanted to get my IP address. Although I could use the system's own commands to get it, I wanted to write a program to get it. I searched online for Java to obtain the native IP address. But I found that none of them could be used. I was so angry that I vomited blood.
These people wrote the program with their eyes closed, and they posted it online after they finished writing, without testing it. This made me think that I had RP problems and I couldn’t get the correct IP address, so I strongly condemn it! ! !
In order to express contempt, I will point out the shortcomings of the two main methods found online.
Method 1 (can only be used on Windows, Linux platform will be gei nostalgic):
try
{
("Native IP = " + ());
} catch (UnknownHostException e)
{
();
}
The execution result under Linux is: the IP of the machine = xxx/127.0.1.1 (where xxx is your computer name, and I'm mosaicing here)
Method 2 (claims that it can be executed under Linux)
Enumeration netInterfaces=();
InetAddress ip = null;
while(())
{
NetworkInterface ni=(NetworkInterface)();
(());
ip=(InetAddress) ().nextElement();
if( !()
&& !()
&& ().indexOf(":")==-1)
{
("Native ip=" + ());
break;
}
else
{
ip=null;
}
}
From the red part code, we can see that the code does not loop to obtain the IP address of the first NetworkInterface obtained, but only processes the first IP address, which results in the fact that the first IP address is not the same
The address of Inet4Address is a < span
>Inet6Address, this judgment ().indexOf(":")==-1 will always be false. This if condition cannot be entered, it will be very harmful to people, and I strongly despise it!
However, the second method is correct, but there are some minor problems. I changed it. The final version of the code that can be correctly executed under Linux is as follows:
Enumeration allNetInterfaces = ();
InetAddress ip = null;
while (())
{
NetworkInterface netInterface = (NetworkInterface) ();
(());
Enumeration addresses = ();
while (())
{
ip = (InetAddress) ();
if (ip != null && ip instanceof Inet4Address)
{
("Native IP = " + ());
}
}
}
These people wrote the program with their eyes closed, and they posted it online after they finished writing, without testing it. This made me think that I had RP problems and I couldn’t get the correct IP address, so I strongly condemn it! ! !
In order to express contempt, I will point out the shortcomings of the two main methods found online.
Method 1 (can only be used on Windows, Linux platform will be gei nostalgic):
try
{
("Native IP = " + ());
} catch (UnknownHostException e)
{
();
}
The execution result under Linux is: the IP of the machine = xxx/127.0.1.1 (where xxx is your computer name, and I'm mosaicing here)
Method 2 (claims that it can be executed under Linux)
Enumeration netInterfaces=();
InetAddress ip = null;
while(())
{
NetworkInterface ni=(NetworkInterface)();
(());
ip=(InetAddress) ().nextElement();
if( !()
&& !()
&& ().indexOf(":")==-1)
{
("Native ip=" + ());
break;
}
else
{
ip=null;
}
}
From the red part code, we can see that the code does not loop to obtain the IP address of the first NetworkInterface obtained, but only processes the first IP address, which results in the fact that the first IP address is not the same
The address of Inet4Address is a < span
>Inet6Address, this judgment ().indexOf(":")==-1 will always be false. This if condition cannot be entered, it will be very harmful to people, and I strongly despise it!
However, the second method is correct, but there are some minor problems. I changed it. The final version of the code that can be correctly executed under Linux is as follows:
Enumeration allNetInterfaces = ();
InetAddress ip = null;
while (())
{
NetworkInterface netInterface = (NetworkInterface) ();
(());
Enumeration addresses = ();
while (())
{
ip = (InetAddress) ();
if (ip != null && ip instanceof Inet4Address)
{
("Native IP = " + ());
}
}
}