Client IP addresses are often used when interacting with servers. Of course, on the server side, you can get the requested ip address. on the mobile phone side, how can you get your own ip address? See the following functions:
[Java] public static String GetHostIp (){
Try {
For (Enumeration <NetworkInterface> en = NetworkInterface
. GetNetworkInterfaces (); en. hasMoreElements ();){
NetworkInterface intf = en. nextElement ();
For (Enumeration <InetAddress> ipAddr = intf. getInetAddresses (); ipAddr
. HasMoreElements ();){
InetAddress inetAddress = ipAddr. nextElement ();
If (! InetAddress. isLoopbackAddress ()){
Return inetAddress. getHostAddress ();
}
}
}
} Catch (SocketException ex ){
} Catch (Exception e ){
}
Return null;
}
Public static String GetHostIp (){
Try {
For (Enumeration <NetworkInterface> en = NetworkInterface
. GetNetworkInterfaces (); en. hasMoreElements ();){
NetworkInterface intf = en. nextElement ();
For (Enumeration <InetAddress> ipAddr = intf. getInetAddresses (); ipAddr
. HasMoreElements ();){
InetAddress inetAddress = ipAddr. nextElement ();
If (! InetAddress. isLoopbackAddress ()){
Return inetAddress. getHostAddress ();
}
}
}
} Catch (SocketException ex ){
} Catch (Exception e ){
}
Return null;
}
In fact, the above method obtains the ip address through the related classes in java.net. The main classes used are java.net. NetworkInterface and java.net. InetAddress.
From the pure soul