Async::TcpServer Class Reference

A class for creating a TCP server. More...

#include <AsyncTcpServer.h>

List of all members.

Public Member Functions

Public Attributes


Detailed Description

A class for creating a TCP server.

Author:
Tobias Blomberg
Date:
2003-12-07

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 Async::TcpConnection object is created which is used to do the actual communication. An example of how to use it is shown below.

#include <iostream>
#include <AsyncCppApplication.h>
#include <AsyncTcpServer.h>

using namespace std;
using namespace Async;

class MyClass : public SigC::Object
{
  public:
    MyClass(void)
    {
      server = new TcpServer("12345");
      server->clientConnected.connect(slot(*this, &MyClass::onClientConnected));
      server->clientDisconnected.connect(
              slot(*this, &MyClass::onClientDisconnected));
      cout << "Connect using: \"telnet localhost 12345\" from "
              "another console\n";
    }
    
    ~MyClass(void)
    {
      delete server;
    }

  private:
    TcpServer *server;
    
    void onClientConnected(TcpConnection *con)
    {
      cout << "Client " << con->remoteHost() << ":"
           << con->remotePort() << " connected, "
           << server->numberOfClients() << " clients connected\n";
        // We need ONLY to add signal for receive data to the TcpConnection
      con->dataReceived.connect(slot(*this, &MyClass::onDataReceived));
        // Send welcome message to the connected client */
      con->write("Hello, client!\n", 15);
    }
    
    void onClientDisconnected(TcpConnection *con,
                              TcpConnection::DisconnectReason reason)
    {
      cout << "Client " << con->remoteHost().toString() << ":"
           << con->remotePort() << " disconnected,"
           << server->numberOfClients() << " clients connected\n";
      /* Don't delete the con object, the TcpServer will do it */
    }
    
    int onDataReceived(TcpConnection *con, void *buf, int count)
    {
        // retreive data
      char *str = static_cast<char *>(buf);
      string data(str, str+count);
      cout << data;
      
        // Send data back to sender
      string dataOut = string("You said: ") + data;
      server->writeOnly(con, dataOut.c_str(), dataOut.size());
      
        // Other way to send to sender
      //con->write(dataOut.c_str(), dataOut.size());
      
        // Send to other clients if there is more then one connected to server
      if (server->numberOfClients() > 1)
      {
          // Send data back to all OTHER clients
        dataOut = string("He said : ") + data;
        server->writeExcept(con, dataOut.c_str(), dataOut.size());
        
          // Send data back to all clients
        dataOut = string("To all  : ") + data;
        server->writeAll(dataOut.c_str(), dataOut.size());
      }
      return count;
    }
};

int main(int argc, char **argv)
{
  CppApplication app;
  MyClass my_class;
  app.exec();
}


Examples:

AsyncTcpServer_demo.cpp.

Definition at line 126 of file AsyncTcpServer.h.


Constructor & Destructor Documentation

Async::TcpServer::TcpServer ( const std::string &  port_str  ) 

Default constuctor.

Parameters:
port_str A port number or service name to listen to
Async::TcpServer::~TcpServer ( void   ) 

Destructor.


Member Function Documentation

TcpConnection* Async::TcpServer::getClient ( unsigned int  index  ) 

Get the client object pointer from the server.

Parameters:
index The wanted client by number 0 - numberOfClients()-1
Returns:
The TcpConnection pointer to the client (zero if not found)
int Async::TcpServer::numberOfClients ( void   ) 

Get the number of clients that is connected to the server.

Returns:
The number of connected clients
int Async::TcpServer::writeAll ( const void *  buf,
int  count 
)

Write data to all connected clients.

Parameters:
buf The data buffer
count The number of bytes in the data buffer
Returns:
The number of bytes sent
int Async::TcpServer::writeExcept ( TcpConnection con,
const void *  buf,
int  count 
)

Send data to all connected clients except the given client.

Parameters:
con The TcpConnection object not to send to
buf The data buffer
count The number of bytes in the data buffer
Returns:
The number of bytes sent
int Async::TcpServer::writeOnly ( TcpConnection con,
const void *  buf,
int  count 
)

Send data only to the given client.

Parameters:
con The TcpConnection object to send to
buf The data buffer
count The number of bytes in data buffer
Returns:
The number of bytes sent

Member Data Documentation

A signal that is emitted when a client connect to the server.

Parameters:
con The connected TcpConnection object

Definition at line 183 of file AsyncTcpServer.h.

A signal that is emitted when a client disconnect from the server.

Parameters:
con The disconnected TcpConnection object

Definition at line 191 of file AsyncTcpServer.h.


The documentation for this class was generated from the following file:
Generated by  doxygen 1.6.2-20100208