daytimed.cpp

Implements a daytime server (you can test it with "telnet localhost daytime"). It returns a string representation of the current time, then closes the connection.

00001 #include <iostream>
00002 #include <ctime>
00003 #include <socklibpp/sockstream.h>
00004 
00005 using namespace std;
00006 using namespace socklibpp;
00007 
00008 int main()
00009 {
00010         addr_in local(addr_in::addr_any, 0);
00011         local.port("daytime");
00012 
00013         sock server;
00014         
00015         server.create(sock::pf_inet, sock::sock_stream);
00016         
00017         if (!server.bind(local)) {
00018                 cerr << "Could not bind: " << server.error << endl;
00019                 return 1;
00020         }
00021         
00022         if (!server.listen()) {
00023                 cerr << "Could not listen: " << server.error << endl;
00024                 return 1;
00025         }
00026 
00027         cout << "Running daytime server on " << local << endl;
00028         
00029         while (1) {
00030                 addr_in client_addr;
00031                 sock client = server.accept(client_addr);
00032                 
00033                 cout << "Serving client " << client_addr << endl;
00034                 
00035                 // Let's not receive anything from the client
00036                 client.shutdown(sock::shut_recv);
00037                 
00038                 time_t now = time(0);
00039                 const char * timestr = ctime(&now);
00040 
00041                 client.send(timestr, strlen(timestr));
00042 
00043                 client.close();
00044         }
00045 }

Generated on Thu Jan 18 19:26:34 2007 for socklib++ by  doxygen 1.5.1