Main Page | Modules | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

tcp_server.cpp

00001 /*
00002    CABAL Protocol
00003    Test for server TCP socket
00004 
00005    $Id: tcp_server.cpp,v 1.3 2004/03/26 22:45:11 jonnymind Exp $
00006 ---------------------------------------------
00007    Begin      :
00008    Author     : Giancarlo Niccolai
00009 
00010    Last modified because:
00011 
00012 */
00013 
00014 #include <cabal.h>
00015 
00016 #define DEFAULT_PORT       2051
00017 
00018 using namespace std;
00019 using namespace Cabal;
00020 
00021 int main ( int argc, char *argv[] )
00022 {
00023    int n_port;
00024    if ( argc > 1 ) {
00025       n_port = atoi( argv[1] );
00026    }
00027    else {
00028       n_port = DEFAULT_PORT;
00029    }
00030 
00031 
00032    try {
00033       Net::init();
00034 
00035       Address local( "0.0.0.0", n_port );
00036       ServerSocket ss( local );
00037       TCPSocket *s;
00038 
00039       cout << "Server waiting on port "<< n_port << " (closing in 60 secs)\n";
00040       // setting timeout to 30 seconds
00041       ss.timeout( 60000 );
00042 
00043       while ( ( s = ss.accept() ) ) {
00044          cout << "Connection from: " << s->remote_ip() << ":" <<
00045             s->remote_port() << "\n";
00046 
00047          s->timeout( 10000 );
00048 
00049          s->sendLine("Welcome on board!");
00050          s->sendLine("Say something in 10 seconds or be banned!");
00051 
00052          string line;
00053 
00054          while ( s->osError() == 0 && ! s->closed() && ! s->hasTimedOut() ) {
00055             line = s->recvLine( 1024 );
00056             if ( line != "" ) {
00057                cout << "Client sayed: \"" << line << "\"\n";
00058                if ( ! s->osError() && ! s->closed() )
00059                   s->sendLine( "Received" );
00060             }
00061          }
00062 
00063          if ( s->hasTimedOut() )  {
00064             if ( ! s->closed() )
00065                s->sendLine("You have timed out. Goodbye.");
00066             cout << "Client has timed out\n";
00067          }
00068          else if ( s->closed() )
00069             cout << "Client closed connection\n";
00070          else
00071             cout << "Socket has got error: " << s->osError()<<"\n";
00072 
00073          delete s;
00074       }
00075 
00076       if ( ss.hasTimedOut() )
00077          cout << "Done on timeout\n";
00078       else
00079          cout << "Done\n";
00080 
00081       Net::exit();
00082    }
00083    catch (Error &e ) {
00084       cout << e << endl;
00085    }
00086 
00087    return 0;
00088 }

Generated on Sat Apr 10 17:41:49 2004 for Cabal by doxygen 1.3.5