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

tcp_client.cpp

00001 /*
00002    CABAL Protocol
00003    Test for client TCP socket
00004 
00005    $Id: tcp_client.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 #define DEFAULT_IP         "127.0.0.1"
00018 
00019 using namespace std;
00020 using namespace Cabal;
00021 
00022 int main ( int argc, char *argv[] )
00023 {
00024    int n_port;
00025    char *c_ip;
00026 
00027    if ( argc == 3 ) {
00028       c_ip = argv[1];
00029       n_port = atoi( argv[2] );
00030    }
00031    else if ( argc == 2 ) {
00032       c_ip = DEFAULT_IP;
00033       n_port = atoi( argv[1] );
00034    }
00035    else {
00036       c_ip = DEFAULT_IP;
00037       n_port = DEFAULT_PORT;
00038    }
00039 
00040 
00041    try {
00042       Net::init();
00043 
00044       // timeout == 0 means non-blocking!
00045       TCPSocket s;
00046       s.timeout(0);
00047       bool res =  s.connect( c_ip, n_port );
00048       int count = 0 ;
00049 
00050       while( s.hasTimedOut() && count < 40 ) {
00051          cout << "." << flush;
00052          Net::sleep( 250 );
00053          res = s.continueOp();
00054          count++;
00055       };
00056 
00057       if ( ! res ) {
00058          cout <<"\nCan't connect: "<< s.osError() << "\n";
00059       }
00060       else {
00061          cout << "\nConnected!\n";
00062          Net::sleep( 1000 );
00063          cout << "Server sayed: " << s.recvLine() << "\n";
00064          if ( s.sendLine( "Ok, I have connected" ) ) {
00065             cout << "Data Sent sucessfully\n";
00066          }
00067          s.close();
00068       }
00069       cout << "Done\n";
00070 
00071       Net::exit();
00072    }
00073    catch (Error e ) {
00074       cout << e <<"\n";
00075    }
00076 
00077    return 0;
00078 }

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