【问题标题】:Ethernet on windows 7?Windows 7上的以太网?
【发布时间】:2019-02-04 07:22:28
【问题描述】:

我怎么知道哪个接口允许 UPD 连接? 我得到所有网络接口的列表,如下所示:

Enumeration<NetworkInterface> networkInterfaceEnumeration = NetworkInterface.getNetworkInterfaces();

$ ipconfig
Windows IP Configuration

Ethernet adapter Local Area Connection:

   Connection-specific DNS Suffix  . : myworld.com
   IPv4 Address. . . . . . . . . . . : 10.219.1.157
   Subnet Mask . . . . . . . . . . . : 255.255.254.0
   Default Gateway . . . . . . . . . : 10.219.0.1

Tunnel adapter isatap.myworld.com:
   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . : myworld.com

【问题讨论】:

  • 嗨奥利弗,这不是一个重复的问题。由于基于 Windows 的 ipconfig 不提供详细信息,例如列出所有接口、支持的连接类型等。因此,我必须遍历所有接口来测试我的 UDP 连接。

标签: windows networking


【解决方案1】:

我找到的解决方案是遍历所有接口,并测试启用了哪些连接: 示例工作代码是:

public static DatagramChannel openDatagramChannelForAnyWorkingInterface(int port, String ip) throws IOException {

    DatagramChannel channel;
    try {
        channel = DatagramChannel.open(StandardProtocolFamily.INET);
        channel.configureBlocking(true);
        channel.socket().setReuseAddress(true);
        channel.bind(new InetSocketAddress(port));
        Enumeration<NetworkInterface> networkInterfaceEnumeration = NetworkInterface.getNetworkInterfaces();
        for (NetworkInterface np : Collections.list(networkInterfaceEnumeration)) {
            try {
                channel.join(InetAddress.getByName(ip), NetworkInterface.getByName(np.getName()));
                log.info("Data joining channel ip [{}], port [{}] interface [{}]", ip, port, np.getName());
                break;
            } catch (Exception ignore) {
            }
        }
    } catch (Exception e) {
        log.error("Exception while subscribing to market data: ", e);
        throw e;
    }
    return channel;
}

【讨论】:

    猜你喜欢
    • 2011-03-12
    • 2012-05-23
    • 2014-05-15
    • 1970-01-01
    • 2011-11-21
    • 2014-01-26
    • 1970-01-01
    • 1970-01-01
    • 2016-10-25
    相关资源
    最近更新 更多