📄 ip_dn.java
字号:
package net;
import java.awt.HeadlessException;
import java.net.*;
import javax.swing.*;
public class IP_DN {
public static void main(String[] args) {
String strIP = JOptionPane.showInputDialog(null, "输入IP地址:", "网络",
JOptionPane.INFORMATION_MESSAGE);
try {
if (strIP.equals(null))
return;
else {
String strIPAddress = strIP;
byte[] ipAddress = new byte[4];
// 将IP地址字符串转换成相应的字节数组
for (int i = 0; i < 3; i++) {
ipAddress[i] = Byte.parseByte(strIP.substring(0, strIP
.indexOf('.')));
strIP = strIP.substring(strIP.indexOf('.') + 1);
}
ipAddress[3] = Byte.parseByte(strIP);
InetAddress host = InetAddress.getByAddress(ipAddress);
String hostName = host.getHostName();
JOptionPane.showMessageDialog(null, "主机名:" + hostName + "\n"
+ "IP地址:" + strIPAddress, "IP-->DN",
JOptionPane.INFORMATION_MESSAGE);
}
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (HeadlessException e) {
e.printStackTrace();
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -