`
cary
  • 浏览: 84280 次
  • 性别: Icon_minigender_1
  • 来自: 合肥
社区版块
存档分类
最新评论

JAVA 中IP地址的获得

    博客分类:
  • J2EE
阅读更多
import java.net.*;

public class NetTool {

InetAddress myIPaddress = null;

InetAddress myServer = null;

public static void main(String args[]) {

NetTool mytool;
mytool = new NetTool();

System.out.println("Your host IP is: " + mytool.getMyIP());
System.out.println("The Server IP is :" + mytool.getServerIP("www.dsrcom.com"));

}

// 取得LOCALHOST的IP地址
public InetAddress getMyIP() {
try {
myIPaddress = InetAddress.getLocalHost();
} catch (UnknownHostException e) {
}
return (myIPaddress);
}

// 取得IP地址(指定)
public InetAddress getServerIP(String address) {
try {
myServer = InetAddress.getByName(address);
} catch (UnknownHostException e) {
}
return (myServer);
}

}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics