Android IP抓取

Android IP抓取,android,Android,我们正在做一个聊天应用程序,使用android 2.3.3作为一个项目,当我们打开该应用程序时,我们需要通过代码自动获取IP地址,并将其发送到服务器,并显示状态。如何做到这一点。我们将非常感谢您对本主题的任何帮助。 提前谢谢请看这里 别忘了加一行 <uses-permission android:name="android.permission.INTERNET" /> 到清单为什么要将IP发送到服务器?当客户端连接时,服务器可以获得IP吗 通常,客户机必须知道服务器的IP并连

我们正在做一个聊天应用程序,使用android 2.3.3作为一个项目,当我们打开该应用程序时,我们需要通过代码自动获取IP地址,并将其发送到服务器,并显示状态。如何做到这一点。我们将非常感谢您对本主题的任何帮助。 提前谢谢

请看这里

别忘了加一行

<uses-permission android:name="android.permission.INTERNET" />


到清单

为什么要将IP发送到服务器?当客户端连接时,服务器可以获得IP吗

通常,客户机必须知道服务器的IP并连接到它。在某些情况下,很难或不可能知道当它到达服务器时会得到什么样的IP(NAT防火墙、代理、多个网络设备,如3G、wifi等)

要列出Android设备上注册的所有IP地址,您可以执行以下操作(代码来源):

公共字符串getLocalIpAddress(){
试一试{
对于(枚举en=NetworkInterface.getNetworkInterfaces();en.hasMoreElements();){
NetworkInterface intf=en.nextElement();
对于(枚举Enumeration EnumipAddress=intf.getInetAddresses();EnumipAddress.hasMoreElements();){
InetAddress InetAddress=enumIpAddr.nextElement();
如果(!inetAddress.isLoopbackAddress()){
返回inetAddress.getHostAddress().toString();
}
}
}
}捕获(SocketException例外){
Log.e(Log_标记,例如toString());
}
返回null;
}
我建议您仔细阅读客户机/服务器体系结构。关于这个话题有几条指南

public String getLocalIpAddress() {
    try {
        for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
            NetworkInterface intf = en.nextElement();
            for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                InetAddress inetAddress = enumIpAddr.nextElement();
                if (!inetAddress.isLoopbackAddress()) {
                    return inetAddress.getHostAddress().toString();
                }
            }
        }
    } catch (SocketException ex) {
        Log.e(LOG_TAG, ex.toString());
    }
    return null;
}