<!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 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 <iostream></span> <span class="preprocessor">#include <<a class="code" href="AsyncCppApplication_8h.html" title="The core class for writing asyncronous cpp applications.">AsyncCppApplication.h</a>></span> <span class="preprocessor">#include <<a class="code" href="AsyncTcpServer_8h.html" title="A class for creating a TCP server.">AsyncTcpServer.h</a>></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">"12345"</span>); server->clientConnected.connect(slot(*<span class="keyword">this</span>, &MyClass::onClientConnected)); server->clientDisconnected.connect( slot(*<span class="keyword">this</span>, &MyClass::onClientDisconnected)); cout << <span class="stringliteral">"Connect using: \"telnet localhost 12345\" from "</span> <span class="stringliteral">"another console\n"</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 << <span class="stringliteral">"Client "</span> << con-><a name="a2"></a><a class="code" href="classAsync_1_1TcpConnection.html#a4072ea8aef1bdd369bacba2367e8660d" title="Return the IP-address of the remote host.">remoteHost</a>() << <span class="stringliteral">":"</span> << con-><a name="a3"></a><a class="code" href="classAsync_1_1TcpConnection.html#acc4f658b73f32acc85c79671c2f50676" title="Return the remote port used.">remotePort</a>() << <span class="stringliteral">" connected, "</span> << server->numberOfClients() << <span class="stringliteral">" clients connected\n"</span>; <span class="comment">// We need ONLY to add signal for receive data to the TcpConnection</span> con-><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>, &MyClass::onDataReceived)); <span class="comment">// Send welcome message to the connected client */</span> con-><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">"Hello, client!\n"</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 << <span class="stringliteral">"Client "</span> << con-><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>() << <span class="stringliteral">":"</span> << con-><a class="code" href="classAsync_1_1TcpConnection.html#acc4f658b73f32acc85c79671c2f50676" title="Return the remote port used.">remotePort</a>() << <span class="stringliteral">" disconnected,"</span> << server->numberOfClients() << <span class="stringliteral">" clients connected\n"</span>; <span class="comment">/* Don'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<</span><span class="keywordtype">char</span> *<span class="keyword">></span>(buf); <span class="keywordtype">string</span> data(str, str+count); cout << data; <span class="comment">// Send data back to sender</span> <span class="keywordtype">string</span> dataOut = string(<span class="stringliteral">"You said: "</span>) + data; server->writeOnly(con, dataOut.c_str(), dataOut.size()); <span class="comment">// Other way to send to sender</span> <span class="comment">//con->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->numberOfClients() > 1) { <span class="comment">// Send data back to all OTHER clients</span> dataOut = string(<span class="stringliteral">"He said : "</span>) + data; server->writeExcept(con, dataOut.c_str(), dataOut.size()); <span class="comment">// Send data back to all clients</span> dataOut = string(<span class="stringliteral">"To all : "</span>) + data; server->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 <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>