📄 gethostname.java
字号:
import java.net.Socket;
import java.net.InetAddress;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.BufferedReader;
/**
* Created by IntelliJ IDEA.
* User: ${040421220 郑天南}
* Date: 2007-4-12
* Time: 20:27:41
*/
public class getHostName
{
public static void main(String[] args)
{
try
{
Socket s=new Socket("127.0.0.1",6629);
InetAddress address=s.getLocalAddress();
//getHostName
System.out.println(address.getHostName());
//getMACaddress
MACAddress mac=new MACAddress();
System.out.println(mac.getMACAddress());
}
catch(Exception ex)
{
}
}
}
class MACAddress {
public MACAddress() {
}
public String getMACAddress() {
String address = "";
String os = System.getProperty("os.name");
if (os != null && os.startsWith("Windows")) {
try {
String command = "cmd.exe /c ipconfig /all";
Process p = Runtime.getRuntime().exec(command);
BufferedReader br =
new BufferedReader(
new InputStreamReader(p.getInputStream()));
String line;
while ((line = br.readLine()) != null) {
if (line.indexOf("Physical Address") > 0) {
int index = line.indexOf(":");
index += 2;
address = line.substring(index);
break;
}
}
br.close();
return address.trim();
} catch (IOException e) {}
}
return address;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -