当程序在VMWare中运行时,Socket的IP
我想用 VMWare 测试我的程序。所以我在VMWare 中运行了该程序。在该机器上使用 ipconfig 命令给我一些 IP,如 192.168.XXX.XXX。它有一个服务器套接字侦听该 IP 上的某个端口。当有人需要与它交谈时,它会连接到这个 192.168.xxx.xxx IP(使用 Socket s = new Socket(IP, port))。 但是当我在程序内部调用socket.getInetAdress()时,它应该返回192.168.xx IP,但它返回0.0.0.0,并且模拟失败。
我能做些什么? 提前谢谢:)
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 :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是 Java 问题而不是 VMWare 问题。我假设您正在尝试获取计算机的 IP,而不仅仅是服务器绑定的 IP。在这种情况下,请参阅此问题以获取一些建议的解决方案: java InetAddress.getLocalHost();返回127.0.0.1 ...如何获取真实IP?
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
表示套接字绑定到所有可用的本地 IP 地址,而不是特定的 IP 地址。对于使用通配符 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.这可能是因为像这样的事情
InetAddress.getLocalHost();
获取主机名,然后从中查找 ip 地址。如果您的虚拟机将该主机名解析为 localhost,那么您将获得 127.0.0.1。这很可能就是正在发生的事情。您需要从外部网络接口获取 IP 地址。
尝试运行这个:
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: