如何在android中创建InetAddress对象?

发布于 2024-10-25 06:21:27 字数 318 浏览 1 评论 0原文

你好!

我正在编写将在 android 中运行的代码。我想获取我的电脑的IP地址,即连接到同一网络的电脑。即我的手机通过 wifi 连接,电脑通过以太网电缆连接到同一路由器。我可以通过手机 ping 我的电脑,反之亦然,但我无法通过代码获取电脑的 IP 地址或主机名。

我正在使用这个

InetAddress inet = InetAddress.getByName( "192.168.0.102");

我收到网络无法访问错误。

请帮助我,因为我已经陷入其中很长时间了。 感谢并问候

法斯

HI!

I am writing code that will run in android. I want to get the IP address of my pc i.e connected to the same network. i.e My Phone is connected via wifi and the pc is connected via ethernet cable to the same router. I am able to ping my pc from my phone and vice versa but I am not able to get the ip address or hostname of my pc via code.

I am using this

InetAddress inet = InetAddress.getByName( "192.168.0.102");

I get network unreachable error.

Kindly help as I am stuck in it for very long.
Thanks and regards

Fas

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

箹锭⒈辈孓 2024-11-01 06:21:27

您可以尝试将字符串 IP 转换为整数,然后从包含 IP 地址的字节构造 InetAddress 对象。这是代码

InetAddress inet = intToInetAddress(ipStringToInt( "192.168.0.102"));

public static int ipStringToInt(String str) {
     int result = 0;
     String[] array = str.split("\\.");
     if (array.length != 4) return 0;
     try {
         result = Integer.parseInt(array[3]);
         result = (result << 8) + Integer.parseInt(array[2]);
         result = (result << 8) + Integer.parseInt(array[1]);
         result = (result << 8) + Integer.parseInt(array[0]);
     } catch (NumberFormatException e) {
         return 0;
     }
     return result;
 }

public static InetAddress intToInetAddress(int hostAddress) {
    InetAddress inetAddress;
    byte[] addressBytes = { (byte)(0xff & hostAddress),
                            (byte)(0xff & (hostAddress >> 8)),
                            (byte)(0xff & (hostAddress >> 16)),
                            (byte)(0xff & (hostAddress >> 24)) };

    try {
       inetAddress = InetAddress.getByAddress(addressBytes);
    } catch(UnknownHostException e) {
       return null;
    }
    return inetAddress;
}

You can try converting string IP into integer and then construct InetAddress object from bytes containing IP address. Here's the code

InetAddress inet = intToInetAddress(ipStringToInt( "192.168.0.102"));

public static int ipStringToInt(String str) {
     int result = 0;
     String[] array = str.split("\\.");
     if (array.length != 4) return 0;
     try {
         result = Integer.parseInt(array[3]);
         result = (result << 8) + Integer.parseInt(array[2]);
         result = (result << 8) + Integer.parseInt(array[1]);
         result = (result << 8) + Integer.parseInt(array[0]);
     } catch (NumberFormatException e) {
         return 0;
     }
     return result;
 }

public static InetAddress intToInetAddress(int hostAddress) {
    InetAddress inetAddress;
    byte[] addressBytes = { (byte)(0xff & hostAddress),
                            (byte)(0xff & (hostAddress >> 8)),
                            (byte)(0xff & (hostAddress >> 16)),
                            (byte)(0xff & (hostAddress >> 24)) };

    try {
       inetAddress = InetAddress.getByAddress(addressBytes);
    } catch(UnknownHostException e) {
       return null;
    }
    return inetAddress;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文