java获取本机IP,系统随机分配端口号,获取当前线程ID

ServerSocket serverSocket = null; //读取空闲的可用端口
String localIp = null;
try {
    serverSocket = new ServerSocket(0);
    localIp = serverSocket.getInetAddress().getLocalHost().getHostAddress();
} catch (IOException e) {
    e.printStackTrace();
}
int port = serverSocket.getLocalPort();
System.out.println("系统分配的端口号 port="+port);
System.out.println("当前本机ip:"+localIp);
//获取当前进程ID
String processName =
        java.lang.management.ManagementFactory.getRuntimeMXBean().getName();
System.out.println("当前进程ID:"+Long.parseLong(processName.split("@")[0]));

你可能感兴趣的:(java获取本机IP,系统随机分配端口号,获取当前线程ID)