Listing 8: Test client program
#include "Sockets.h" #include <iostream> #include <string> #include <complex> using namespace std; const int IPportnumber = 12345; const char *IPserveraddress = "127.0.0.1"; int main() { string message; complex<double> cplx; int integer; if (socketsInit() == false) { cout << "cannot initialize sockets" << endl; return 0; } try { TCPClientStream stream(IPserveraddress, IPportnumber); bool oncemore = true; int command; while (oncemore) { cout << "1. request for a string" << endl; cout << "2. request for a number" << endl; cout << "3. request for a complex number" << endl; cout << "other - end" << endl; cin >> command; stream << command << endl; switch (command) { case 1: do { getline(stream, message); } while (message.empty()); cout << "received: " << message << endl; break; case 2: stream >> integer; cout << "received: " << integer << endl; break; case 3: stream >> cplx; cout << "received: " << cplx << endl; break; default: oncemore = false; break; } } } catch (const SocketRunTimeException &e) { cout << "socket exception: " << e.what() << endl; } socketsEnd(); return 0; } End of Listing