/*************************************************************************** udpTalk Designed as a simple class example. The program send a request and waits for reply. This program expects one argument: udpTalk Hostname identifies the listener Version 1.0 October, 2002 Yvan Petillot ****************************************************************************/ #include #include #include #include #include #include #include #include #include #include #define TRUE 1 #define FALSE 0 #define BUFFER_SIZE 100 #define PORT1 10001 #define PORT2 10001 void SysError( char * ); /* Main Program */ main ( int argc, char *argv[] ) { int count; long input_value; int family = AF_INET; /* The default for most cases */ int type = SOCK_DGRAM; /* Says it's a UDP connection */ int result; struct sockaddr_in my_addr; /* Structure storing socket info */ struct sockaddr_in their_addr; /* Structure storing socket info */ int lsa = sizeof(my_addr); int fd; /* Socket id. On Unix a socket is a file descriptor */ char console_buffer[BUFFER_SIZE]; char ip_input_buffer[BUFFER_SIZE]; char ip_output_buffer[BUFFER_SIZE]; struct hostent *host; int on; /* Checks first we have the correct number of arguments */ if ( argc < 2 ) { printf( "The program expects arguments\n" ); printf( "udpTalk \n" ); exit(0); } host = gethostbyname(argv[1]); /* Create socket*/ if ((fd = socket (family, type, 0)) < 0) { SysError ("Error on socket"); } /* Fills my_addr socket structure with correct fields */ my_addr.sin_family = family; my_addr.sin_port = PORT2; my_addr.sin_addr.s_addr = htonl(INADDR_ANY); /* the kernel assigns the IP addr*/ /* Fills their socket structure with correct fields */ their_addr.sin_family = family; their_addr.sin_port = PORT1; memcpy((char *)&their_addr.sin_addr, (char *)host->h_addr, host->h_length); on = 1; setsockopt(fd,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on)); setsockopt(fd,SOL_SOCKET,SO_BROADCAST,&on,sizeof(on)); /* First bind fd and sa structure*/ /*if (bind (fd, (struct sockaddr *)&my_addr, sizeof(my_addr) ) == -1) SysError ("Error on bind");*/ /* Infinite loop until fault */ while( TRUE ) { /* Get a number from console */ printf( "> " ); scanf( "%s", console_buffer ); /* Convert it to integer (-1 to exit)*/ if ( atoi( console_buffer ) == -1 ) { printf( "We Have Successfully Finished.\n" ); exit(0); } /* Make sure output buffer is empty */ bzero( ip_output_buffer, sizeof( ip_output_buffer )); /* Copy value to output buffer */ strcpy( ip_output_buffer, console_buffer ); /* Send value to other machine */ count = sendto(fd, ip_output_buffer, strlen(ip_output_buffer), 0,&their_addr,sizeof(their_addr) ); if (count == 0) SysError( "Error on send" ); /* Make sure input buffer is empty*/ bzero( ip_input_buffer, sizeof(ip_input_buffer) ); /* receive data back from machine */ /*count = recvfrom( fd, ip_input_buffer,sizeof(ip_input_buffer),0,&their_addr,sizeof(their_addr)); if (count == 0) SysError( "Error on recv" );*/ /*Print reception */ printf( "%s\n", ip_input_buffer ); } close(fd); /* End of while TRUE */ } /* End of main */ void SysError( char *string ) { printf( "Error found: String given is --> %s\n", string ); exit(0); }