blob: e38ee4759acfa3f18b6f256d87fcb607c27c8e62 [file] [log] [blame]
<!DOCTYPE html>
<html lang="en">
<head>
<title>2.5 - Sample UDP Client &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.4-sample-udp-server.html">2.4 - Sample UDP Server</a>
</div>
<div class="nav_up">
<a href="ch2-basics.html">Chapter 2 - Basics</a>
</div>
<div class="nav_next">
<a href="ch2.6-summary.html">2.6 - Summary</a>
</div>
<div class="clearfix"></div>
</div>
<h1 id="25--sample-udp-client">2.5 -Sample UDP Client</h1>
<p>Lets look at the client code for the UDP Server from previous section.</p>
<p>To implement the Client we need to do following:</p>
<ul>
<li>Create Socket and Connect to Server</li>
<li>Set the IoHandler</li>
<li>Collect free memory</li>
<li>Send the Data to the Server</li>
</ul>
<p>We will begin by looking at the file <a href="https://nightlies.apache.org/mina/mina/2.0.22/xref/org/apache/mina/example/udp/client/MemMonClient.html">MemMonClient.java</a>, found in the org.apache.mina.example.udp.client java package. The first few lines of the code are simple and straightforward.</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">connector <span style="color:#666">=</span> <span style="color:#a2f;font-weight:bold">new</span> NioDatagramConnector<span style="color:#666">(</span><span style="color:#666">)</span><span style="color:#666">;</span>
connector<span style="color:#666">.</span><span style="color:#b44">setHandler</span><span style="color:#666">(</span> <span style="color:#a2f;font-weight:bold">this</span> <span style="color:#666">)</span><span style="color:#666">;</span>
ConnectFuture connFuture <span style="color:#666">=</span> connector<span style="color:#666">.</span><span style="color:#b44">connect</span><span style="color:#666">(</span> <span style="color:#a2f;font-weight:bold">new</span> InetSocketAddress<span style="color:#666">(</span><span style="color:#b44">&#34;localhost&#34;</span><span style="color:#666">,</span> MemoryMonitor<span style="color:#666">.</span><span style="color:#b44">PORT</span> <span style="color:#666">)</span><span style="color:#666">)</span><span style="color:#666">;</span>
</code></pre></div><p>Here we create a NioDatagramConnector, set the handler and connect to the server. One gotcha I ran into was that you must set the host in the InetSocketAddress object or else nothing seems to work. This example was mostly written and tested on a Windows XP machine, so things may be different elsewhere. Next we will wait for acknowledgment that the client has connected to the server. Once we know we are connected, we can start writing data to the server. Here is that code:</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">connFuture<span style="color:#666">.</span><span style="color:#b44">addListener</span><span style="color:#666">(</span> <span style="color:#a2f;font-weight:bold">new</span> IoFutureListener<span style="color:#666">(</span><span style="color:#666">)</span><span style="color:#666">{</span>
<span style="color:#a2f;font-weight:bold">public</span> <span style="color:#0b0;font-weight:bold">void</span> <span style="color:#00a000">operationComplete</span><span style="color:#666">(</span>IoFuture future<span style="color:#666">)</span> <span style="color:#666">{</span>
ConnectFuture connFuture <span style="color:#666">=</span> <span style="color:#666">(</span>ConnectFuture<span style="color:#666">)</span>future<span style="color:#666">;</span>
<span style="color:#a2f;font-weight:bold">if</span><span style="color:#666">(</span> connFuture<span style="color:#666">.</span><span style="color:#b44">isConnected</span><span style="color:#666">(</span><span style="color:#666">)</span> <span style="color:#666">)</span><span style="color:#666">{</span>
session <span style="color:#666">=</span> future<span style="color:#666">.</span><span style="color:#b44">getSession</span><span style="color:#666">(</span><span style="color:#666">)</span><span style="color:#666">;</span>
<span style="color:#a2f;font-weight:bold">try</span> <span style="color:#666">{</span>
sendData<span style="color:#666">(</span><span style="color:#666">)</span><span style="color:#666">;</span>
<span style="color:#666">}</span> <span style="color:#a2f;font-weight:bold">catch</span> <span style="color:#666">(</span>InterruptedException e<span style="color:#666">)</span> <span style="color:#666">{</span>
e<span style="color:#666">.</span><span style="color:#b44">printStackTrace</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:#a2f;font-weight:bold">else</span> <span style="color:#666">{</span>
log<span style="color:#666">.</span><span style="color:#b44">error</span><span style="color:#666">(</span><span style="color:#b44">&#34;Not connected...exiting&#34;</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><span style="color:#666">;</span>
</code></pre></div><p>Here we add a listener to the ConnectFuture object and when we receive a callback that the client has connected, we will start to write data. The writing of data to the server will be handled by a method called sendData. This method is shown below:</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"><span style="color:#a2f;font-weight:bold">private</span> <span style="color:#0b0;font-weight:bold">void</span> <span style="color:#00a000">sendData</span><span style="color:#666">(</span><span style="color:#666">)</span> <span style="color:#a2f;font-weight:bold">throws</span> InterruptedException <span style="color:#666">{</span>
<span style="color:#a2f;font-weight:bold">for</span> <span style="color:#666">(</span><span style="color:#0b0;font-weight:bold">int</span> i <span style="color:#666">=</span> 0<span style="color:#666">;</span> i <span style="color:#666">&lt;</span> 30<span style="color:#666">;</span> i<span style="color:#666">+</span><span style="color:#666">+</span><span style="color:#666">)</span> <span style="color:#666">{</span>
<span style="color:#0b0;font-weight:bold">long</span> free <span style="color:#666">=</span> Runtime<span style="color:#666">.</span><span style="color:#b44">getRuntime</span><span style="color:#666">(</span><span style="color:#666">)</span><span style="color:#666">.</span><span style="color:#b44">freeMemory</span><span style="color:#666">(</span><span style="color:#666">)</span><span style="color:#666">;</span>
IoBuffer buffer <span style="color:#666">=</span> IoBuffer<span style="color:#666">.</span><span style="color:#b44">allocate</span><span style="color:#666">(</span>8<span style="color:#666">)</span><span style="color:#666">;</span>
buffer<span style="color:#666">.</span><span style="color:#b44">putLong</span><span style="color:#666">(</span>free<span style="color:#666">)</span><span style="color:#666">;</span>
buffer<span style="color:#666">.</span><span style="color:#b44">flip</span><span style="color:#666">(</span><span style="color:#666">)</span><span style="color:#666">;</span>
session<span style="color:#666">.</span><span style="color:#b44">write</span><span style="color:#666">(</span>buffer<span style="color:#666">)</span><span style="color:#666">;</span>
<span style="color:#a2f;font-weight:bold">try</span> <span style="color:#666">{</span>
Thread<span style="color:#666">.</span><span style="color:#b44">sleep</span><span style="color:#666">(</span>1000<span style="color:#666">)</span><span style="color:#666">;</span>
<span style="color:#666">}</span> <span style="color:#a2f;font-weight:bold">catch</span> <span style="color:#666">(</span>InterruptedException e<span style="color:#666">)</span> <span style="color:#666">{</span>
e<span style="color:#666">.</span><span style="color:#b44">printStackTrace</span><span style="color:#666">(</span><span style="color:#666">)</span><span style="color:#666">;</span>
<span style="color:#a2f;font-weight:bold">throw</span> <span style="color:#a2f;font-weight:bold">new</span> InterruptedException<span style="color:#666">(</span>e<span style="color:#666">.</span><span style="color:#b44">getMessage</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>
<span style="color:#666">}</span>
</code></pre></div><p>This method will write the amount of free memory to the server once a second for 30 seconds. Here you can see that we allocate a IoBuffer large enough to hold a long variable and then place the amount of free memory in the buffer. This buffer is then flipped and written to the server.</p>
<p>Our UDP Client implementation is complete.</p>
<div class="nav">
<div class="nav_prev">
<a href="ch2.4-sample-udp-server.html">2.4 - Sample UDP Server</a>
</div>
<div class="nav_up">
<a href="ch2-basics.html">Chapter 2 - Basics</a>
</div>
<div class="nav_next">
<a href="ch2.6-summary.html">2.6 - Summary</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>