20 October 2008 ~

Implementing UDP Client using Apache MINA


Its been a while that I wrote on Apache MINA. Please refer to my post collection on Apache MINA.

In this post, I have tried to capture briefly on how to implement a UDP Client using Apache MINA. We shall concentrate on keeping our scope limited to sending data over UDP. In Subsequent posts, we shall see how we can enhance this UDP Client to send SNMP Traps.

In brief, these are the steps we need to perform to send a UDP packet

  1. Create a Datagram Socket
  2. Create a Datagram Packet
  3. Send the packet
This is how we used to do when using java.net API's. The logical flow remains same, but lets see how it maps to Apache MINA
  1. Create a NioDatagramConnector instance
  2. Add an IoHandler (we can use an IoHandlerAdapter, as for this example we don't need to use all the API's)
  3. Connect the NioDatagramConnector
  4. Get the session
  5. Write the data onto the session
There is a slight difference in which the MINA API's work. The API's work asynchronously. A call to connect() on NioDatagramConnector, return a reference to ConnectFuture and the operation is kicked off in a new thread. Lets see how the Code looks like and see it in details.

 

 

Lets see the Code in detail now. We have maintained the Session at the class level, assuming that we may reuse the session for sending packets, if session is still active. At line 25, we check if the session is active or not. If inactive we initiate the connection. 

Line 28, creates an instance of NioDatagramConnector

Line 31, add an IoHandler to the NioDatagramConnector on Line 28. In MINA terminology, IoHandler contains the code where Business logic of an Application resides. In our case its a pure Adapter, with only log statements inside the functions.

Line 32, a call to connect(), returns an reference to ConnectFuture. The call, starts the connection operation, in a separate thread and returns to the caller. To cater to this asynchronous behaviour, we add an IoFutureListener, to returned ConnectFuture reference. Out there (Line 38-43), we just check if the connection is OK, we get the session from it. Line 39, signals that connection is connected and its safe to get the session for writing data.

The connect code is complete. Lets see how to send the data.

The send function is pretty simple. We just create an IoBuffer and call write on the session. We have called connect() on Line 59, just to ensure that the connection is alive. If it isn't, it shall create a connection and return a session back.

NOTE: This code is in its very simple form. There can be various mechanism to handle Session (based on destination etc). Also, please be aware of the asynchronous behaviour of the NioDatagramConnector connect() API. If the connection is not complete in time, you might see a null pointer at Line 67 :-(. But this is usually the case. For production code, it is advisable to ensure that session is never null.

That's it, out UDP client is ready. To create an SNMP Trap sender, we just need to pass Trap byte array to send() and we are done. Will try to post that article soon.

References

Sphere: Related Content

11 Responses to “Implementing UDP Client using Apache MINA”

  1. shiva 5 November 2008 at 6:29 pm Permalink

    hi…..

    can u plz send some complete sample UDP or TCP source codes using APACHE MINA…
    In this blog u have given a piece of code sample , plz send whole code….

    plz urgent to mail-id : shivachitta@gmail.com

    whatever the samples or examples you have ….

    thank you…..

  2. ashish 5 November 2008 at 6:41 pm Permalink

    Shiva, the code is complete, you just to glue this function together in main. I have removed unnecessary code, to keep posts simple. So, in main you call connect() and the call send data. Prepare the data u need to send.

    Its that simple

  3. shiva 10 November 2008 at 10:45 am Permalink

    hi….

    how to send and recieve data from server….
    can you plz send me one example which is working fine….

    a bit urgent..
    actually i’d like to do TFTP client server program….

    plz a bit urgent…

  4. shiva 10 November 2008 at 11:13 am Permalink

    hi…

    what is difference b/w apache.commons and apache MINA…

    could you tel me soon……

  5. shiva 11 November 2008 at 4:02 pm Permalink

    hi
    asish……….

    i’ll describe my intent here..

    “how to send data from client side file to server side file”

    plz help me soon……..

    thank you.

  6. shiva 21 November 2008 at 11:30 am Permalink

    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…….

  7. shiva 18 December 2008 at 10:51 am Permalink

    Subject: TFTP load balancing

    I am interested finding a load balancing solution for TFTP servers so that we can load balance the TFTP servers (and thus the TFTP IP protocol). Has anyone done this before, had experience with this, know of any products/solutions to help me. Please? :)..
    ( not only with MINA)

    please forward any links ..!!!

  8. sia 13 October 2009 at 12:50 pm Permalink

    hi… at the start, sorry for my terrible english…

    I must implement a chat with apache mina for a university project but I don’t understand nothing about Mina. I’m using Eclipse and it produces always the exception

    “Exception in thread “main” java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
    at org.apache.mina.core.service.SimpleIoProcessorPool.(SimpleIoProcessorPool.java:81)
    at org.apache.mina.core.polling.AbstractPollingIoAcceptor.(AbstractPollingIoAcceptor.java:104)
    at org.apache.mina.transport.socket.nio.NioSocketAcceptor.(NioSocketAcceptor.java:66)
    at Server.main(Server.java:18)
    Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClassInternal(Unknown Source)
    … 4 more”

    Please, could you send some complete sample TCP source code using apache MINA? because with the published code I can do anything…

    thank you

  9. ashish 13 October 2009 at 1:13 pm Permalink

    Please set classpath as per this page http://mina.apache.org/mina-v20-quick-start-guide.html
    Its not a problem. You can find the code with MINA distribution

  10. sia 13 October 2009 at 2:18 pm Permalink

    ops, I had losting a slf4j-api library.

    The Server is starting correctly but the client is a Nightmare…
    Just a few question:
    The code in the top is for Client or for the ClientHandler? Where is the main function for client?
    And where I can define the Object session?

    thanks

  11. ashish 13 October 2009 at 3:28 pm Permalink

    This is code extract from MINA example. Please get the code from there. You get the Session reference once your connection is complete :-)


Leave a Reply