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

udp_client.cpp

00001 /*
00002    CABAL 
00003    Test for client UDP socket
00004 
00005    $Id: udp_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    try {
00041       Net::init();
00042       
00043       // timeout == 0 means non-blocking!
00044       Address serverAddr( c_ip, n_port);
00045       UDPSocket s( 1500, 0, &serverAddr  );
00046       s.setBroadcast();
00047 
00048       char *out_data = "Ok, I have connected\r\n";
00049       if ( s.send( out_data, strlen( out_data ) ) ) {
00050          cout << "Data Sent sucessfully to "<< serverAddr.ip() <<":"<< serverAddr.port() << 
00051                ", wait for a reply." << endl;
00052       }
00053       
00054       char data[1500];
00055       int len = s.recv( data, 1500 );
00056       
00057       if ( s.hasTimedOut() ) 
00058          cout << "Reply not received." << endl;
00059       else if ( s.osError() )
00060          cout << "Socket had error "<< s.osError() << ":" << Net::errorDescription( s.osError()) << endl;
00061       else if ( len > 0 ) {
00062          data[ len ] = 0;
00063          cout << "Reply: " << data << endl;
00064       }
00065       
00066       cout << "Done" << endl;
00067       Net::exit();
00068    }
00069    catch (Error e ) {
00070       cout << e <<"\n";
00071    }
00072 
00073    return 0;
00074 }

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