码迷,mamicode.com
首页 > 编程语言 > 详细

Creating InetAddress object in Java

时间:2014-10-21 03:34:56      阅读:201      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   os   ar   java   sp   div   

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)

 

Creating InetAddress object in Java

标签:style   blog   color   io   os   ar   java   sp   div   

原文地址:http://www.cnblogs.com/ghgyj/p/4039363.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!