gpt4 book ai didi

java - 在 Java 中创建 InetAddress 对象

转载 作者:IT老高 更新时间:2023-10-28 13:51:36 25 4
gpt4 key购买 nike

我正在尝试将由 IP 号或名称指定的地址转换为字符串(即 localhost127.0.0.1) InetAdress 对象。没有构造函数,而是返回 InetAddress 的静态方法。因此,如果我得到一个主机名,这不是问题,但如果我得到 IP 号怎么办?有一种方法可以获得 byte[] 但我不确定这对我有什么帮助。所有其他方法都获取主机名。

InetAddress API documentation

最佳答案

您应该能够使用 getByNamegetByAddress

The host name can either be a machine name, such as "java.sun.com", or a textual representation of its IP address

InetAddress addr = InetAddress.getByName("127.0.0.1");

采用字节数组的方法可以这样使用:

byte[] ipAddr = new byte[]{127, 0, 0, 1};
InetAddress addr = InetAddress.getByAddress(ipAddr);

关于java - 在 Java 中创建 InetAddress 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5719449/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com