<?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</title>
	<atom:link href="http://www.ashishpaliwal.com/blog/tag/udp/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 Server using Apache MINA</title>
		<link>http://www.ashishpaliwal.com/blog/2008/10/implementing-udp-server-using-apache-mina/</link>
		<comments>http://www.ashishpaliwal.com/blog/2008/10/implementing-udp-server-using-apache-mina/#comments</comments>
		<pubDate>Tue, 21 Oct 2008 07:07:28 +0000</pubDate>
		<dc:creator>ashish</dc:creator>
				<category><![CDATA[Network Programming]]></category>
		<category><![CDATA[Apache MINA]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[UDP]]></category>

		<guid isPermaLink="false">http://www.ashishpaliwal.com/blog/?p=151</guid>
		<description><![CDATA[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 Create a Socket and listen for incoming connection Process each packet [...]]]></description>
			<content:encoded><![CDATA[<p>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.</p>
<p>Steps to create a UDP Server using java.net API's</p>
<ol>
<li>Create a Socket and listen for incoming connection</li>
<li>Process each packet in a separate thread <img src='http://www.ashishpaliwal.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' />  (I hate this, unfortunately need this to have high processing rate)</li>
<li>Parse and process the request and optionally send response (Lets omit this to keep things simple)</li>
</ol>
<div>Lets see how to achieve the same using Apache MINA</div>
<div>
<ol>
<li>Create a NioDatagramAcceptor</li>
<li>Add an IoHandler</li>
<li>Bind and make application ready to receive</li>
</ol>
</div>
<div>That's it <img src='http://www.ashishpaliwal.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </div>
<div>Before we dive into the code, lets see some assumption made to run this</div>
<div>
<ul>
<li>Our protocol is carrying Strings in UDP packet</li>
<li>We shall not do any transformation on the packets received. We shall just dump the content</li>
</ul>
<div><strong>The Handler</strong></div>
<p> </p>
<div><a href="http://www.ashishpaliwal.com/blog/wp-content/uploads/2008/10/udpsrvhandler.png"><img class="aligncenter size-full wp-image-142" title="udpsrvhandler" src="http://www.ashishpaliwal.com/blog/wp-content/uploads/2008/10/udpsrvhandler.png" alt="" width="500" height="192" /></a></div>
<p><script type="text/javascript"><!--
google_ad_client = "pub-6961884887741817";
/* 468x15LinkUnit */
google_ad_slot = "4400881690";
google_ad_width = 468;
google_ad_height = 15;
//-->
</script><br />
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
<p>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 "<em>message</em>". 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 <em>message </em>to that object.</p>
<div>That's all in the handler.</div>
<p> </p>
<div><strong>The Server</strong></div>
<div>Lets see the main Server code</div>
<p> </p>
<div><a href="http://www.ashishpaliwal.com/blog/wp-content/uploads/2008/10/minaudpsrv.png"><img class="aligncenter size-full wp-image-143" title="minaudpsrv" src="http://www.ashishpaliwal.com/blog/wp-content/uploads/2008/10/minaudpsrv.png" alt="" width="500" height="379" /></a></div>
<p> <script type="text/javascript"><!--
google_ad_client = "pub-6961884887741817";
/* 468x15LinkUnit */
google_ad_slot = "4400881690";
google_ad_width = 468;
google_ad_height = 15;
//-->
</script><br />
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
<div>The Server is even simpler than client <img src='http://www.ashishpaliwal.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </div>
<div>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.</div>
<p>So what does 31 does. There is wonderful description of this option at <a href="http://www.unixguide.net/network/socketfaq/4.5.shtml">http://www.unixguide.net/network/socketfaq/4.5.shtml</a><br />
The main function is pretty simple. The Server is ready. You can run it, using the Client from the post <a href="http://www.ashishpaliwal.com/blog/2008/10/implementing-udp-client-using-apache-mina/">Implementing UDP Client using Apache MINA</a></p>
<p><strong>References</strong></p>
<div>
<ul>
<li><a href="http://mina.apache.org/udp-tutorial.html">http://mina.apache.org/udp-tutorial.html</a></li>
</ul>
</div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.ashishpaliwal.com/blog/2008/10/implementing-udp-server-using-apache-mina/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
