The Java implementation resolves the domain name into an IP example

  • 2020-04-01 03:19:42
  • OfStack

According to China's national conditions, broadband sharing is seriously affected by DNS pollution and HTTP interception, resulting in unstable network requests. However, the IP/TCP protocol is generally not affected. Therefore, you can first resolve the domain name to IP and save, after the use of IP access. The client launches, resolves the domain name to IP, and if that fails, tests whether the previous IP is available and verifies the authenticity of the IP (the same as below). If the domain name is resolved successfully, send the encrypted message to the server to return the decrypted content to test the authenticity of the IP (ensure that it is not contaminated by DNS). If possible, avoid using HTTP and use custom protocols. For the mobile client, you can even use the mobile network to resolve the domain name, and then use the WiFi hotspot. The above method is only effective for incomplete disconnection after broadband detection.


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 the IP address of LOCALHOST
 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());
 }
}


Related articles: