//------------------------------------------------------------------- // usenagle.cpp // // Sends a stream of bytes to an echo-server using the default // behavior of a Linux TCP socket (i.e., the Nagle algorithm). // // to compile: $ g++ usenagle.cpp -o usenagle // to execute: $ ./usenagle // // programmer: ALLAN CRUSE // written on: 01 MAR 2009 //------------------------------------------------------------------- #include // for gethostbyname() #include // for printf(), perror() #include // for exit() #include // for read(), write(), close() #include // for strncpy() #include // for inet_addr() #define DEMO_PORT 54321 int main( int argc, char **argv ) { //------------------------------------------- // check for mandatory command-line argument //------------------------------------------- if ( argc == 1 ) { fprintf( stderr, " target? \n" ); exit( 1 ); } //-------------------------------------------------------- // setup hostname and network address for the remote peer //-------------------------------------------------------- char peername[ 64 ] = { 0 }, peeraddr[ 16 ] = { 0 }; strncpy( peername, argv[ 1 ], 63 ); struct hostent *pp = gethostbyname( peername ); strcpy( peeraddr, inet_ntoa( *(in_addr*)pp->h_addr ) ); //------------------------------------------------------- // create a socket-address structure for the remote peer //------------------------------------------------------- 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 = inet_addr( peeraddr ); printf( "\nDestination: %s (%s) \n", peername, peeraddr ); //---------------------------------------------------------- // create a socket to use for communicating with the server //---------------------------------------------------------- int sock = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP ); if ( sock < 0 ) { perror( "socket" ); exit(1); } //---------------------------------------------------- // establish a connection with with the remote server //---------------------------------------------------- if ( connect( sock, (sockaddr*)&paddr, palen ) < 0 ) { perror( "connect" ); exit(1); } //---------------------------------------------- // prepare the message-string to be transmitted //---------------------------------------------- char msg[ 4096 ] = { 0 }; int k = 0; for (int i = 0; i < 26; i++) { for (int j = 0; j < 72; j++) k += sprintf( msg+k, "%c", 'a'+i ); k += sprintf( msg+k, "\n" ); } int len = strlen( msg ); //------------------------------------------------------- // write our message-string to the socket's output-queue //------------------------------------------------------- for (int i = 0; i < len; i++) write( sock, msg+i, 1 ); fflush( fdopen( sock, "rw" ) ); printf( "sent %d bytes \n", len ); //---------------------------------- // close our socket's output-stream //---------------------------------- shutdown( sock, SHUT_WR ); //----------------------------------------------- // read the response from our connection-partner //----------------------------------------------- for(;;) { char buf[ 4096 ] = { 0 }; int rx = read( sock, buf, sizeof( buf ) ); if ( rx == 0 ) break; if ( rx < 0 ) { perror( "read" ); exit(1); } printf( "received %d bytes \n", rx ); puts( buf ); } }