According to China's national conditions, broadband sharing is seriously affected by DNS pollution and HTTP interception, causing instability in network requests. However, the IP/TCP protocol is generally not affected. Therefore, you can resolve the domain name into IP first and save it, and use IP to access it in the future. The client starts and resolves the domain name into IP. If it fails, test whether the previous IP is available and identify the authenticity of the IP (same as below). If the domain name is successfully resolved, the encrypted information is sent to the server and the decrypted content is returned to test the authenticity of the IP (to ensure that it is not 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 first, and then use your own WiFi hotspot. The above method is only effective for incomplete network disconnection after broadband detection.
Copy the 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 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());
}
}