<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Ashish's Tech Blog &#187; UDP Client</title>
	<atom:link href="http://www.ashishpaliwal.com/blog/tag/udp-client/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.ashishpaliwal.com/blog</link>
	<description>From Programmer, For Programmers</description>
	<lastBuildDate>Tue, 17 Aug 2010 12:04:43 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Implementing UDP Client using Apache MINA</title>
		<link>http://www.ashishpaliwal.com/blog/2008/10/implementing-udp-client-using-apache-mina/</link>
		<comments>http://www.ashishpaliwal.com/blog/2008/10/implementing-udp-client-using-apache-mina/#comments</comments>
		<pubDate>Mon, 20 Oct 2008 10:47:45 +0000</pubDate>
		<dc:creator>ashish</dc:creator>
				<category><![CDATA[Network Programming]]></category>
		<category><![CDATA[Apache MINA]]></category>
		<category><![CDATA[UDP Client]]></category>

		<guid isPermaLink="false">http://www.ashishpaliwal.com/blog/?p=110</guid>
		<description><![CDATA[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 [...]]]></description>
			<content:encoded><![CDATA[<p>Its been a while that I wrote on Apache MINA. Please refer to my post collection on <a href="http://www.ashishpaliwal.com/blog/apache-mina/">Apache MINA</a>.</p>
<p>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.</p>
<p>In brief, these are the steps we need to perform to send a UDP packet</p>
<ol>
<li>Create a Datagram Socket</li>
<li>Create a Datagram Packet</li>
<li>Send the packet</li>
</ol>
<div>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</div>
<div>
<ol>
<li>Create a NioDatagramConnector instance</li>
<li>Add an IoHandler (we can use an IoHandlerAdapter, as for this example we don't need to use all the API's)</li>
<li>Connect the NioDatagramConnector</li>
<li>Get the session</li>
<li>Write the data onto the session</li>
</ol>
<div>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.</div>
<p> </p>
<div><a href="http://www.ashishpaliwal.com/blog/wp-content/uploads/2008/10/minaudpclient.png"><img class="alignleft size-full wp-image-113" title="minaudpclient" src="http://www.ashishpaliwal.com/blog/wp-content/uploads/2008/10/minaudpclient.png" alt="" width="500" height="270" /></a></div>
<div> </div>
<p>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. </p>
<p>Line 28, creates an instance of NioDatagramConnector</p>
<p>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.</p>
<p>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.</p>
<p>The connect code is complete. Lets see how to send the data.</p>
<p><a href="http://www.ashishpaliwal.com/blog/wp-content/uploads/2008/10/minaudpclient_send.png"><img class="aligncenter size-full wp-image-116" title="minaudpclient_send" src="http://www.ashishpaliwal.com/blog/wp-content/uploads/2008/10/minaudpclient_send.png" alt="" width="500" height="286" /></a></p>
<p>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.</p>
<p>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 <img src='http://www.ashishpaliwal.com/blog/wp-includes/images/smilies/icon_sad.gif' alt=':-(' class='wp-smiley' /> . But this is usually the case. For production code, it is advisable to ensure that session is never null.</p>
<p>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.</p>
<p>References</p>
<ul>
<li><a href="http://mina.apache.org/udp-tutorial.html">http://mina.apache.org/udp-tutorial.html</a></li>
</ul>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.ashishpaliwal.com/blog/2008/10/implementing-udp-client-using-apache-mina/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
	</channel>
</rss>
