//------------------------------------------------------------------- // msgclient.cpp // // This example uses the 'sendmsg()' and 'recvmsg()' functions // to send a message to our 'msgserver' application, then wait // for up to five seconds for the expected response to arrive. // // compile using: $ g++ msgclient.cpp -o msgclient // execute using: $ ./msgclient // // programmer: ALLAN CRUSE // written on: 12 FEB 2009 //------------------------------------------------------------------- #include // for gethostbyname() #include // for printf(), perror() #include // for open() #include // for exit() #include // for read(), write(), close() #include // for strncpy() #include // for inet_ntoa() #define SERVER_PORT 54321 int main( int argc, char **argv ) { //------------------------------------------------ // get destination hostname from the command-line //------------------------------------------------ char peername[ 64 ] = { 0 }; if ( argc == 1 ) { fprintf( stderr, "target?\n" ); exit(1); } strncpy( peername, argv[ 1 ], 63 ); //-------------------------------------------------- // obtain the internet address for this remote host //-------------------------------------------------- char peeraddr[ 16 ] = { 0 }; struct hostent *pp = gethostbyname( peername ); if ( !pp ) { herror( "gethostbyname" ); exit(1); } strcpy( peeraddr, inet_ntoa( *(in_addr*)pp->h_addr ) ); //-------------------------------------------------------- // initialize a socket-address object for the remote host //-------------------------------------------------------- struct sockaddr_in paddr = { 0 }; socklen_t palen = sizeof( paddr ); paddr.sin_family = AF_INET; paddr.sin_port = htons( SERVER_PORT ); paddr.sin_addr.s_addr = *(uint32_t*)pp->h_addr; //------------------------------------ // create an internet datagram socket //------------------------------------ int sock = socket( AF_INET, SOCK_DGRAM, IPPROTO_UDP ); if ( sock < 0 ) { perror( "socket" ); exit(1); } //------------------------------------- // set a timeout for receiving replies //------------------------------------- struct timeval mytv = { 5, 0 }; int tlen = sizeof( mytv ); if ( setsockopt( sock, SOL_SOCKET, SO_RCVTIMEO, &mytv, tlen ) < 0 ) { perror( "setsockopt RCVTIMEO" ); exit(1); } //--------------------------------------------------- // initialize objects for a message-header structure //--------------------------------------------------- char msg[] = " ABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890 "; int len = strlen( msg ); struct iovec myiov[1] = { { msg, len } }; //----------------------------------------- // initialize the message-header structure //----------------------------------------- struct msghdr mymsghdr = { 0 }; mymsghdr.msg_name = &paddr; mymsghdr.msg_namelen = palen; mymsghdr.msg_iov = myiov; mymsghdr.msg_iovlen = 1; mymsghdr.msg_control = NULL; mymsghdr.msg_controllen = 0; mymsghdr.msg_flags = 0; //------------------------------------- // send our message to the remote host //------------------------------------- int tx = sendmsg( sock, &mymsghdr, 0 ); if ( tx < 0 ) { perror( "sendmsg" ); exit(1); } printf( " sent %d bytes to \'%s\' (%s) \n", tx, peername, peeraddr ); //-------------------------- // erase our message buffer //-------------------------- bzero( msg, sizeof( msg ) ); //----------------------------------- // receive a reply (or else timeout) //----------------------------------- int rx = recvmsg( sock, &mymsghdr, 0 ); if ( rx < 0 ) perror( "recvmsg" ); else printf( " received %d bytes from", rx ); printf( " %s \n", inet_ntoa( *(in_addr*)&paddr.sin_addr ) ); if ( rx > 0 ) puts( msg ); }