您的位置:首页 > 移动开发 > Objective-C

Creating InetAddress object in Java

2014-10-21 03:13 603 查看
I am trying to convert an address specified by an IP number or a name, both in String (i.e.
localhost
or
127.0.0.1
), into an InetAdress object. There's no constructor but rather static methods that return an InetAddress. So if I get a host name it's not a problem, but what if I get the IP number? There's one method that getsbyte[] but I'm not sure how that can help me. All other methods gets the host name.

You should be able to use getByName or getByAddress.

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");


The method that takes a byte array can be used like this:

byte[] ipAddr = new byte[]{127, 0, 0, 1};
InetAddress addr = InetAddress.getByAddress(ipAddr)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: