如果有多块网卡,如何获得某特定网卡的IP地址?最不希望得到的结果是127.0.0.1 呵呵……怎样才能得到有意义的IP地址呢?也就是连接入Internet或者局域网中的时候被别人所识别的IP地址。ServerSocket server = new ServerSocket(port);
System.out.println(server.getLocalAddress());这样得到的竟然是0.0.0.0
System.out.println(server.getLocalAddress());这样得到的竟然是0.0.0.0
解决方案 »
- 问个基本的问题,如何使用for循环编写java程序,打印出菱形
- java源码阅读求指导,有这发面经验的同仁给点建议哈!
- JSpinner如何设置文本框为不可编辑
- 求java.lang.OutOfMemoryError: Java heap space解决方案?
- 如何实现多个相同选项的JComboBox ???
- 键盘事件问题! 急
- java泛型 问题
- 小弟kangta:一个调查多长时间考过scjp的问题!
- 我的Sin();Cos()函数为什么不能用啊?已经import java.lang.*;还不行吗?
- 快来强分?
- java.lang.IllegalThreadStateException这个异常是如何产生的,怎样避免
- 怎么样才能使鼠标右键弹出的PopupMenu只发生在JTabbedPane头部?给点思路。
System.out.println(address.getHostAddress());用上面的语句另外再运行的时候,你可能,还需要加入一个用-D 指定的策略文件java xxx -Djava.security.policy=xxxxxx
InetAddress a = InetAddress.getLocalHost();
System.out.println (a);
}catch(Exception e){
}
192.168.0.88嗬嗬,函数起作用了,不过我想知道的是我上Internet的IP地址,例如218.25.147.154
如何才能得到呢?目前是通过PPPoE协议使用ADSL上网。另外,如果是安装了2块以上网卡也会出现多个IP地址的问题,如何找到自己需要的呢?
InetAddress localHostAddress =InetAddress.getLocalHost();
System.out.println(localHostAddress);
}
catch(Exception e){}
{
public static void main(String[] args)
{
try
{
InetAddress address=InetAddress.getLocalHost();
System.out.println(address);
}
catch(UnknownHostException e)
{
System.out.println("Could not find this computer's address.");
}
}
}
try
{
InetAddress[] addresses=InetAddress.getLocalhost();
for(int i=0;i<addresses.length;i++)
{
System.out.println(addresses[i]);
}
}
catch(UnknownHostException e)
{
System.out.println("Could not find www.microsoft.com");
}
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());
}
public InetAddress getMyIP(){
try{
myIPaddress=InetAddress.getLocalHost();
}catch(UnknownHostException e){}
return (myIPaddress);
}
public InetAddress getServerIP(){
try{
myServer=InetAddress.getByName("www.abc.com");
}catch(UnknownHostException e){}
return (myServer);
}
}