The difference between inetaddress and inetsocketaddress in Java

Source: Internet
Author: User
Tags get ip

InetAddress and inetsocketaddress look similar in Java to describe IP addresses and host names. Of course, they also support the use of regular methods to check the address: Loopback address, local address, multicast address, basic return method: Get IP, get host name, etc.

The important thing is that inetsocketaddress contains inetaddress. This means that if we want to do anything with the inetaddress in inetsocketaddress, we just need to get it through the Getinetaddress () method. Comparison Table

Property inetaddress inetsocketaddress
Description Object IP Address Socket address (IP address + port)
Describe IP and host object names The IP and host object names, including the port number
Solve the problem IP to host name, host name to IP IP to host name, host name to IP, can contain port
Get Object Inetaddress.getlocalhost ();
Inetaddress.getbyname (String);
Inetaddress.getbyaddress (String);
Inetsocketaddress.createunresolved (String, Port);
code Example
public class  Hotdream {public

    static void Main (String args[]) {

       byte[] b = new byte[] {(byte), (Byte), (b Yte) 1, (byte) 1};

        try {

            inetaddress add = inetaddress.getbyaddress (b);

            System.out.println (Add.tostring ());

            Add = Inetaddress.getbyname ("localhost");

            System.out.println (Add.tostring ());

            Inetsocketaddress Addsock = inetsocketaddress.createunresolved (

                    "localhost",);

            System.out.println (Addsock.tostring ());

        } catch (Exception e) {

        }

    }

}
Original Connection

https://commitflame.wordpress.com/2010/12/22/comparing-inetaddress-and-inetsocketaddress-in-java/

Related Article

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.