java 是否可以指定 JVM(或 IDE)使用哪个网络接口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1008781/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 14:47:12  来源:igfitidea点击:

Is it possible to specify which network interface for a JVM ( or IDE ) to use

javanetworkingidejvmintellij-idea

提问by wojtek_z

The situation goes like this, I have two network interfaces in my macbook pro. One is a statically defined Ethernet interface and the other one is a DHCP configured wireless interface.

情况是这样的,我的 macbook pro 中有两个网络接口。一个是静态定义的以太网接口,另一个是 DHCP 配置的无线接口。

I am trying to figure out how to configure a specific java project to use my statically wired interface only and not simply pick the first one in the list either through the JVM or through my IDEA ( IntelliJ 8 )

我试图弄清楚如何配置特定的 java 项目以仅使用我的静态有线接口,而不是简单地通过 JVM 或通过我的 IDEA ( IntelliJ 8 ) 选择列表中的第一个

I have managed to achieve this via an instance of vmware where I have my virtual environment feeding off my wired interface only but this ads performance problems and just general headaches of flipping between windows / osx.

我设法通过一个 vmware 实例实现了这一点,在该实例中,我的虚拟环境仅通过有线接口供电,但此广告性能问题以及在 windows / osx 之间切换的一般性头痛。

I have so far been unable to find a jvm property that let's me specify which nic gets used nor have I seen documentation for IntelliJ that defines that. If anyone has any ideas on how to solve this as painlessly as possible I would appreciate the help.

到目前为止,我一直无法找到让我指定使用哪个 nic 的 jvm 属性,也没有看到定义它的 IntelliJ 文档。如果有人对如何尽可能轻松地解决此问题有任何想法,我将不胜感激。

采纳答案by Zahangir Alam

Yes, you can specify by following way:

是的,您可以通过以下方式指定:

int port= 52000;//some free port

int port= 52000;//一些自由端口

MulticastSocket msocket = new MulticastSocket(port);

MulticastSocket msocket = new MulticastSocket(port);

msocket.setInterface(InetAddress.getByName("172.16.30.205"));

msocket.setInterface(InetAddress.getByName("172.16.30.205"));

Where 172.16.30.205 is one of my pc's IP address and I would like to communicate through interface.

其中 172.16.30.205 是我电脑的 IP 地址之一,我想通过接口进行通信。

回答by skaffman

java.net.Socket has a constructor which specifies which local IP address to bind to, and since each network interface gets its own IP address, you can use that.

java.net.Socket 有一个构造函数,它指定要绑定到哪个本地 IP 地址,并且由于每个网络接口都有自己的 IP 地址,因此您可以使用它。

But getting from a java.net.Socket to a higher-level protocol (e.g. HTTP) is another matter entirely, but you don't specify what that would be.

但是从 java.net.Socket 到更高级别的协议(例如 HTTP)完全是另一回事,但您没有指定那将是什么。

I don't know of a JVM-level way of doing this, sadly, just the above programmatic approach.

遗憾的是,我不知道 JVM 级别的方法,只是上面的编程方法。

回答by Cogsy

The NIC used for comms is selected by the operating system depending on the best 'route' available to what ever address is being accessed. There is no way for an application that sits above the NIC drivers to select a specific NIC. You can only get close when you're listening on a port bound at a specific address, which is only applicable if you're running a server.

用于通信的 NIC 由操作系统根据访问地址的最佳“路由”选择。位于 NIC 驱动程序之上的应用程序无法选择特定的 NIC。只有在侦听绑定在特定地址的端口时才能接近,这仅适用于运行服务器的情况。

You could try modifying the 'metric' of the route specific to a NIC to force the OS to prefer it.

您可以尝试修改特定于 NIC 的路由的“度量”,以强制操作系统更喜欢它。

回答by yawmark

This may be of help: Identify Network Interface Example

这可能会有所帮助: Identify Network Interface Example

"[I]f you have a preference or otherwise need to specify which NIC to use, you can query the system for the appropriate interfaces and find an address on the interface you want to use."

“[I] 如果您有偏好或需要指定使用哪个 NIC,您可以向系统查询适当的接口并在您想要使用的接口上找到地址。”