IP of a Socket, when program runs in VMWare
I wanna test my program with VMWare. So I've run the 开发者_开发百科program in a VMWare. Using ipconfig command on that machine gives me some IP like 192.168.XXX.XXX. It has a server socket listening on some port, on this IP. When someone needs to speak with it, it connect to this 192.168.xxx.xxx IP fine (using Socket s = new Socket(IP, port)). But when inside the program I call socket.getInetAdress() which should return the 192.168.x.x IP, it returns 0.0.0.0 instead, and the simulation fails.
What can I do? Thanx in advance :)
This is a Java issue rather than a VMWare issue. I'm assuming that you're trying to get your machine's IP rather than just the ip that the server is bound to. In that case, see this question for some proposed solutions: java InetAddress.getLocalHost(); returns 127.0.0.1 ... how to get REAL IP?
0.0.0.0
means the socket is bound to all available local IP addresses, not a specific IP address. That is normal for listening servers that are bound using wildcard IPs.
This is likely because things like
InetAddress.getLocalHost();
get the hostname and then lookup the ipAddress from it. If your vm resolves that hostname to localhost, then you will get 127.0.0.1. This is likely what is happening. You need to get the ip address from the external network interface.
Try running this:
Enumeration netInterfaces=NetworkInterface.getNetworkInterfaces();
while(netInterfaces.hasMoreElements()){
NetworkInterface ni=(NetworkInterface)netInterfaces.nextElement();
InetAddress ip=(InetAddress) ni.getInetAddresses().nextElement();
System.out.println(ni.getName()+" "+ip.getAddress());
}
精彩评论