同志们,如果本机有多个IP,有IPv4的、有IPv6的、有的能访问外网服务、有的只能访问局域网,这种怎么解决的? 
主要是 InetAddress.getLocalHost()   拿到IP经常是错的
我们现在是通过配置文件

解决方案 »

  1.   

    上网 不是用IPv4 就行么?   不能固定ip么?
      

  2.   

    获得所有ip 然后 挨个 试试行不?
    new serversocket().accept()返回的是一个socket对象,也就是当前连接的信息,这里面包含有一个 getRemoteAddress方法
      

  3.   


    public static ArrayList<String> getAddress(){
    ArrayList<String> addresses = new ArrayList<String>();  String address = "";  String os = System.getProperty("os.name");  if (os != null && os.startsWith("Windows")) {  try {  String command = "cmd.exe /c ipconfig /all";  Process p = Runtime.getRuntime().exec(command);  BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));  String line;  while ((line = br.readLine()) != null) {  if (line.indexOf("IP Address") > 0) {  int index = line.indexOf(":");  index += 2;  address = line.substring(index);  addresses.add(address.trim());  }  System.out.println(line);  }  br.close();  return addresses;  } catch (IOException e) {  e.printStackTrace();  }  }  return addresses;
    }