Sophie

Sophie

distrib > Fedora > 14 > x86_64 > media > os > by-pkgid > 9c481c872fb0ae0cbadccdc2f4a18ab6 > files > 124

libasync-devel-0.17.0-5.fc14.x86_64.rpm

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<title>Async: AsyncTcpServer_demo.cpp</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="doxygen.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<!-- Generated by Doxygen 1.6.2-20100208 -->
<div class="navigation" id="top">
  <div class="tabs">
    <ul>
      <li><a href="index.html"><span>Main&nbsp;Page</span></a></li>
      <li><a href="namespaces.html"><span>Namespaces</span></a></li>
      <li><a href="annotated.html"><span>Classes</span></a></li>
      <li><a href="files.html"><span>Files</span></a></li>
      <li><a href="dirs.html"><span>Directories</span></a></li>
      <li><a href="examples.html"><span>Examples</span></a></li>
    </ul>
  </div>
</div>
<div class="contents">
<h1>AsyncTcpServer_demo.cpp</h1><p>An example of how to use the <a class="el" href="classAsync_1_1TcpServer.html" title="A class for creating a TCP server.">Async::TcpServer</a> class</p>
<div class="fragment"><pre class="fragment"><span class="preprocessor">#include &lt;iostream&gt;</span>
<span class="preprocessor">#include &lt;<a class="code" href="AsyncCppApplication_8h.html" title="The core class for writing asyncronous cpp applications.">AsyncCppApplication.h</a>&gt;</span>
<span class="preprocessor">#include &lt;<a class="code" href="AsyncTcpServer_8h.html" title="A class for creating a TCP server.">AsyncTcpServer.h</a>&gt;</span>

<span class="keyword">using namespace </span>std;
<span class="keyword">using namespace </span>Async;

<span class="keyword">class </span>MyClass : <span class="keyword">public</span> SigC::Object
{
  <span class="keyword">public</span>:
    MyClass(<span class="keywordtype">void</span>)
    {
      server = <span class="keyword">new</span> <a name="_a0"></a><a class="code" href="classAsync_1_1TcpServer.html" title="A class for creating a TCP server.">TcpServer</a>(<span class="stringliteral">&quot;12345&quot;</span>);
      server-&gt;clientConnected.connect(slot(*<span class="keyword">this</span>, &amp;MyClass::onClientConnected));
      server-&gt;clientDisconnected.connect(
              slot(*<span class="keyword">this</span>, &amp;MyClass::onClientDisconnected));
      cout &lt;&lt; <span class="stringliteral">&quot;Connect using: \&quot;telnet localhost 12345\&quot; from &quot;</span>
              <span class="stringliteral">&quot;another console\n&quot;</span>;
    }
    
    ~MyClass(<span class="keywordtype">void</span>)
    {
      <span class="keyword">delete</span> server;
    }

  <span class="keyword">private</span>:
    <a class="code" href="classAsync_1_1TcpServer.html" title="A class for creating a TCP server.">TcpServer</a> *server;
    
    <span class="keywordtype">void</span> onClientConnected(<a name="_a1"></a><a class="code" href="classAsync_1_1TcpConnection.html" title="A class for handling exiting TCP connections.">TcpConnection</a> *con)
    {
      cout &lt;&lt; <span class="stringliteral">&quot;Client &quot;</span> &lt;&lt; con-&gt;<a name="a2"></a><a class="code" href="classAsync_1_1TcpConnection.html#a4072ea8aef1bdd369bacba2367e8660d" title="Return the IP-address of the remote host.">remoteHost</a>() &lt;&lt; <span class="stringliteral">&quot;:&quot;</span>
           &lt;&lt; con-&gt;<a name="a3"></a><a class="code" href="classAsync_1_1TcpConnection.html#acc4f658b73f32acc85c79671c2f50676" title="Return the remote port used.">remotePort</a>() &lt;&lt; <span class="stringliteral">&quot; connected, &quot;</span>
           &lt;&lt; server-&gt;numberOfClients() &lt;&lt; <span class="stringliteral">&quot; clients connected\n&quot;</span>;
        <span class="comment">// We need ONLY to add signal for receive data to the TcpConnection</span>
      con-&gt;<a name="a4"></a><a class="code" href="classAsync_1_1TcpConnection.html#a833455297abb96d1f414bbeca7a19d96" title="A signal that is emitted when data has been received on the connection.">dataReceived</a>.connect(slot(*<span class="keyword">this</span>, &amp;MyClass::onDataReceived));
        <span class="comment">// Send welcome message to the connected client */</span>
      con-&gt;<a name="a5"></a><a class="code" href="classAsync_1_1TcpConnection.html#a9c941f8e1ce95b2d6419f42b92cc32a6" title="Write data to the TCP connection.">write</a>(<span class="stringliteral">&quot;Hello, client!\n&quot;</span>, 15);
    }
    
    <span class="keywordtype">void</span> onClientDisconnected(<a class="code" href="classAsync_1_1TcpConnection.html" title="A class for handling exiting TCP connections.">TcpConnection</a> *con,
                              <a class="code" href="classAsync_1_1TcpConnection.html#a2a09caf85797c567d0cfce6f29ab9dd4" title="Reason code for disconnects.">TcpConnection::DisconnectReason</a> reason)
    {
      cout &lt;&lt; <span class="stringliteral">&quot;Client &quot;</span> &lt;&lt; con-&gt;<a class="code" href="classAsync_1_1TcpConnection.html#a4072ea8aef1bdd369bacba2367e8660d" title="Return the IP-address of the remote host.">remoteHost</a>().<a name="a6"></a><a class="code" href="classAsync_1_1IpAddress.html#a7f9be3568547648ef7d2d73cd561dc45" title="Return the string representation of the IP address.">toString</a>() &lt;&lt; <span class="stringliteral">&quot;:&quot;</span>
           &lt;&lt; con-&gt;<a class="code" href="classAsync_1_1TcpConnection.html#acc4f658b73f32acc85c79671c2f50676" title="Return the remote port used.">remotePort</a>() &lt;&lt; <span class="stringliteral">&quot; disconnected,&quot;</span>
           &lt;&lt; server-&gt;numberOfClients() &lt;&lt; <span class="stringliteral">&quot; clients connected\n&quot;</span>;
      <span class="comment">/* Don&#39;t delete the con object, the TcpServer will do it */</span>
    }
    
    <span class="keywordtype">int</span> onDataReceived(<a class="code" href="classAsync_1_1TcpConnection.html" title="A class for handling exiting TCP connections.">TcpConnection</a> *con, <span class="keywordtype">void</span> *buf, <span class="keywordtype">int</span> count)
    {
        <span class="comment">// retreive data</span>
      <span class="keywordtype">char</span> *str = <span class="keyword">static_cast&lt;</span><span class="keywordtype">char</span> *<span class="keyword">&gt;</span>(buf);
      <span class="keywordtype">string</span> data(str, str+count);
      cout &lt;&lt; data;
      
        <span class="comment">// Send data back to sender</span>
      <span class="keywordtype">string</span> dataOut = string(<span class="stringliteral">&quot;You said: &quot;</span>) + data;
      server-&gt;writeOnly(con, dataOut.c_str(), dataOut.size());
      
        <span class="comment">// Other way to send to sender</span>
      <span class="comment">//con-&gt;write(dataOut.c_str(), dataOut.size());</span>
      
        <span class="comment">// Send to other clients if there is more then one connected to server</span>
      <span class="keywordflow">if</span> (server-&gt;numberOfClients() &gt; 1)
      {
          <span class="comment">// Send data back to all OTHER clients</span>
        dataOut = string(<span class="stringliteral">&quot;He said : &quot;</span>) + data;
        server-&gt;writeExcept(con, dataOut.c_str(), dataOut.size());
        
          <span class="comment">// Send data back to all clients</span>
        dataOut = string(<span class="stringliteral">&quot;To all  : &quot;</span>) + data;
        server-&gt;writeAll(dataOut.c_str(), dataOut.size());
      }
      <span class="keywordflow">return</span> count;
    }
};

<span class="keywordtype">int</span> main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> **argv)
{
  <a name="_a7"></a><a class="code" href="classAsync_1_1CppApplication.html" title="An application class for writing non GUI applications.">CppApplication</a> app;
  MyClass my_class;
  app.<a name="a8"></a><a class="code" href="classAsync_1_1CppApplication.html#a9a921d33644bbb18767fb4d599f53bf4" title="Execute the application main loop.">exec</a>();
}


</pre></div> </div>
<hr class="footer"/><address style="text-align: right;"><small>Generated by&nbsp;
<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.6.2-20100208 </small></address>
</body>
</html>