如何获得我的IP地址? [英] How to get my ip address?

查看:128
本文介绍了如何获得我的IP地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ServerSocket,我想知道的IP地址,但

I've a serverSocket and I would like to know the IP address, but with

listenSocket.getInetAddress().toString();

我得到的 0.0.0.0 的。 我怎样才能获得的IP地址,或者(如果有使两个连接)其中之一吗?

I get 0.0.0.0 . How can I get the IP address, or (if there are two connections enabled) one of them?

推荐答案

我已经在过去使用这样的:

I've used this in the past:

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();
                }
            }
        }
    } catch (SocketException ex) {
        Log.e(LOG_TAG, ex.toString());
    }
    return null;
}

来源: HTTP:// WWW。 droidnova.com/get-the-ip-address-of-your-device,304.html

这篇关于如何获得我的IP地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆