blob: 12cf48f62239ea2d5c80c3fec2a271960ef40c46 [file] [log] [blame]
<!DOCTYPE html>
<html lang="en">
<head>
<title>2.3 - Sample TCP 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.2-sample-tcp-server.html">2.2 - Sample TCP Server</a>
</div>
<div class="nav_up">
<a href="ch2-basics.html">Chapter 2 - Basics</a>
</div>
<div class="nav_next">
<a href="ch2.4-sample-udp-server.html">2.4 - Sample UDP Server</a>
</div>
<div class="clearfix"></div>
</div>
<h1 id="23---sample-tcp-client">2.3 - Sample TCP Client</h1>
<p>We have seen the Client Architecture. Lets explore a sample Client implementation.</p>
<p>We shall use <a href="https://nightlies.apache.org/mina/mina/2.0.22/xref/org/apache/mina/example/sumup/Client.html">Sumup Client</a> as a reference implementation.</p>
<p>We will remove boiler plate code and concentrate on the important constructs. Below the code for the Client :</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">public</span> <span style="color:#a2f;font-weight:bold">static</span> <span style="color:#0b0;font-weight:bold">void</span> <span style="color:#00a000">main</span><span style="color:#666">(</span>String<span style="color:#666">[</span><span style="color:#666">]</span> args<span style="color:#666">)</span> <span style="color:#a2f;font-weight:bold">throws</span> Throwable <span style="color:#666">{</span>
NioSocketConnector connector <span style="color:#666">=</span> <span style="color:#a2f;font-weight:bold">new</span> NioSocketConnector<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">setConnectTimeoutMillis</span><span style="color:#666">(</span>CONNECT_TIMEOUT<span style="color:#666">)</span><span style="color:#666">;</span>
<span style="color:#a2f;font-weight:bold">if</span> <span style="color:#666">(</span>USE_CUSTOM_CODEC<span style="color:#666">)</span> <span style="color:#666">{</span>
connector<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><span style="color:#b44">addLast</span><span style="color:#666">(</span><span style="color:#b44">&#34;codec&#34;</span><span style="color:#666">,</span>
<span style="color:#a2f;font-weight:bold">new</span> ProtocolCodecFilter<span style="color:#666">(</span><span style="color:#a2f;font-weight:bold">new</span> SumUpProtocolCodecFactory<span style="color:#666">(</span><span style="color:#a2f;font-weight:bold">false</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>
connector<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><span style="color:#b44">addLast</span><span style="color:#666">(</span><span style="color:#b44">&#34;codec&#34;</span><span style="color:#666">,</span>
<span style="color:#a2f;font-weight:bold">new</span> ProtocolCodecFilter<span style="color:#666">(</span><span style="color:#a2f;font-weight:bold">new</span> ObjectSerializationCodecFactory<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>
connector<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><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>
connector<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> ClientSessionHandler<span style="color:#666">(</span>values<span style="color:#666">)</span><span style="color:#666">)</span><span style="color:#666">;</span>
IoSession session<span style="color:#666">;</span>
<span style="color:#a2f;font-weight:bold">for</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">try</span> <span style="color:#666">{</span>
ConnectFuture future <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>HOSTNAME<span style="color:#666">,</span> PORT<span style="color:#666">)</span><span style="color:#666">)</span><span style="color:#666">;</span>
future<span style="color:#666">.</span><span style="color:#b44">awaitUninterruptibly</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">break</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>RuntimeIoException e<span style="color:#666">)</span> <span style="color:#666">{</span>
System<span style="color:#666">.</span><span style="color:#b44">err</span><span style="color:#666">.</span><span style="color:#b44">println</span><span style="color:#666">(</span><span style="color:#b44">&#34;Failed to connect.&#34;</span><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>
Thread<span style="color:#666">.</span><span style="color:#b44">sleep</span><span style="color:#666">(</span>5000<span style="color:#666">)</span><span style="color:#666">;</span>
<span style="color:#666">}</span>
<span style="color:#666">}</span>
<span style="color:#080;font-style:italic">// wait until the summation is done
</span><span style="color:#080;font-style:italic"></span> session<span style="color:#666">.</span><span style="color:#b44">getCloseFuture</span><span style="color:#666">(</span><span style="color:#666">)</span><span style="color:#666">.</span><span style="color:#b44">awaitUninterruptibly</span><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">dispose</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>To construct a Client, we need to do following</p>
<ul>
<li>Create a Connector</li>
<li>Create a Filter Chain</li>
<li>Create a IOHandler and add to Connector</li>
<li>Bind to Server</li>
</ul>
<p>Lets examine each one in detail</p>
<h2 id="create-a-connector">Create a Connector</h2>
<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">NioSocketConnector connector <span style="color:#666">=</span> <span style="color:#a2f;font-weight:bold">new</span> NioSocketConnector<span style="color:#666">(</span><span style="color:#666">)</span><span style="color:#666">;</span>
</code></pre></div><p>Here we have created a NIO Socket connector</p>
<h2 id="create-a-filter-chain">Create a Filter Chain</h2>
<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">if</span> <span style="color:#666">(</span>USE_CUSTOM_CODEC<span style="color:#666">)</span> <span style="color:#666">{</span>
connector<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><span style="color:#b44">addLast</span><span style="color:#666">(</span><span style="color:#b44">&#34;codec&#34;</span><span style="color:#666">,</span>
<span style="color:#a2f;font-weight:bold">new</span> ProtocolCodecFilter<span style="color:#666">(</span><span style="color:#a2f;font-weight:bold">new</span> SumUpProtocolCodecFactory<span style="color:#666">(</span><span style="color:#a2f;font-weight:bold">false</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>
connector<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><span style="color:#b44">addLast</span><span style="color:#666">(</span><span style="color:#b44">&#34;codec&#34;</span><span style="color:#666">,</span>
<span style="color:#a2f;font-weight:bold">new</span> ProtocolCodecFilter<span style="color:#666">(</span><span style="color:#a2f;font-weight:bold">new</span> ObjectSerializationCodecFactory<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>We add Filters to the Filter Chain for the Connector. Here we have added a ProtocolCodec, to the filter Chain.</p>
<h2 id="create-iohandler">Create IOHandler</h2>
<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:#b44">setHandler</span><span style="color:#666">(</span><span style="color:#a2f;font-weight:bold">new</span> ClientSessionHandler<span style="color:#666">(</span>values<span style="color:#666">)</span><span style="color:#666">)</span><span style="color:#666">;</span>
</code></pre></div><p>Here we create an instance of <a href="https://nightlies.apache.org/mina/mina/2.0.22/xref/org/apache/mina/example/sumup/ClientSessionHandler.html">ClientSessionHandler</a> and set it as a handler for the Connector.</p>
<h2 id="bind-to-server">Bind to Server</h2>
<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">IoSession session<span style="color:#666">;</span>
<span style="color:#a2f;font-weight:bold">for</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">try</span> <span style="color:#666">{</span>
ConnectFuture future <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>HOSTNAME<span style="color:#666">,</span> PORT<span style="color:#666">)</span><span style="color:#666">)</span><span style="color:#666">;</span>
future<span style="color:#666">.</span><span style="color:#b44">awaitUninterruptibly</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">break</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>RuntimeIoException e<span style="color:#666">)</span> <span style="color:#666">{</span>
System<span style="color:#666">.</span><span style="color:#b44">err</span><span style="color:#666">.</span><span style="color:#b44">println</span><span style="color:#666">(</span><span style="color:#b44">&#34;Failed to connect.&#34;</span><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>
Thread<span style="color:#666">.</span><span style="color:#b44">sleep</span><span style="color:#666">(</span>5000<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 is the most important stuff. We connect to remote Server. Since, connect is an async task, we use the <a href="https://nightlies.apache.org/mina/mina/2.0.22/xref/org/apache/mina/core/future/ConnectFuture.html">ConnectFuture</a> class to know the when the connection is complete.
Once the connection is complete, we get the associated <a href="https://nightlies.apache.org/mina/mina/2.0.22/xref/org/apache/mina/core/session/IoSession.html">IoSession</a>. To send any message to the Server, we shall have to write to the session. All responses/messages from server shall traverse the Filter chain and finally be handled in IoHandler.</p>
<div class="nav">
<div class="nav_prev">
<a href="ch2.2-sample-tcp-server.html">2.2 - Sample TCP Server</a>
</div>
<div class="nav_up">
<a href="ch2-basics.html">Chapter 2 - Basics</a>
</div>
<div class="nav_next">
<a href="ch2.4-sample-udp-server.html">2.4 - Sample UDP Server</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>