| 
	 | Using the TCP/IP ServerPrefaceThe disko lbrary does contain various functions for the application programmer. One of them is a simple to use TCP/IP server which shall be shown in this tutorial. The server working with various interfaces for a single port. The communication has to be set up, that the involved interfaces act in a cooperative way. Involved ClassesThe classes involved in this are MMSTCPServer, MMSServerInterface and MMSTCPClient. The MMSTCPServer contains the Server functionalities, it takes a vector of interface classes (MMSServerInterface) that will process the requests. The MMSTCPClient class is used to connect to a TCP/IP server, and to send and receive some messages. The ServerFirst step to implement a basic server is to derive a server interface from the MMSServerInterface class and to overwrite the processRequest() method. 
class MyInterface : public MMSServerInterface {
    public:
        MyInterface() : MMSServerInterface("iface1") {};
        ~MyInterface() {};
        bool processRequest(string *request, string *answer);
};
bool MyInterface::processRequest(string *request, string *answer) {
    cout << "request is: " << *request << endl;
    answer->append("request done!\n");
    return true;
}Then create an instance and append it to the interface vector. MyInterface *iface = new MyInterface(); <MMSServerInterface*> interfaces; interfaces.push_back(iface); After that create an instance of the server passing the interface vector listen address and port to the constructor and start the server. MMSTCPServer *server = new MMSTCPServer(interfaces,"127.0.0.1",11111); server->start(); The clientThe client is fairly easy to use. Just create an instance of the MMSTCPClient passing the target address and port to the constructor. After that just send a message to the server. 
MMSTCPClient *client = new MMSTCPClient("127.0.0.1",11111);
string retstr;
client->sendAndReceive("Heres my client message!", &retstr);
cout  << "answer was: " << retstr << endl;When all went well the server should print request is: Heres my client message! and the client should print answer was: request done!. A sample server and client application can be downloaded from here. To compile & link the sample type make in the root directory of the sample application. |