Creating InetAddress object in Java

Source: Internet
Author: User

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 I Netadress object. There's no constructor but rather static methods this return an inetaddress. So if I get a host name it's not a problem and what if I get the IP number? There ' s one method that getsbyte[] but I'm not sure how can help me. All other methods gets the host name.

You should is able to use getbyname or getbyaddress.

The host name can either is 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 is used like this:

byte New byte []{127, 0, 0, 1= inetaddress.getbyaddress (ipaddr)

Creating InetAddress object in Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.