java网络编程中如何获得本机的ip地址 1.如何正确获得本机的IP地址.通过:InetAddress.getLocalHost();获得的是"xxxxxx/192.168.0.1"形式的,我怎样获得本机的IP地址形式:"192.168.0.1"?2.在具有公网IP的计算机如何与局域网内的计算机建立socket链接?请考虑有防火墙的情况. 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 public String[] getIpAddress(String hostname) throws SocketException,UnknownHostException { String[] get = null; InetAddress[] inetAddress = null; if (new CommonConvert().isEmpty(hostname)) {hostname = "localhost";} inetAddress = Inet4Address.getAllByName(hostname.toLowerCase()); if (inetAddress.length > 0 && inetAddress != null) { get = new String[inetAddress.length]; for (int i = 0;i<inetAddress.length;i++) { get[i] = inetAddress[i].getHostAddress(); } } return get; } 楼上class:CommonConvert从哪里引入 try { InetAddress[] ia = InetAddress.getAllByName(InetAddress.getLocalHost().getHostName()); for(int i=0;i<ia.length;i++){ System.out.print(ia[i].getHostAddress().toString()); } } catch (UnknownHostException ex) { // TODO 自动生成 catch 块 ex.printStackTrace(); } java中tcp能用多线程实现无阻塞通信吗 请问jscrollpanel的滚动条怎么随tab焦点移动而滚动? 菜鸟问题:请问这段代码有什么错误? 求助:编译抛出异常问题解决方法!!! 求js资料 求助!java文件处理 java中的SWING到底有用不?? 求助一个字符串处理问题 在接口声明一个抽象方法,为什么,请大家讨论一下 那位大哥做过JAVA-VRML-EAI 的工作的,那里可以下到有关的包和资料阿! 求一算法 兄弟们进来看看...愁死我了 JCREATOR项目问题
String[] get = null;
InetAddress[] inetAddress = null;
if (new CommonConvert().isEmpty(hostname)) {hostname = "localhost";}
inetAddress = Inet4Address.getAllByName(hostname.toLowerCase());
if (inetAddress.length > 0 && inetAddress != null) {
get = new String[inetAddress.length];
for (int i = 0;i<inetAddress.length;i++) {
get[i] = inetAddress[i].getHostAddress();
}
}
return get;
}
InetAddress[] ia = InetAddress.getAllByName(InetAddress.getLocalHost().getHostName());
for(int i=0;i<ia.length;i++){
System.out.print(ia[i].getHostAddress().toString());
}
} catch (UnknownHostException ex) {
// TODO 自动生成 catch 块
ex.printStackTrace();
}