java 如何指定程序应连接的正确IP地址
我有一个连接到远程主机的java程序(swing)。这个程序在我的 Windows 7 机器上运行得很好。但是,我安装VirtualBox后,无法连接到远程盒子;经过调查,我们发现它开始将自身“绑定”到 VirtualBox IP 地址,而不是像之前那样绑定到“无线 LAN 适配器无线网络连接”。
我编写了一个连接到同一远程主机的测试程序,尽管有 Virtual Box,但该程序运行良好。
public static void main(String[] args) throws UnknownHostException, IOException {
Socket testSock = new Socket("10.86.149.206", 7547);
System.out.println("connected!");
}
问题:我如何告诉java swing 程序应该使用哪个IP 作为源IP 地址?
就其价值而言,我们使用 jdk 1.6
更新:
最终,我不得不卸载 VirtualBox 才能让 Java Swing 程序连接到远程主机:-(
更新 2 :
我想知道是否可以指定一个系统属性,以便 java 程序知道要做什么。
更新3:
如果有人想知道,我无法访问 swing 程序的源代码。如果可能的话,我想从外部规范其行为。**
I have a java program(swing) that connects to a remote host. This program was working just fine on my windows 7 box. However, after I installed VirtualBox, it failed to connect to the remote box; On investigation we found it started "binding" itself to the VirtualBox IP address rather than the "Wireless LAN Adapter wireless network connection" that it previously did.
I wrote a test program which connected to the same remote host and this program worked fine despite Virtual Box.
public static void main(String[] args) throws UnknownHostException, IOException {
Socket testSock = new Socket("10.86.149.206", 7547);
System.out.println("connected!");
}
Question : How do I tell the java swing program which IP it should use as the source IP address ?
for what it's worth, we use jdk 1.6
UPDATE :
Eventually, I had to uninstall VirtualBox in order to get the Java Swing program to connect to the remote host :-(
UPDATE 2 :
I was wondering if I could specify a system property so the java program would know what to do.
UPDATE 3:
in case anyone was wondering, I don't have access to the swing program's source code. I would like to regulate its behavior externally, if possible.**
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Socket 构造函数支持四个参数版本,您还可以在其中指定本地地址和端口。请参阅 java.net.Socket 了解详细信息。
The Socket contructor supports a four parameter version where you can specify the local address and port as well. See java.net.Socket for details.
这似乎本质上是同一个问题: 是否可以指定 JVM(或 IDE)使用哪个网络接口
This seems to be essentially the same question: Is it possible to specify which network interface for a JVM ( or IDE ) to use
如果你想使用特定的本地IP地址连接到服务器,你应该调用bind方法并将该地址作为参数传递。
If you want to use specific local IP address to connect to server you should call bind method and as a parameter pass this address.