📄 catchip.java
字号:
//chp 9
/**
* 获取IP地址
*/
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class CatchIp {
private InetAddress LocalIP = null;
private InetAddress ServerIP = null;
public static void main(String args[]) {
CatchIp mytest;
mytest = new CatchIp();
System.out.println("LocalHost IP is: " + mytest.catchLocalIP());
System.out.println("Server IP is :" + mytest.catchServerIP());
}
//取得本机IP地址
public InetAddress catchLocalIP() {
try {
LocalIP = InetAddress.getLocalHost();
} catch (UnknownHostException e) {
}
return LocalIP;
}
public InetAddress catchServerIP() {
try {
ServerIP = InetAddress.getByName("www.sina.com.cn");
//取得 www.sina.com.cn 的IP地址
} catch (UnknownHostException e) {
System.out.println(e.getMessage());
}
return ServerIP;
}
}//end class CatchIp
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -