Java implementation resolves domain name to IP example _java

Source: Internet
Author: User

According to China's national conditions, broadband sharing suffers from DNS pollution and HTTP interception is very serious, resulting in instability of network requests. But the IP/TCP agreement is generally unaffected. Therefore, the domain name can be resolved into IP and save, and then use IP access. The client initiates, resolves the domain name to the IP, if fails, tests the previous IP is usable and the Authentication IP authenticity (the same). If the domain name is resolved successfully, send encrypted information to the server to return the decrypted content to test the authenticity of the IP (guaranteed not to be contaminated by DNS). If possible, avoid using the HTTP protocol and use a custom protocol. For mobile clients, you can even use the mobile network to resolve the domain name, and then using the WiFi hotspot from the set up. The above method is only effective for the broadband detection after incomplete network disconnection.

Copy Code code as follows:

Import java.net.InetAddress;
Import java.net.UnknownHostException;

public class Parsedomainname {
InetAddress myServer = null;
InetAddress myipaddress = null;
String domainname = null;

Public Parsedomainname (String DomainName) {
This.domainname = domainname;
}

Public inetaddress Getserverip () {
try {
MyServer = inetaddress.getbyname (domainname);
catch (Unknownhostexception e) {
}
return (MyServer);
}

Get localhost IP address
Public inetaddress Getmyip () {
try {
myIpAddress = Inetaddress.getlocalhost ();
catch (Unknownhostexception e) {
}
return (myipaddress);
}

public static void Main (string[] args) {
Parsedomainname PDN = new Parsedomainname ("www.baidu.com");
System.out.println ("Your host IP is:" + Pdn.getmyip (). gethostaddress ());
System.out.println ("The Server IP is:" + Pdn.getserverip (). gethostaddress ());
}

}

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.