00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
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 Net::init();
00032
00033 try {
00034 UDPSocket s( "0.0.0.0", n_port );
00035 s.timeout( 60000 );
00036
00037 cout << "Server waiting on port "<< n_port << " (closing in 60 secs)\n";
00038
00039 char data[1500];
00040
00041 int len;
00042 while ( (len = s.recv( data, 1500 ) ) >0 ) {
00043 cout << "Connection from: " << s.remote_ip() << ":" <<
00044 s.remote_port() << "\n";
00045 data[len] = 0;
00046 cout << "Message is: " << data << "\n";
00047 std::string hail = "Hail client from your server!!!\r\n";
00048
00049 s.send( hail.c_str(), hail.length() );
00050 }
00051
00052 if ( s.hasTimedOut() )
00053 cout << "Done on timeout\n";
00054 else if ( s.osError() )
00055 cout << "Closing on error " << s.osError() << "\n";
00056 else
00057 cout << "Done\n";
00058 }
00059 catch (Error e ) {
00060 cout << e <<"\n";
00061 }
00062
00063 Net::exit();
00064 return 0;
00065 }