void test_address() { printf( "-----------------------------------------------------\n" ); printf( "test address\n" ); printf( "-----------------------------------------------------\n" ); printf( "defaults\n" ); { Address address; check( address.GetA() == 0 ); check( address.GetB() == 0 ); check( address.GetC() == 0 ); check( address.GetD() == 0 ); check( address.GetPort() == 0 ); check( address.GetAddress() == 0 ); } printf( "a,b,c,d,port\n" ); { const unsigned char a = 100; const unsigned char b = 110; const unsigned char c = 50; const unsigned char d = 12; const unsigned short port = 10000; Address address( a, b, c, d, port ); check( a == address.GetA() ); check( b == address.GetB() ); check( c == address.GetC() ); check( d == address.GetD() ); check( port == address.GetPort() ); } printf( "equality/inequality\n"); { Address x(100,110,0,1,50000); Address y(101,210,6,5,50002); check( x != y ); check( y == y ); check( x == x ); } }
int main( int argc, char * argv[] ) { // initialize socket layer if ( !InitializeSockets() ) { printf( "failed to initialize sockets\n" ); return 1; } // create socket int port = 30000; if ( argc == 2 ) port = atoi( argv[1] ); printf( "creating socket on port %d\n", port ); Socket socket; if ( !socket.Open( port ) ) { printf( "failed to create socket!\n" ); return 1; } // read in addresses.txt to get the set of addresses we will send packets to vector<Address> addresses; string line; ifstream file( "addresses.txt" ); if ( !file.is_open() ) { printf( "failed to open 'addresses.txt'\n" ); return 1; } while ( !file.eof() ) { getline( file, line ); int a,b,c,d,port; if ( sscanf( line.c_str(), "%d.%d.%d.%d:%d", &a, &b, &c, &d, &port ) == 5 ) addresses.push_back( Address( a,b,c,d,port ) ); } file.close(); // send and receive packets until the user ctrl-breaks... while ( true ) { const char data[] = "hello world!"; for ( int i = 0; i < (int) addresses.size(); ++i ) socket.Send( addresses[i], data, sizeof( data ) ); while ( true ) { Address sender; unsigned char buffer[256]; int bytes_read = socket.Receive( sender, buffer, sizeof( buffer ) ); if ( !bytes_read ) break; printf( "received packet from %d.%d.%d.%d:%d (%d bytes)\n", sender.GetA(), sender.GetB(), sender.GetC(), sender.GetD(), sender.GetPort(), bytes_read ); } wait_seconds( 1.0f ); } // shutdown socket layer ShutdownSockets(); return 0; }