blob: 84c07b2ec5d0b3afcdc32227b3965d82487c571b [file] [log] [blame]
<!DOCTYPE html>
<html lang="en">
<head>
<title>2.4 - Sample UDP Server &mdash; Apache MINA</title>
<link href="/assets/css/common.css" rel="stylesheet" type="text/css"/>
<link href="/assets/css/mina.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<script src="https://www.apachecon.com/event-images/snippet.js"></script>
<div id="container">
<div id="header">
<div id="subProjectsNavBar">
<a href="/">
Apache MINA Project
</a>
&nbsp;|&nbsp;
<a href="/mina-project/">
<strong>MINA</strong>
</a>
&nbsp;|&nbsp;
<a href="/asyncweb-project/">
AsyncWeb
</a>
&nbsp;|&nbsp;
<a href="/ftpserver-project/">
FtpServer
</a>
&nbsp;|&nbsp;
<a href="/sshd-project/">
SSHD
</a>
&nbsp;|&nbsp;
<a href="/vysper-project/">
Vysper
</a>
</div>
</div>
<div id="content">
<div id="leftColumn">
<div id="navigation">
<a class="acevent" data-format="wide" data-width="170"></a>
<h5>Social Networks</h5>
<ul>
<li><a href="https://fosstodon.org/@apachemina">Apache MINA Mastodon</a></li>
</ul>
<h5>Latest Downloads</h5>
<ul>
<li><a href="/mina-project/downloads_2_0.html">Mina 2.0.25</a></li>
<li><a href="/mina-project/downloads_2_1.html">Mina 2.1.8</a></li>
<li><a href="/mina-project/downloads_2_2.html">Mina 2.2.3</a></li>
<li><a href="/mina-project/downloads_old.html">Mina old versions</a></li>
</ul>
<h5>Documentation</h5>
<ul>
<li><a href="/mina-project/documentation.html" class="external-link" rel="nofollow">Base documentation</a></li>
<li><a href="/mina-project/userguide/user-guide-toc.html" class="external-link" rel="nofollow">User guide</a></li>
<li><a href="/mina-project/2.2-vs-2.1.html" class="external-link" rel="nofollow">2.2 vs 2.1</a></li>
<li><a href="/mina-project/2.1-vs-2.0.html" class="external-link" rel="nofollow">2.1 vs 2.0</a></li>
<li><a href="/mina-project/features.html" class="external-link" rel="nofollow">Features</a></li>
<li><a href="/mina-project/road-map.html" class="external-link" rel="nofollow">Road Map</a></li>
<li><a href="/mina-project/quick-start-guide.html" class="external-link" rel="nofollow">Quick Start Guide</a></li>
<li><a href="/mina-project/faq.html" class="external-link" rel="nofollow">FAQ</a></li>
</ul>
<h5>Resources</h5>
<ul>
<li><a href="/mina-project/mailing-lists.html" class="external-link" rel="nofollow">Mailing lists &amp; IRC</a></li>
<li><a href="/mina-project/issue-tracking.html" class="external-link" rel="nofollow">Issue tracking</a></li>
<li><a href="/mina-project/sources.html" class="external-link" rel="nofollow">Sources</a></li>
<li><a href="/mina-project/gen-docs/latest-2.0/apidocs/index.html" class="external-link" rel="nofollow">API Javadoc 2.0.25</a></li>
<li><a href="/mina-project/gen-docs/latest-2.1/apidocs/index.html" class="external-link" rel="nofollow">API Javadoc 2.1.8</a></li>
<li><a href="/mina-project/gen-docs/latest-2.2/apidocs/index.html" class="external-link" rel="nofollow">API Javadoc 2.2.3</a></li>
<li><a href="/mina-project/gen-docs/latest-2.0/xref/index.html" class="external-link" rel="nofollow">API xref 2.0.25</a></li>
<li><a href="/mina-project/gen-docs/latest-2.1/xref/index.html" class="external-link" rel="nofollow">API xref 2.1.8</a></li>
<li><a href="/mina-project/gen-docs/latest-2.2/xref/index.html" class="external-link" rel="nofollow">API xref 2.2.3</a></li>
<li><a href="/mina-project/performances.html" class="external-link" rel="nofollow">Performances</a></li>
<li><a href="/mina-project/testimonials.html" class="external-link" rel="nofollow">Testimonials</a></li>
<li><a href="/mina-project/conferences.html" class="external-link" rel="nofollow">Conferences</a></li>
<li><a href="/mina-project/developer-guide.html" class="external-link" rel="nofollow">Developers Guide</a></li>
<li><a href="/mina-project/related-projects.html" class="external-link" rel="nofollow">Related Projects</a></li>
<li><a href="https://people.apache.org/~vgritsenko/stats/projects/mina.html" class="external-link" rel="nofollow">Statistics</a></li>
</ul>
<h5>Community</h5>
<ul>
<li><a href="https://www.apache.org/foundation/contributing.html" class="external-link" rel="nofollow">Contributing</a></li>
<li><a href="/contributors.html" class="external-link" rel="nofollow">Team</a></li>
<li><a href="/special-thanks.html" class="external-link" rel="nofollow">Special Thanks</a></li>
<li><a href="https://www.apache.org/security/" class="external-link" rel="nofollow">Security</a></li>
</ul>
<h5>About Apache</h5>
<ul>
<li><a href="https://www.apache.org" class="external-link" rel="nofollow">Apache main site</a></li>
<li><a href="https://www.apache.org/licenses/" class="external-link" rel="nofollow">License</a></li>
<li><a href="https://www.apache.org/foundation/sponsorship.html" title="The ASF sponsorship program" class="external-link" rel="nofollow">Sponsorship program</a></li>
<li><a href="https://www.apache.org/foundation/thanks.html" class="external-link" rel="nofollow">Thanks</a></li>
</ul>
<h3><a name="Navigation-Upcoming"></a>Upcoming</h3>
<ul>
<li>No event</li>
</ul>
</div>
</div>
<div id="rightColumn">
<div class="nav">
<div class="nav_prev">
<a href="ch2.3-sample-tcp-client.html">2.3 - Sample TCP-Client</a>
</div>
<div class="nav_up">
<a href="ch2-basics.html">Chapter 2 - Basics</a>
</div>
<div class="nav_next">
<a href="ch2.5-sample-udp-client.html">2.5 - Sample UDP Client</a>
</div>
<div class="clearfix"></div>
</div>
<h1 id="24---sample-udp-server">2.4 - Sample UDP Server</h1>
<p>We will begin by looking at the code found in the <a href="https://nightlies.apache.org/mina/mina/2.0.22/xref/org/apache/mina/example/udp/package-summary.html">org.apache.mina.example.udp</a> package. To keep life simple, we shall concentrate on MINA related constructs only.</p>
<p>To construct the server, we shall have to do the following:</p>
<ol>
<li>Create a Datagram Socket to listen for incoming Client requests (See <a href="https://nightlies.apache.org/mina/mina/2.0.22/xref/org/apache/mina/example/udp/MemoryMonitor.html">MemoryMonitor.java</a>)</li>
<li>Create an IoHandler to handle the MINA framework generated events (See <a href="https://nightlies.apache.org/mina/mina/2.0.22/xref/org/apache/mina/example/udp/MemoryMonitorHandler.html">MemoryMonitorHandler.java</a>)</li>
</ol>
<p>Here is the first snippet that addresses Point# 1:</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-java" data-lang="java">NioDatagramAcceptor acceptor <span style="color:#666">=</span> <span style="color:#a2f;font-weight:bold">new</span> NioDatagramAcceptor<span style="color:#666">(</span><span style="color:#666">)</span><span style="color:#666">;</span>
acceptor<span style="color:#666">.</span><span style="color:#b44">setHandler</span><span style="color:#666">(</span><span style="color:#a2f;font-weight:bold">new</span> MemoryMonitorHandler<span style="color:#666">(</span><span style="color:#a2f;font-weight:bold">this</span><span style="color:#666">)</span><span style="color:#666">)</span><span style="color:#666">;</span>
</code></pre></div><p>Here, we create a NioDatagramAcceptor to listen for incoming Client requests, and set the IoHandler.The variable &lsquo;PORT&rsquo; is just an int. The next step is to add a logging filter to the filter chain that this DatagramAcceptor will use. LoggingFilter is a very nice way to see MINA in Action. It generate log statements at various stages, providing an insight into how MINA works.</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-java" data-lang="java">DefaultIoFilterChainBuilder chain <span style="color:#666">=</span> acceptor<span style="color:#666">.</span><span style="color:#b44">getFilterChain</span><span style="color:#666">(</span><span style="color:#666">)</span><span style="color:#666">;</span>
chain<span style="color:#666">.</span><span style="color:#b44">addLast</span><span style="color:#666">(</span><span style="color:#b44">&#34;logger&#34;</span><span style="color:#666">,</span> <span style="color:#a2f;font-weight:bold">new</span> LoggingFilter<span style="color:#666">(</span><span style="color:#666">)</span><span style="color:#666">)</span><span style="color:#666">;</span>
</code></pre></div><p>Next we get into some more specific code for the UDP traffic. We will set the acceptor to reuse the address</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-java" data-lang="java">DatagramSessionConfig dcfg <span style="color:#666">=</span> acceptor<span style="color:#666">.</span><span style="color:#b44">getSessionConfig</span><span style="color:#666">(</span><span style="color:#666">)</span><span style="color:#666">;</span>
dcfg<span style="color:#666">.</span><span style="color:#b44">setReuseAddress</span><span style="color:#666">(</span><span style="color:#a2f;font-weight:bold">true</span><span style="color:#666">)</span><span style="color:#666">;</span>acceptor<span style="color:#666">.</span><span style="color:#b44">bind</span><span style="color:#666">(</span><span style="color:#a2f;font-weight:bold">new</span> InetSocketAddress<span style="color:#666">(</span>PORT<span style="color:#666">)</span><span style="color:#666">)</span><span style="color:#666">;</span>
</code></pre></div><p>Of course the last thing that is required here is to call bind().</p>
<h2 id="iohandler-implementation">IoHandler implementation</h2>
<p>There are three major events of interest for our Server Implementation</p>
<ul>
<li>Session Created</li>
<li>Message Received</li>
<li>Session Closed</li>
</ul>
<p>Lets look at each of them in detail</p>
<h3 id="session-created-event">Session Created Event</h3>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-java" data-lang="java"><span style="color:#a2f">@Override</span>
<span style="color:#a2f;font-weight:bold">public</span> <span style="color:#0b0;font-weight:bold">void</span> <span style="color:#00a000">sessionCreated</span><span style="color:#666">(</span>IoSession session<span style="color:#666">)</span> <span style="color:#a2f;font-weight:bold">throws</span> Exception <span style="color:#666">{</span>
SocketAddress remoteAddress <span style="color:#666">=</span> session<span style="color:#666">.</span><span style="color:#b44">getRemoteAddress</span><span style="color:#666">(</span><span style="color:#666">)</span><span style="color:#666">;</span>
server<span style="color:#666">.</span><span style="color:#b44">addClient</span><span style="color:#666">(</span>remoteAddress<span style="color:#666">)</span><span style="color:#666">;</span>
<span style="color:#666">}</span>
</code></pre></div><p>In the session creation event, we just call addClient() function, which internally adds a Tab to the UI</p>
<h3 id="message-received-event">Message Received Event</h3>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-java" data-lang="java"><span style="color:#a2f">@Override</span>
<span style="color:#a2f;font-weight:bold">public</span> <span style="color:#0b0;font-weight:bold">void</span> <span style="color:#00a000">messageReceived</span><span style="color:#666">(</span>IoSession session<span style="color:#666">,</span> Object message<span style="color:#666">)</span> <span style="color:#a2f;font-weight:bold">throws</span> Exception <span style="color:#666">{</span>
<span style="color:#a2f;font-weight:bold">if</span> <span style="color:#666">(</span>message <span style="color:#a2f;font-weight:bold">instanceof</span> IoBuffer<span style="color:#666">)</span> <span style="color:#666">{</span>
IoBuffer buffer <span style="color:#666">=</span> <span style="color:#666">(</span>IoBuffer<span style="color:#666">)</span> message<span style="color:#666">;</span>
SocketAddress remoteAddress <span style="color:#666">=</span> session<span style="color:#666">.</span><span style="color:#b44">getRemoteAddress</span><span style="color:#666">(</span><span style="color:#666">)</span><span style="color:#666">;</span>
server<span style="color:#666">.</span><span style="color:#b44">recvUpdate</span><span style="color:#666">(</span>remoteAddress<span style="color:#666">,</span> buffer<span style="color:#666">.</span><span style="color:#b44">getLong</span><span style="color:#666">(</span><span style="color:#666">)</span><span style="color:#666">)</span><span style="color:#666">;</span>
<span style="color:#666">}</span>
<span style="color:#666">}</span>
</code></pre></div><p>In the message received event, we just dump the data received in the message. Applications that need to send responses, can process message and write the responses onto session in this function.</p>
<h3 id="session-closed-event">Session Closed Event</h3>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-java" data-lang="java"><span style="color:#a2f">@Override</span>
<span style="color:#a2f;font-weight:bold">public</span> <span style="color:#0b0;font-weight:bold">void</span> <span style="color:#00a000">sessionClosed</span><span style="color:#666">(</span>IoSession session<span style="color:#666">)</span> <span style="color:#a2f;font-weight:bold">throws</span> Exception <span style="color:#666">{</span>
System<span style="color:#666">.</span><span style="color:#b44">out</span><span style="color:#666">.</span><span style="color:#b44">println</span><span style="color:#666">(</span><span style="color:#b44">&#34;Session closed...&#34;</span><span style="color:#666">)</span><span style="color:#666">;</span>
SocketAddress remoteAddress <span style="color:#666">=</span> session<span style="color:#666">.</span><span style="color:#b44">getRemoteAddress</span><span style="color:#666">(</span><span style="color:#666">)</span><span style="color:#666">;</span>
server<span style="color:#666">.</span><span style="color:#b44">removeClient</span><span style="color:#666">(</span>remoteAddress<span style="color:#666">)</span><span style="color:#666">;</span>
<span style="color:#666">}</span>
</code></pre></div><p>In the Session Closed, event we just remove the Client tab from the UI</p>
<div class="nav">
<div class="nav_prev">
<a href="ch2.3-sample-tcp-client.html">2.3 - Sample TCP-Client</a>
</div>
<div class="nav_up">
<a href="ch2-basics.html">Chapter 2 - Basics</a>
</div>
<div class="nav_next">
<a href="ch2.5-sample-udp-client.html">2.5 - Sample UDP Client</a>
</div>
<div class="clearfix"></div>
</div>
</div>
<div id="endContent"></div>
</div>
<div id="footer">
&copy; 2003-2024, <a href="https://www.apache.org">The Apache Software Foundation</a> - <a href="https://privacy.apache.org/policies/privacy-policy-public.html">Privacy Policy</a><br />
Apache MINA, MINA, Apache Vysper, Vysper, Apache SSHd, SSHd, Apache FtpServer, FtpServer, Apache AsyncWeb, AsyncWeb,
Apache, the Apache feather logo, and the Apache Mina project logos are trademarks of The Apache Software Foundation.
</div>
</div>
</body>
</html>