title: Listening for TCP and UDP requests on the same port
body: <p>I am writing a Client/Server set of programs</p>
  <p>Depending on the operation requested by the client, I use make TCP or UDP request.</p>
  <p>Implementing the client side is straight-forward, since I can easily open connection with any protocol and send the request to the server-side.</p>
  <p>On the servers-side, on the other hand, I would like to listen both for UDP and TCP connections on the same port. Moreover, I like the server to open new thread for each connection request.</p>
  <p>I have adopted the approach explained in: <a href="http://www.java2s.com/Code/Java/Network-Protocol/HandlesTCPandUDPconnectionsandprovidesexceptionhandlinganderrorlogging.htm" rel="nofollow noreferrer">link text</a></p>
  <p>I have extended this code sample by creating new threads for each TCP/UDP request.</p>
  <p>This works correctly if I use TCP only, but it fails when I attempt to make UDP bindings.</p>
  <p>Please give me any suggestion how can I correct this.</p>
  <p>tnx</p>
  <p>Here is the Server Code:</p>
  <pre><code>public class Server {
  public static void main(String args[]) {
      try {
          int port = 4444;
          if (args.length &gt; 0)
              port = Integer.parseInt(args[0]);
  
          SocketAddress localport = new InetSocketAddress(port);
  
          // Create and bind a tcp channel to listen for connections on.
          ServerSocketChannel tcpserver = ServerSocketChannel.open();
          tcpserver.socket().bind(localport);
  
          // Also create and bind a DatagramChannel to listen on.
          DatagramChannel udpserver = DatagramChannel.open();
          udpserver.socket().bind(localport);
  
          // Specify non-blocking mode for both channels, since our
          // Selector object will be doing the blocking for us.
          tcpserver.configureBlocking(false);
          udpserver.configureBlocking(false);
  
          // The Selector object is what allows us to block while waiting
          // for activity on either of the two channels.
          Selector selector = Selector.open();
  
          tcpserver.register(selector, SelectionKey.OP_ACCEPT);
          udpserver.register(selector, SelectionKey.OP_READ);
          
          System.out.println(&quot;Server Sterted on port: &quot; + port + &quot;!&quot;);
          
          //Load Map
          Utils.LoadMap(&quot;mapa&quot;);
          System.out.println(&quot;Server map ... LOADED!&quot;);
          
          // Now loop forever, processing client connections
          while(true) {
              try { 
                  selector.select();
                  Set&lt;SelectionKey&gt; keys = selector.selectedKeys();
                  
                  // Iterate through the Set of keys.
                  for (Iterator&lt;SelectionKey&gt; i = keys.iterator(); i.hasNext();) {
                      SelectionKey key = i.next();
                      i.remove();
  
                      Channel c = key.channel();
  
                      if (key.isAcceptable() &amp;&amp; c == tcpserver) {
                          new TCPThread(tcpserver.accept().socket()).start();
                      } else if (key.isReadable() &amp;&amp; c == udpserver) {
                          new UDPThread(udpserver.socket()).start();
                      }
                  }
              } catch (Exception e) {
                  e.printStackTrace();
              }
          }
      } catch (Exception e) {
          e.printStackTrace();
          System.err.println(e);
          System.exit(1);
      }
  }
  </code></pre>
  <p>}</p>
  <p>The UDPThread code:</p>
  <pre><code>public class UDPThread extends Thread {
  private DatagramSocket socket = null;
  
  public UDPThread(DatagramSocket socket) {
      super(&quot;UDPThread&quot;);
      this.socket = socket;
  }
  
  @Override
  public void run() {
      byte[] buffer = new byte[2048];
      try {           
          DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
          socket.receive(packet);
          
          String inputLine = new String(buffer);
          String outputLine = Utils.processCommand(inputLine.trim());
          
          DatagramPacket reply = new DatagramPacket(outputLine.getBytes(), outputLine.getBytes().length,
                                                    packet.getAddress(), packet.getPort());
          socket.send(reply);
          
      } catch (IOException e) {
          e.printStackTrace();
      }
      socket.close(); 
  }
  </code></pre>
  <p>}</p>
  <p>I receive:</p>
  <pre><code>Exception in thread &quot;UDPThread&quot; java.nio.channels.IllegalBlockingModeException
  at sun.nio.ch.DatagramSocketAdaptor.receive(Unknown Source)
  at server.UDPThread.run(UDPThread.java:25)
  </code></pre>
  <p>10x</p>