//------------------------------------------------------------------- // sixlinks.cpp // // This TCP client establishes six connections with our server, // for checking the server's abiity to handle multiple clients. // // to compile: $ g++ sixlinks.cpp -o sixlinks // to execute: $ ./sixlings // // programmer: ALLAN CRUSE // written on: 09 MAR 2009 //------------------------------------------------------------------- #include // for gethostbyname() #include // for printf(), perror() #include // for exit() #include // for read(), write(), close() #include // for strncpy(), strlen() #define DEMO_PORT 54321 #define NUM_LINKS 6 int main( int argc, char **argv ) { //----------------------------------------------- // check for the mandatory command-line argument //----------------------------------------------- if ( argc == 1 ) { fprintf( stderr, "must specify server\n" ); exit(1); } //------------------------------------------------- // get the server's hostname from the command-line //------------------------------------------------- char peername[ 64 ] = { 0 }; strncpy( peername, argv[ 1 ], 63 ); //------------------------------------- // obtain the IP-address of the server //------------------------------------- struct hostent *pp = gethostbyname( peername ); if ( !pp ) { herror( "gethostbyname" ); exit(1); } //-------------------------------------------- // construct a socket-address for this server //-------------------------------------------- struct sockaddr_in paddr = { 0 }; socklen_t palen = sizeof( paddr ); paddr.sin_family = AF_INET; paddr.sin_port = htons( DEMO_PORT ); paddr.sin_addr.s_addr = *(int32_t*)pp->h_addr; //----------------------------------------------------------- // create an array of sockets for connecting with the server //----------------------------------------------------------- int sock[ NUM_LINKS ]; for (int i = 0; i < NUM_LINKS; i++) { sock[ i ] = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP ); if ( sock[ i ] < 0 ) { perror( "socket" ); exit(1); } } //---------------------------------------------------- // establish six separate connections with the server //---------------------------------------------------- for (int i = 0; i < NUM_LINKS; i++) { if ( connect( sock[ i ], (sockaddr*)&paddr, palen ) < 0 ) { perror( "connect" ); exit(1); } } //----------------------------------------------------------- // send a message to the server on each of these connections //----------------------------------------------------------- char msg[ 40 ] = "Never the twain shall meet\n"; for (int i = 0; i < NUM_LINKS; i++) { int tx = write( sock[ i ], msg, strlen( msg ) ); if ( tx < 0 ) { perror( "write" ); exit(1); } fflush( fdopen( sock[ i ], "rw" ) ); if ( shutdown( sock[ i ], SHUT_WR ) < 0 ) perror( "shutdown" ); puts( msg ); } //--------------------------------------------------------- // receive the server's reply on each of these connections //--------------------------------------------------------- for (int i = 0; i < NUM_LINKS; i++) { char buf[ 40 ] = { 0 }; int rx = read( sock[ i ], buf, strlen( msg ) ); if ( rx < 0 ) { perror( "read" ); exit(1); } puts( buf ); } //--------------------------------------------- // close down each of these socket-connections //--------------------------------------------- for (int i = 0; i < NUM_LINKS; i++) close( sock[ i ] ); }