java--Network Programming-Basic knowledge

Source: Internet
Author: User
Tags fully qualified domain name

Java provides java.net packages for network support, and classes such as URLs and URLConnection under the package provide programmatic access to Web services, while Urldecoder and Urlencoder provide ordinary strings and appliction/ X-www-form-urlencoded A static method that converts MIME strings to each other.

Use the InetAddress class (the implemented interface is the serializable serialization interface):

Java provides the InetAddress class to represent the IP address, and there are also two sub-classes under InetAddress: Inet4address, inet6address, which represent IPV4 and IPv6 addresses, respectively.

InetAddress does not provide a constructor, but instead provides the following two static methods to obtain the InetAddress instance.

1. Getbyname (String host): Gets the corresponding InetAddress object based on the host.

2, Getbyaddress (byte[] addr): According to the original IP address to obtain the corresponding InetAddress object.

static InetAddress[] getAllByName(String host)
In the case of a given hostname, an array of its IP addresses is returned based on the name service configured on the system.
static InetAddress getByAddress(byte[] addr)
Returns the object, given the original IP address InetAddress .
static InetAddress getByAddress(String host, byte[] addr)
Creates a 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.

InetAddress also provides the following three methods to obtain the IP address and host name of the InetAddress instance.

1. String getcanonicalhostname (): Gets the fully qualified domain name of this IP address.

2. String gethostaddress (): Returns the IP address string (as a string) corresponding to the InetAddress instance.

3. String gethostname (): Gets the host name of this IP address.

In addition, the InetAddress class provides a getlocalhost () method to obtain a inetaddress instance of the native IP address.

The InetAddress class also provides a isreachable () method that tests whether the address can be reached, and the method will do its best to try to reach the host.

1 Importjava.net.InetAddress;2  Public classInetaddressclass3 {4      Public Static voidMain (string[] arg)throwsexception{5         //to obtain the corresponding InetAddress instance based on the host name6InetAddress ip=inetaddress.getbyname ("www.baidu.com");7         //determine if you can reach8SYSTEM.OUT.PRINTLN ("Baidu can Reach:" +ip.isreachable (2000));9         //get the IP string for the inetaddress instanceTen System.out.println (Ip.gethostaddress ()); One         //Obtain the corresponding InetAddress instance based on the original IP address AInetAddress local=inetaddress.getbyaddress (New byte[]{127,0,0,1}); -         //determine if the host is reachable -System.out.println ("The native host can reach:" +local.isreachable (2000)); the         //gets the fully qualified domain name used by the InetAddress instance pair - System.out.println (Local.getcanonicalhostname ()); -     } -}

java--Network Programming-Basic knowledge

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.