Public Member Functions

TRTK::Server Class Reference

Simple TCP/IP server (one client at a time). More...

#include <ClientServer.hpp>

List of all members.

Public Member Functions

 Server ()
 Constructs a Server object.
virtual ~Server ()
 Destroys a Server object.
void startServer (short port)
 Starts the server.
void stopServer ()
 Stops the server.
virtual std::pair< char *, size_t > handleRequest (const char *received_data, size_t length)=0
 Handles incoming requests.
void closeConnection ()
 Notify the server about the client's intention to close the connection.

Detailed Description

Simple TCP/IP server (one client at a time).

This class allows creating a simple server by just deriving from it. The server is only able to handle a single client-server-connection at a time (i.e. after accepting a client connection it communicates only with this particular client until this client disconnects).

A client-request is handled by the handleRequest() function which is declared virtual so that it can be reimplemented by the derived class. It is the only way to communicate with the client. Each client request is followed by the server's answer which is directly sent to the client. It is assumed that the server keeps track of its state.

In the case of a network error (transmission error, unexpectedly disconnected client, etc.) an exception of the type NetworkError is thrown.

Example:

Server.cpp

 #include <cstdio>
 #include <iostream>
 #include <string>

 #include <TRTK/ClientServer.hpp>

 const int PORT = 9734;

 class Server : public TRTK::Server
 {
 public:
     std::pair<char *, size_t> handleRequest(const char * data, size_t size)
     {
         // Put out the received data.
         if (size) std::cout << data << std::endl;

         // Here the internal state can be changed or
         // the server can be shut down.
         stopServer();

         // Send the answer.
         char * str = new char[14];
         std::strcpy(str, "Hello Client!");
         return std::pair<char *, size_t>(str, 14);
     }
 };

 int main()
 {
     try
     {
         Server server;
         server.startServer(PORT);
     }
     catch (std::exception & error)
     {
         std::cout << error.what() << std::endl;
     }

     return 0;
 }

Client.cpp

 #include <iostream>
 #include <TRTK/ClientServer.hpp>

 const char HOSTNAME[] = "127.0.0.1"; // "localhost";
 const int PORT = 9734;

 class Client : public TRTK::Client
 {
 public:
     Client() : reply((char *) NULL, 0)
     {
     }

     void run()
     {
         connect(HOSTNAME, PORT);

         char message[] = "Hello server!";
         reply = request(&message[0], 14);
         if (reply.second) std::cout << reply.first << std::endl;
         delete[] reply.first;

         disconnect();
     }

 private:
     std::pair<char *, size_t> reply;
 };

 int main()
 {
     try
     {
         Client client;
         client.run();
     }
     catch (std::exception & error)
     {
         std::cout << error.what();
     }

     return 0;
 }

Output:

Server.cpp

Hello server! 

Client.cpp

Hello client! 
See also:
TRTK::Client
Author:
Christoph Haenisch
Version:
0.1.1
Date:
last changed on 2014-07-05

Definition at line 214 of file ClientServer.hpp.


Member Function Documentation

void TRTK::Server::closeConnection (  )

Notify the server about the client's intention to close the connection.

Calling this function leads to the termination of the internal event loop regarding incoming client messages. This function could be called from the derived class (see handleRequest()) if the client-server-dialog ends intentionally.

Definition at line 414 of file ClientServer.cpp.

std::pair< char *, size_t > TRTK::Server::handleRequest ( const char *  received_data,
size_t  length 
) [pure virtual]

Handles incoming requests.

This function is called, each time a client sends data to the server. The server responds by returning some data in the form of a std::pair consisting of a pointer to the block of data (on the heap) and its size. After having sent the data to the client the memory is freed using delete[].

void TRTK::Server::startServer ( short  port )

Starts the server.

The server will listen on port port and run in an infinite loop. If a client connects to the server the virtual method handleRequest() is called and the received data is passed on. The data returned by this method is send to the client.

Definition at line 465 of file ClientServer.cpp.


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines