Java Fundamentals-Network programming

Source: Internet
Author: User
Tags get ip

Feature: IP address port number Transfer Protocol
 Packagecom. Train;Importjava.net.InetAddress;Importjava.net.InetSocketAddress;Importjava.net.UnknownHostException; Public classNetTest { Public Static voidMain (string[] args)throwsunknownhostexception{/**the =========== IP address is encapsulated as a InetAddress object ============*/                //This class represents an Internet Protocol (IP) address//obtain an IP object by using the static method of the InetAddress class:        /**get loopback ip inetaddress ialoopback = inetaddress.getloopbackaddress ();         * Get the native IP inetaddress ialocalhost = inetaddress.getlocalhost ();          * Get IP * static inetaddress getlocalhost () based on hostname to return local host.         * Static inetaddress[] Getallbyname (String host) returns an array of its IP addresses, based on the name service configured on the system, given the host name.          * Static inetaddress getbyaddress (byte[] addr) Returns the InetAddress object given the original IP address.          * Static inetaddress getbyaddress (String host, byte[] addr) creates inetaddress based on the host name and IP address provided.          * Static inetaddress Getbyname (String host) determines the IP address of the host in the given host name. * * Obtained InetAddress class object, which encapsulates a non-static method to obtain the hostname and IP address **/inetaddress Ialocalhost=Inetaddress.getlocalhost ();        System.out.println (Ialocalhost.tostring ());        System.out.println (Ialocalhost.gethostname ());        System.out.println (Ialocalhost.gethostaddress ()); System.out.println ("------------------"); InetAddress ia= Inetaddress.getbyname ("www.baidu.com");        System.out.println (Ia.gethostname ());        System.out.println (Ia.gethostaddress ()); System.out.println ("------------------"); //TCP: Connection-oriented-reliable-three-time handshake        /*three-time handshake establish connection: Is it?                In!        I know you're here! *///UDP: For non-connected---unreliable, fast}}

Java Fundamentals-Network programming

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.