Implementing UDP Server using Apache MINA

In my last post we created a UDP client using Apache MINA. Lets turn the table and implement the Server side. Let's see how using Apache MINA reduces the effort to create a UDP Server.

Steps to create a UDP Server using java.net API's

  1. Create a Socket and listen for incoming connection
  2. Process each packet in a separate thread 🙂 (I hate this, unfortunately need this to have high processing rate)
  3. Parse and process the request and optionally send response (Lets omit this to keep things simple)
Lets see how to achieve the same using Apache MINA
  1. Create a NioDatagramAcceptor
  2. Add an IoHandler
  3. Bind and make application ready to receive
That's it 🙂
Before we dive into the code, lets see some assumption made to run this
  • Our protocol is carrying Strings in UDP packet
  • We shall not do any transformation on the packets received. We shall just dump the content
The Handler

 

The Handler is in its basic form. Our only method of interest is messageReceived. Since we know that we are getting "String" message without any transformation, we could easily create a new string from the bytes received. A wonderful thing to note about this API is the parameter "message". This makes the API generic enough to cater to any kind of objects. If we had used a ProtocolCodec in the chain and had transformed the byte[] into a custom Object, we would have type-casted message to that object.

That's all in the handler.

 

The Server
Lets see the main Server code

 

 

The Server is even simpler than client 🙂
We create an instance of NioDatagramAcceptor and add our custom handler to it. We then bind to the port desired. Here I have made it bind to the default port, code can be customized to bind to any port desired.

So what does 31 does. There is wonderful description of this option at http://www.unixguide.net/network/socketfaq/4.5.shtml
The main function is pretty simple. The Server is ready. You can run it, using the Client from the post Implementing UDP Client using Apache MINA

References

3 thoughts on “Implementing UDP Server using Apache MINA

  1. hi, ashish…..
    how do you do?….

    im devlping TFTP server with MINA, i have a prob…( describing here)

    i want to send a packet and i need to for ACK,
    if and only if ACK comes then i have to send next packet and so….
    ( here I must wait for ACK)

    I tried( even now trying) but not geting… Hope u can do it easily,
    If you can plz try and send me…..

    thanq…….

  2. Hi Ashish

    I have developed a UDP server using Apache MINA, I need to start it from JBoss JVM. But due to some reason after sometime (after receiving few messages) UDP server crashes.

    I need to monitor this UDP server inside JBoss (need to restart if crashes). Please let know how it is possible.

    — AMan

  3. Dear Ashish,

    There is a problem about Mina, would you mind to help me to solve it? Thanks much.

    The following is the mina java code:
    //##################################################
    NioDatagramAcceptor acceptor = new NioDatagramAcceptor();

    //Executor threadPool = Executors.newCachedThreadPool();
    //Executor threadPool = Executors.newFixedThreadPool(threadPoolSize);
    //UnorderedThreadPoolExecutor threadPool = new UnorderedThreadPoolExecutor(threadPoolSize);
    OrderedThreadPoolExecutor threadPool = new OrderedThreadPoolExecutor(threadPoolSize);
    acceptor.getFilterChain().addLast(“exector”, new ExecutorFilter(threadPool));
    //……
    //##################################################

    Look into the running with jvisual VM, i found that the pool threads are created, but the thread chosen is one by one, i donot know why? It shows that they are serial execution. After some time, only one pool-thread can be chosen, is it a bug on mina 2.0.9.

    The mina version: 2.0.9.

    Appreciate for yr help and sorry about this trouble.

    Best Regards,
    Tommy

Leave a Reply to AMan Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.