java demo获得获得本机的P
package socket;
import java.net.InetAddress;
public class ShowIP {undefined
public static void main(String[] args) {undefined
try{undefined
if(args.length>0){undefined
String hostName=args[0];
InetAddress[] addr = InetAddress.getAllByName(hostName); //得到该主机的所有地址
//打印输出到控制台
for(InetAddress address : addr){undefined
System.out.println(address.getHostAddress());
}
}else{undefined
System.out.println(InetAddress.getLocalHost().getHostAddress());
}
}catch(Exception e){undefined
e.printStackTrace();
}
}
//控制台输出192.168.234.103,是本机的IP地址
}
欢迎关注公众号:Java后端技术全栈