Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows7上的以太网?_Windows_Networking - Fatal编程技术网

Windows7上的以太网?

Windows7上的以太网?,windows,networking,Windows,Networking,我怎么知道,哪个接口将允许UPD连接? 我得到如下所有网络接口的列表: 枚举networkInterfaceEnumeration=NetworkInterface.getNetworkInterfaces 我找到的解决方案是迭代所有接口,并测试为连接启用的接口: 示例工作代码为: public static DatagramChannel openDatagramChannelForAnyWorkingInterface(int port, String ip) throws IOExcept

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

枚举networkInterfaceEnumeration=NetworkInterface.getNetworkInterfaces


我找到的解决方案是迭代所有接口,并测试为连接启用的接口: 示例工作代码为:

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;
}

你好,奥利弗,这不是一个重复的问题。由于基于windows的ipconfig不提供详细信息,如列出所有接口、支持的连接类型等。因此,我必须迭代所有接口以测试UDP连接。
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;
}