Listening to UDP Datagrams using Java DatagramChannel
<p>I am trying to bind a Java DatagramChannel to listen to UDP traffic on a specific port and subinterface, but I cannot seem to get it to work on my linux host where I have created several virtual network subinterfaces (specifically the one I want to listen to is shown below). The code just fine if I host the java application in the same network Windows PC (using a different IP address however in that case for the bind call I simply had to specify the listening port and also on windows I do not use subinterfaces).</p>

<p>This is the relevant section output of ifconfig on my linux box showing the virtual network adapter( Subinterface )</p>

<pre><code>p2p1:37: flags=4163&lt;UP,BnetROADCAST,RUNNING,MULTICAST&gt;  mtu 1500
        inet 192.168.34.1  netmask 255.255.0.0  broadcast 192.168.255.255
        ether 00:13:72:a5:9c:e2  txqueuelen 1000  (Ethernet)
        device interrupt 16
</code></pre>

<p>The interface was setup as follows:</p>

<pre><code>ifconfig p2p1:37 192.168.34.1 netmask 255.255.0.0
</code></pre>

<p>The code that I use to initialize the non blocking DatagramChannel is shown below: (I need a <code>DatagramChannel</code> as I need to use a selector to read from multiple ports simultaneously in a non blocking manner).  I tried binding to just the default adapter using the port number which is stored in in <code>entry.getValue()</code> via <code>channel.socket().bind(new InetSocketAddress(entry.getValue())</code> but that did not receive any packets.  I read somewhere that after a DatagramChannel is constructed, it by default constructs a datagram socket that is bound to '0.0.0.0' and once bound it cannot be unbound - but I am not sure about any of that.  Any guidance would be greatly appreciated.</p>

<pre><code>    @Override
    protected Task&lt;Void&gt; createTask() {
        return new Task&lt;Void&gt;() {
            @Override
            protected Void call() throws Exception {
                updateMessage("Running...");
                Map&lt;String, DatagramChannel&gt; dlmuChannelInfo = new HashMap&lt;&gt;();
                Map&lt;String, DatagramChannel&gt; wsuChannelInfo =  new HashMap&lt;&gt;();
                try {
                    // array of bytes for receiving datagrams
                    ByteBuffer rxBuffer = ByteBuffer.allocateDirect(MAX_PACKET_SIZE);

                    for (Map.Entry&lt;String, Integer&gt; entry : mDLMUPortInfo.entrySet()) {
                        DatagramChannel channel = DatagramChannel.open();
                        // select only works with nonblocking channels
                        channel.configureBlocking(false);

//                        NetworkInterface iface = NetworkInterface.getByInetAddress(
//                            InetAddress.getByName("192.168.34.1"));
//                        List&lt;NetworkInterface&gt; list = Collections.list(
//                            NetworkInterface.getNetworkInterfaces());
//                        for (NetworkInterface next : list) {
//                        }

                        channel.socket().bind(new InetSocketAddress(
                            InetAddress.getByName("192.168.34.1"), entry.getValue()));
                        dlmuChannelInfo.put(entry.getKey(), channel);
                    }

                    // register for reads
                    try (
                        // instantiate a selector - note that this autocloses
                        Selector selector = Selector.open()) {
                        // register a selector for reads for both the DLMU &amp; WSU
                        Map&lt;SelectionKey, String&gt; keyServiceInfo = new HashMap&lt;&gt;();
                        for (Map.Entry&lt;String, DatagramChannel&gt; entry : dlmuChannelInfo.entrySet()) {
                            SelectionKey key = entry.getValue().register(
                                selector, SelectionKey.OP_READ);
                            keyServiceInfo.put(key, entry.getKey());
                        }
</code></pre>