<!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: Async::TcpServer Class Reference</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 class="current"><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 class="tabs"> <ul> <li><a href="annotated.html"><span>Class List</span></a></li> <li><a href="hierarchy.html"><span>Class Hierarchy</span></a></li> <li><a href="functions.html"><span>Class Members</span></a></li> </ul> </div> <div class="navpath"><a class="el" href="namespaceAsync.html">Async</a>::<a class="el" href="classAsync_1_1TcpServer.html">TcpServer</a> </div> </div> <div class="contents"> <h1>Async::TcpServer Class Reference</h1><!-- doxytag: class="Async::TcpServer" --> <p>A class for creating a TCP server. <a href="#_details">More...</a></p> <p><code>#include <<a class="el" href="AsyncTcpServer_8h_source.html">AsyncTcpServer.h</a>></code></p> <p><a href="classAsync_1_1TcpServer-members.html">List of all members.</a></p> <h2>Public Member Functions</h2> <ul> <li><a class="el" href="classAsync_1_1TcpServer.html#a8ae806bbbaf3fc1cd05fc86bbfe96e26">TcpServer</a> (const std::string &port_str) <dl class="el"><dd class="mdescRight">Default constuctor. <a href="#a8ae806bbbaf3fc1cd05fc86bbfe96e26"></a><br/></dl><li><a class="el" href="classAsync_1_1TcpServer.html#a2bd82e664fb4a95de4c2f8f4b1255036">~TcpServer</a> (void) <dl class="el"><dd class="mdescRight">Destructor. <a href="#a2bd82e664fb4a95de4c2f8f4b1255036"></a><br/></dl><li>int <a class="el" href="classAsync_1_1TcpServer.html#a23cf0c4145a89cc511696839b93362ba">numberOfClients</a> (void) <dl class="el"><dd class="mdescRight">Get the number of clients that is connected to the server. <a href="#a23cf0c4145a89cc511696839b93362ba"></a><br/></dl><li><a class="el" href="classAsync_1_1TcpConnection.html">TcpConnection</a> * <a class="el" href="classAsync_1_1TcpServer.html#a8e34345330b4c3ee1145548e8ad0a507">getClient</a> (unsigned int index) <dl class="el"><dd class="mdescRight">Get the client object pointer from the server. <a href="#a8e34345330b4c3ee1145548e8ad0a507"></a><br/></dl><li>int <a class="el" href="classAsync_1_1TcpServer.html#ae4fdfc3de5e8fda88f25ec4ba37704f7">writeAll</a> (const void *buf, int count) <dl class="el"><dd class="mdescRight">Write data to all connected clients. <a href="#ae4fdfc3de5e8fda88f25ec4ba37704f7"></a><br/></dl><li>int <a class="el" href="classAsync_1_1TcpServer.html#a2ddd6ab3e8fc46dff52fdc71d9da4a9e">writeOnly</a> (<a class="el" href="classAsync_1_1TcpConnection.html">TcpConnection</a> *con, const void *buf, int count) <dl class="el"><dd class="mdescRight">Send data only to the given client. <a href="#a2ddd6ab3e8fc46dff52fdc71d9da4a9e"></a><br/></dl><li>int <a class="el" href="classAsync_1_1TcpServer.html#a2f7cd9ee896ec49246034239fb96953f">writeExcept</a> (<a class="el" href="classAsync_1_1TcpConnection.html">TcpConnection</a> *con, const void *buf, int count) <dl class="el"><dd class="mdescRight">Send data to all connected clients except the given client. <a href="#a2f7cd9ee896ec49246034239fb96953f"></a><br/></dl></ul> <h2>Public Attributes</h2> <ul> <li>SigC::Signal1< void, <br class="typebreak"/> <a class="el" href="classAsync_1_1TcpConnection.html">TcpConnection</a> * > <a class="el" href="classAsync_1_1TcpServer.html#a63b872e90489bd89f318b29e3a616fc8">clientConnected</a> <dl class="el"><dd class="mdescRight">A signal that is emitted when a client connect to the server. <a href="#a63b872e90489bd89f318b29e3a616fc8"></a><br/></dl><li>SigC::Signal2< void, <br class="typebreak"/> <a class="el" href="classAsync_1_1TcpConnection.html">TcpConnection</a> <br class="typebreak"/> *, <a class="el" href="classAsync_1_1TcpConnection.html#a2a09caf85797c567d0cfce6f29ab9dd4">TcpConnection::DisconnectReason</a> > <a class="el" href="classAsync_1_1TcpServer.html#afea119a0c83b4b62292ee8b27624620a">clientDisconnected</a> <dl class="el"><dd class="mdescRight">A signal that is emitted when a client disconnect from the server. <a href="#afea119a0c83b4b62292ee8b27624620a"></a><br/></dl></ul> <hr/><a name="_details"></a><h2>Detailed Description</h2> <p>A class for creating a TCP server. </p> <dl class="author"><dt><b>Author:</b></dt><dd>Tobias Blomberg </dd></dl> <dl class="date"><dt><b>Date:</b></dt><dd>2003-12-07</dd></dl> <p>This class is used to create a TCP server that listens to a TCP-port. To use it, just create an instance and specify the TCP-port to listen to. When a client connects, a new <a class="el" href="classAsync_1_1TcpConnection.html" title="A class for handling exiting TCP connections.">Async::TcpConnection</a> object is created which is used to do the actual communication. An example of how to use it is shown below.</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 class="code" href="classAsync_1_1TcpServer.html#a8ae806bbbaf3fc1cd05fc86bbfe96e26" title="Default constuctor.">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#a8ae806bbbaf3fc1cd05fc86bbfe96e26" title="Default constuctor.">TcpServer</a> *server; <span class="keywordtype">void</span> onClientConnected(TcpConnection *con) { cout << <span class="stringliteral">"Client "</span> << con->remoteHost() << <span class="stringliteral">":"</span> << con->remotePort() << <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->dataReceived.connect(slot(*<span class="keyword">this</span>, &MyClass::onDataReceived)); <span class="comment">// Send welcome message to the connected client */</span> con->write(<span class="stringliteral">"Hello, client!\n"</span>, 15); } <span class="keywordtype">void</span> onClientDisconnected(TcpConnection *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->remoteHost().toString() << <span class="stringliteral">":"</span> << con->remotePort() << <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(TcpConnection *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) { CppApplication app; MyClass my_class; app.exec(); } </pre></div> <dl><dt><b>Examples: </b></dt><dd> <p><a class="el" href="AsyncTcpServer__demo_8cpp-example.html#_a0">AsyncTcpServer_demo.cpp</a>.</p> </dd> </dl> <p>Definition at line <a class="el" href="AsyncTcpServer_8h_source.html#l00126">126</a> of file <a class="el" href="AsyncTcpServer_8h_source.html">AsyncTcpServer.h</a>.</p> <hr/><h2>Constructor & Destructor Documentation</h2> <a class="anchor" id="a8ae806bbbaf3fc1cd05fc86bbfe96e26"></a><!-- doxytag: member="Async::TcpServer::TcpServer" ref="a8ae806bbbaf3fc1cd05fc86bbfe96e26" args="(const std::string &port_str)" --> <div class="memitem"> <div class="memproto"> <table class="memname"> <tr> <td class="memname">Async::TcpServer::TcpServer </td> <td>(</td> <td class="paramtype">const std::string & </td> <td class="paramname"> <em>port_str</em></td> <td> ) </td> <td></td> </tr> </table> </div> <div class="memdoc"> <p>Default constuctor. </p> <dl><dt><b>Parameters:</b></dt><dd> <table border="0" cellspacing="2" cellpadding="0"> <tr><td valign="top"></td><td valign="top"><em>port_str</em> </td><td>A port number or service name to listen to </td></tr> </table> </dd> </dl> </div> </div> <a class="anchor" id="a2bd82e664fb4a95de4c2f8f4b1255036"></a><!-- doxytag: member="Async::TcpServer::~TcpServer" ref="a2bd82e664fb4a95de4c2f8f4b1255036" args="(void)" --> <div class="memitem"> <div class="memproto"> <table class="memname"> <tr> <td class="memname">Async::TcpServer::~TcpServer </td> <td>(</td> <td class="paramtype">void </td> <td class="paramname"></td> <td> ) </td> <td></td> </tr> </table> </div> <div class="memdoc"> <p>Destructor. </p> </div> </div> <hr/><h2>Member Function Documentation</h2> <a class="anchor" id="a8e34345330b4c3ee1145548e8ad0a507"></a><!-- doxytag: member="Async::TcpServer::getClient" ref="a8e34345330b4c3ee1145548e8ad0a507" args="(unsigned int index)" --> <div class="memitem"> <div class="memproto"> <table class="memname"> <tr> <td class="memname"><a class="el" href="classAsync_1_1TcpConnection.html">TcpConnection</a>* Async::TcpServer::getClient </td> <td>(</td> <td class="paramtype">unsigned int </td> <td class="paramname"> <em>index</em></td> <td> ) </td> <td></td> </tr> </table> </div> <div class="memdoc"> <p>Get the client object pointer from the server. </p> <dl><dt><b>Parameters:</b></dt><dd> <table border="0" cellspacing="2" cellpadding="0"> <tr><td valign="top"></td><td valign="top"><em>index</em> </td><td>The wanted client by number 0 - <a class="el" href="classAsync_1_1TcpServer.html#a23cf0c4145a89cc511696839b93362ba" title="Get the number of clients that is connected to the server.">numberOfClients()</a>-1 </td></tr> </table> </dd> </dl> <dl class="return"><dt><b>Returns:</b></dt><dd>The <a class="el" href="classAsync_1_1TcpConnection.html" title="A class for handling exiting TCP connections.">TcpConnection</a> pointer to the client (zero if not found) </dd></dl> </div> </div> <a class="anchor" id="a23cf0c4145a89cc511696839b93362ba"></a><!-- doxytag: member="Async::TcpServer::numberOfClients" ref="a23cf0c4145a89cc511696839b93362ba" args="(void)" --> <div class="memitem"> <div class="memproto"> <table class="memname"> <tr> <td class="memname">int Async::TcpServer::numberOfClients </td> <td>(</td> <td class="paramtype">void </td> <td class="paramname"></td> <td> ) </td> <td></td> </tr> </table> </div> <div class="memdoc"> <p>Get the number of clients that is connected to the server. </p> <dl class="return"><dt><b>Returns:</b></dt><dd>The number of connected clients </dd></dl> </div> </div> <a class="anchor" id="ae4fdfc3de5e8fda88f25ec4ba37704f7"></a><!-- doxytag: member="Async::TcpServer::writeAll" ref="ae4fdfc3de5e8fda88f25ec4ba37704f7" args="(const void *buf, int count)" --> <div class="memitem"> <div class="memproto"> <table class="memname"> <tr> <td class="memname">int Async::TcpServer::writeAll </td> <td>(</td> <td class="paramtype">const void * </td> <td class="paramname"> <em>buf</em>, </td> </tr> <tr> <td class="paramkey"></td> <td></td> <td class="paramtype">int </td> <td class="paramname"> <em>count</em></td><td> </td> </tr> <tr> <td></td> <td>)</td> <td></td><td></td><td></td> </tr> </table> </div> <div class="memdoc"> <p>Write data to all connected clients. </p> <dl><dt><b>Parameters:</b></dt><dd> <table border="0" cellspacing="2" cellpadding="0"> <tr><td valign="top"></td><td valign="top"><em>buf</em> </td><td>The data buffer </td></tr> <tr><td valign="top"></td><td valign="top"><em>count</em> </td><td>The number of bytes in the data buffer </td></tr> </table> </dd> </dl> <dl class="return"><dt><b>Returns:</b></dt><dd>The number of bytes sent </dd></dl> </div> </div> <a class="anchor" id="a2f7cd9ee896ec49246034239fb96953f"></a><!-- doxytag: member="Async::TcpServer::writeExcept" ref="a2f7cd9ee896ec49246034239fb96953f" args="(TcpConnection *con, const void *buf, int count)" --> <div class="memitem"> <div class="memproto"> <table class="memname"> <tr> <td class="memname">int Async::TcpServer::writeExcept </td> <td>(</td> <td class="paramtype"><a class="el" href="classAsync_1_1TcpConnection.html">TcpConnection</a> * </td> <td class="paramname"> <em>con</em>, </td> </tr> <tr> <td class="paramkey"></td> <td></td> <td class="paramtype">const void * </td> <td class="paramname"> <em>buf</em>, </td> </tr> <tr> <td class="paramkey"></td> <td></td> <td class="paramtype">int </td> <td class="paramname"> <em>count</em></td><td> </td> </tr> <tr> <td></td> <td>)</td> <td></td><td></td><td></td> </tr> </table> </div> <div class="memdoc"> <p>Send data to all connected clients except the given client. </p> <dl><dt><b>Parameters:</b></dt><dd> <table border="0" cellspacing="2" cellpadding="0"> <tr><td valign="top"></td><td valign="top"><em>con</em> </td><td>The <a class="el" href="classAsync_1_1TcpConnection.html" title="A class for handling exiting TCP connections.">TcpConnection</a> object not to send to </td></tr> <tr><td valign="top"></td><td valign="top"><em>buf</em> </td><td>The data buffer </td></tr> <tr><td valign="top"></td><td valign="top"><em>count</em> </td><td>The number of bytes in the data buffer </td></tr> </table> </dd> </dl> <dl class="return"><dt><b>Returns:</b></dt><dd>The number of bytes sent </dd></dl> </div> </div> <a class="anchor" id="a2ddd6ab3e8fc46dff52fdc71d9da4a9e"></a><!-- doxytag: member="Async::TcpServer::writeOnly" ref="a2ddd6ab3e8fc46dff52fdc71d9da4a9e" args="(TcpConnection *con, const void *buf, int count)" --> <div class="memitem"> <div class="memproto"> <table class="memname"> <tr> <td class="memname">int Async::TcpServer::writeOnly </td> <td>(</td> <td class="paramtype"><a class="el" href="classAsync_1_1TcpConnection.html">TcpConnection</a> * </td> <td class="paramname"> <em>con</em>, </td> </tr> <tr> <td class="paramkey"></td> <td></td> <td class="paramtype">const void * </td> <td class="paramname"> <em>buf</em>, </td> </tr> <tr> <td class="paramkey"></td> <td></td> <td class="paramtype">int </td> <td class="paramname"> <em>count</em></td><td> </td> </tr> <tr> <td></td> <td>)</td> <td></td><td></td><td></td> </tr> </table> </div> <div class="memdoc"> <p>Send data only to the given client. </p> <dl><dt><b>Parameters:</b></dt><dd> <table border="0" cellspacing="2" cellpadding="0"> <tr><td valign="top"></td><td valign="top"><em>con</em> </td><td>The <a class="el" href="classAsync_1_1TcpConnection.html" title="A class for handling exiting TCP connections.">TcpConnection</a> object to send to </td></tr> <tr><td valign="top"></td><td valign="top"><em>buf</em> </td><td>The data buffer </td></tr> <tr><td valign="top"></td><td valign="top"><em>count</em> </td><td>The number of bytes in data buffer </td></tr> </table> </dd> </dl> <dl class="return"><dt><b>Returns:</b></dt><dd>The number of bytes sent </dd></dl> </div> </div> <hr/><h2>Member Data Documentation</h2> <a class="anchor" id="a63b872e90489bd89f318b29e3a616fc8"></a><!-- doxytag: member="Async::TcpServer::clientConnected" ref="a63b872e90489bd89f318b29e3a616fc8" args="" --> <div class="memitem"> <div class="memproto"> <table class="memname"> <tr> <td class="memname">SigC::Signal1<void, <a class="el" href="classAsync_1_1TcpConnection.html">TcpConnection</a> *> <a class="el" href="classAsync_1_1TcpServer.html#a63b872e90489bd89f318b29e3a616fc8">Async::TcpServer::clientConnected</a></td> </tr> </table> </div> <div class="memdoc"> <p>A signal that is emitted when a client connect to the server. </p> <dl><dt><b>Parameters:</b></dt><dd> <table border="0" cellspacing="2" cellpadding="0"> <tr><td valign="top"></td><td valign="top"><em>con</em> </td><td>The connected <a class="el" href="classAsync_1_1TcpConnection.html" title="A class for handling exiting TCP connections.">TcpConnection</a> object </td></tr> </table> </dd> </dl> <p>Definition at line <a class="el" href="AsyncTcpServer_8h_source.html#l00183">183</a> of file <a class="el" href="AsyncTcpServer_8h_source.html">AsyncTcpServer.h</a>.</p> </div> </div> <a class="anchor" id="afea119a0c83b4b62292ee8b27624620a"></a><!-- doxytag: member="Async::TcpServer::clientDisconnected" ref="afea119a0c83b4b62292ee8b27624620a" args="" --> <div class="memitem"> <div class="memproto"> <table class="memname"> <tr> <td class="memname">SigC::Signal2<void, <a class="el" href="classAsync_1_1TcpConnection.html">TcpConnection</a> *,<a class="el" href="classAsync_1_1TcpConnection.html#a2a09caf85797c567d0cfce6f29ab9dd4">TcpConnection::DisconnectReason</a>> <a class="el" href="classAsync_1_1TcpServer.html#afea119a0c83b4b62292ee8b27624620a">Async::TcpServer::clientDisconnected</a></td> </tr> </table> </div> <div class="memdoc"> <p>A signal that is emitted when a client disconnect from the server. </p> <dl><dt><b>Parameters:</b></dt><dd> <table border="0" cellspacing="2" cellpadding="0"> <tr><td valign="top"></td><td valign="top"><em>con</em> </td><td>The disconnected <a class="el" href="classAsync_1_1TcpConnection.html" title="A class for handling exiting TCP connections.">TcpConnection</a> object </td></tr> </table> </dd> </dl> <p>Definition at line <a class="el" href="AsyncTcpServer_8h_source.html#l00191">191</a> of file <a class="el" href="AsyncTcpServer_8h_source.html">AsyncTcpServer.h</a>.</p> </div> </div> <hr/>The documentation for this class was generated from the following file:<ul> <li><a class="el" href="AsyncTcpServer_8h_source.html">AsyncTcpServer.h</a></li> </ul> </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>