1.Java获取本机ip地址
throws UnknownHostException //抛出异常
InetAddress host = InetAddress.getLocalHost();//
String ip =host.getHostAddress();//
2.使用Java执行ping命令
Process p = Runtime.getRuntime().exec("ping " + "192.168.2.106");
3.使用socket套接字建立链接
服务器端:
ServerSocket ss = new ServerSocet(port);
Socket s = ss.accept();
s.close();
ss.close();
客户端:
try{
Socket s = new Socket(ip,port);
s.cloce();
}catch (UnknownHostException e) {
e.printStackTrace();
}
网友评论