AsyncTcpServer_demo.cpp

An example of how to use the Async::TcpServer class

00001 #include <iostream>
00002 #include <AsyncCppApplication.h>
00003 #include <AsyncTcpServer.h>
00004 
00005 using namespace std;
00006 using namespace Async;
00007 
00008 class MyClass : public sigc::trackable
00009 {
00010   public:
00011     MyClass(void)
00012     {
00013       server = new TcpServer("12345");
00014       server->clientConnected.connect(
00015         mem_fun(*this, &MyClass::onClientConnected));
00016       server->clientDisconnected.connect(
00017               mem_fun(*this, &MyClass::onClientDisconnected));
00018       cout << "Connect using: \"telnet localhost 12345\" from "
00019               "another console\n";
00020     }
00021     
00022     ~MyClass(void)
00023     {
00024       delete server;
00025     }
00026 
00027   private:
00028     TcpServer *server;
00029     
00030     void onClientConnected(TcpConnection *con)
00031     {
00032       cout << "Client " << con->remoteHost() << ":"
00033            << con->remotePort() << " connected, "
00034            << server->numberOfClients() << " clients connected\n";
00035         // We need ONLY to add signal for receive data to the TcpConnection
00036       con->dataReceived.connect(mem_fun(*this, &MyClass::onDataReceived));
00037         // Send welcome message to the connected client */
00038       con->write("Hello, client!\n", 15);
00039     }
00040     
00041     void onClientDisconnected(TcpConnection *con,
00042                               TcpConnection::DisconnectReason reason)
00043     {
00044       cout << "Client " << con->remoteHost().toString() << ":"
00045            << con->remotePort() << " disconnected,"
00046            << server->numberOfClients() << " clients connected\n";
00047       /* Don't delete the con object, the TcpServer will do it */
00048     }
00049     
00050     int onDataReceived(TcpConnection *con, void *buf, int count)
00051     {
00052         // retreive data
00053       char *str = static_cast<char *>(buf);
00054       string data(str, str+count);
00055       cout << data;
00056       
00057         // Send data back to sender
00058       string dataOut = string("You said: ") + data;
00059       server->writeOnly(con, dataOut.c_str(), dataOut.size());
00060       
00061         // Other way to send to sender
00062       //con->write(dataOut.c_str(), dataOut.size());
00063       
00064         // Send to other clients if there is more then one connected to server
00065       if (server->numberOfClients() > 1)
00066       {
00067           // Send data back to all OTHER clients
00068         dataOut = string("He said : ") + data;
00069         server->writeExcept(con, dataOut.c_str(), dataOut.size());
00070         
00071           // Send data back to all clients
00072         dataOut = string("To all  : ") + data;
00073         server->writeAll(dataOut.c_str(), dataOut.size());
00074       }
00075       return count;
00076     }
00077 };
00078 
00079 int main(int argc, char **argv)
00080 {
00081   CppApplication app;
00082   MyClass my_class;
00083   app.exec();
00084 }
00085 
00086 

Generated on Mon Mar 25 18:14:42 2013 for Async by  doxygen 1.4.7