mina-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache MINA > Sample UDP Client
Date Sat, 16 Apr 2011 08:39:00 GMT
<html>
<head>
    <base href="https://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/2042/9/1/_/styles/combined.css?spaceKey=MINA&amp;forWysiwyg=true"
type="text/css">
    </head>
<body style="background: white;" bgcolor="white" class="email-body">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
    <h2><a href="https://cwiki.apache.org/confluence/display/MINA/Sample+UDP+Client">Sample
UDP Client</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://cwiki.apache.org/confluence/display/~elecharny">Emmanuel
L├ęcharny</a>
    </h4>
        <br/>
                         <h4>Changes (1)</h4>
                                 
    
<div id="page-diffs">
                    <table class="diff" cellpadding="0" cellspacing="0">
    
            <tr><td class="diff-added-lines" style="background-color: #dfd;">{scrollbar}
<br> <br></td></tr>
            <tr><td class="diff-unchanged" >h2. Sample UDP Client <br> <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
    
            </table>
    </div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <style type='text/css'>/*<![CDATA[*/
table.ScrollbarTable  {border: none;padding: 3px;width: 100%;padding: 3px;margin: 0px;background-color:
#f0f0f0}
table.ScrollbarTable td.ScrollbarPrevIcon {text-align: center;width: 16px;border: none;}
table.ScrollbarTable td.ScrollbarPrevName {text-align: left;border: none;}
table.ScrollbarTable td.ScrollbarParent {text-align: center;border: none;}
table.ScrollbarTable td.ScrollbarNextName {text-align: right;border: none;}
table.ScrollbarTable td.ScrollbarNextIcon {text-align: center;width: 16px;border: none;}

/*]]>*/</style><div class="Scrollbar"><table class='ScrollbarTable'><tr><td
class='ScrollbarPrevIcon'><a href="/confluence/display/MINA/Sample+UDP+Server"><img
border='0' align='middle' src='/confluence/images/icons/back_16.gif' width='16' height='16'></a></td><td
width='33%' class='ScrollbarPrevName'><a href="/confluence/display/MINA/Sample+UDP+Server">Sample
UDP Server</a>&nbsp;</td><td width='33%' class='ScrollbarParent'><sup><a
href="/confluence/display/MINA/Chapter+2+-+Basics"><img border='0' align='middle' src='/confluence/images/icons/up_16.gif'
width='8' height='8'></a></sup><a href="/confluence/display/MINA/Chapter+2+-+Basics">Chapter
2 - Basics</a></td><td width='33%' class='ScrollbarNextName'>&nbsp;<a
href="/confluence/display/MINA/Chapter+2+-+Summary">Chapter 2 - Summary</a></td><td
class='ScrollbarNextIcon'><a href="/confluence/display/MINA/Chapter+2+-+Summary"><img
border='0' align='middle' src='/confluence/images/icons/forwd_16.gif' width='16' height='16'></a></td></tr></table></div>

<h2><a name="SampleUDPClient-SampleUDPClient"></a>Sample UDP Client</h2>

<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="http://mina.apache.org/report/trunk/xref/org/apache/mina/example/udp/client/MemMonClient.html"
class="external-link" rel="nofollow">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="preformatted panel" style="border-width: 1px;"><div class="preformattedContent
panelContent">
<pre>connector = new NioDatagramConnector();
connector.setHandler( this );
ConnectFuture connFuture = connector.connect( new InetSocketAddress("localhost", MemoryMonitor.PORT
));
</pre>
</div></div>
<p>Here we create a <tt>NioDatagramConnector</tt>, 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="preformatted panel" style="border-width: 1px;"><div class="preformattedContent
panelContent">
<pre>connFuture.addListener( new IoFutureListener(){
            public void operationComplete(IoFuture future) {
                ConnectFuture connFuture = (ConnectFuture)future;
                if( connFuture.isConnected() ){
                    session = future.getSession();
                    try {
                        sendData();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                } else {
                    log.error("Not connected...exiting");
                }
            }
        });
</pre>
</div></div>
<p>Here we add a listener to the <tt>ConnectFuture</tt> 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="preformatted panel" style="border-width: 1px;"><div class="preformattedContent
panelContent">
<pre>private void sendData() throws InterruptedException {
        for (int i = 0; i &lt; 30; i++) {
            long free = Runtime.getRuntime().freeMemory();
            IoBuffer buffer = IoBuffer.allocate(8);
            buffer.putLong(free);
            buffer.flip();
            session.write(buffer);

            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
                throw new InterruptedException(e.getMessage());
            }
        }
}
</pre>
</div></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 <tt>IoBuffer</tt> 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>
        <div id="commentsSection" class="wiki-content pageSection">
        <div style="float: right;">
            <a href="https://cwiki.apache.org/confluence/users/viewnotifications.action"
class="grey">Change Notification Preferences</a>
        </div>
        <a href="https://cwiki.apache.org/confluence/display/MINA/Sample+UDP+Client">View
Online</a>
        |
        <a href="https://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=2854068&revisedVersion=2&originalVersion=1">View
Changes</a>
                |
        <a href="https://cwiki.apache.org/confluence/display/MINA/Sample+UDP+Client?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message