listener.cpp

Shows how to add sockets to a listener object and test for reading.

00001 #include <iostream>
00002 #include <string>
00003 #include <algorithm>
00004 #include <cstdlib>
00005 #include <socklibpp/socklistener.h>
00006 
00007 using namespace std;
00008 using namespace socklibpp;
00009 
00010 listener<> server;
00011 int global_counter = 1;
00012 
00013 #define USE_FOREACH
00014 
00015 bool do_stuffs(sock sb, int i)
00016 {
00017         if (i == 0) {
00018                 addr_in a;
00019                 sock newconn = sb.accept(a);
00020                 if (!sb.error) {        // No error
00021                         cout << "Received connection " << global_counter <<
00022                                 '(' << newconn.fd << ')' << " from " << a << endl;
00023                         server.add(newconn, global_counter++);
00024                 }
00025         } else {
00026                 char buffer[256];
00027                 int r = sb.recv(buffer, sizeof buffer);
00028                 if (r<1) {
00029                         cout << "Lost connection " << i << '(' << sb.fd << ')' << endl;
00030                         sb.close();
00031                         #ifndef USE_FOREACH
00032                         // If we aren't using for_each_read, we need to remove it explicitly.
00033                         server.remove(sb);
00034                         #endif
00035                         return true;
00036                 }
00037 
00038                 cout << "Receiving data from connection " << i << '(' << sb.fd << ')' << ":\n" <<
00039                         string(buffer, r) << endl;
00040         }
00041         return false;
00042 }
00043 
00044 void do_stuffs_helper(const pair<sock,int>& data)
00045 {
00046         do_stuffs(data.first, data.second);
00047 }
00048 
00049 
00050 int main(int argc, char **argv)
00051 {
00052         uint16_t port = 3747;
00053         
00054         if (argc>1)
00055                 port = strtoul(argv[1], NULL, 10);
00056         
00057         addr_in local(addr_in::addr_any, port);
00058         sock passive(sock::pf_inet, sock::sock_stream);
00059         if (!passive.bind(local) || !passive.listen())
00060                 return 1;
00061 
00062         server.add(passive);
00063         
00064         #ifndef USE_FOREACH
00065         list< pair<sock, int> > to_read;
00066         #endif
00067 
00068         cout << "We are going to test the server using "
00069         #ifdef USE_FOREACH
00070                 "server::for_each_read"
00071         #else
00072                 "server::readable"
00073         #endif
00074                " on port " << port << endl;
00075 
00076         do {
00077                 cout << "Listening to " << server.size() << " sockets.\n"
00078                         "Press enter to test for readability (Ctrl+C to exit)" << endl;
00079                 cin.ignore();
00080 
00081                 #ifndef USE_FOREACH
00082                 to_read.clear();
00083                 server.readable(to_read);
00084                 for_each(to_read.begin(), to_read.end(), do_stuffs_helper);
00085                 #else
00086                 server.for_each_read(do_stuffs);
00087                 #endif
00088 
00089         } while (cin.good());
00090 }

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