java 如何指定程序应连接的正确IP地址

发布于 2024-10-19 05:10:17 字数 731 浏览 1 评论 0原文

我有一个连接到远程主机的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你丑哭了我 2024-10-26 05:10:17

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.

夏天碎花小短裙 2024-10-26 05:10:17

如果你想使用特定的本地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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文