/*************************************************************************** tcp_client Designed as a simple class example. The program waits for a request. It assumes that request is numerical. It adds +1 to the input and sends it back. This program expects one argument: tcp_client The argument says where the server is Version 1.0 October, 2002 Yvan Petillot ****************************************************************************/ #include #include #include #include #include #include #include #include #include #define TRUE 1 #define FALSE 0 #define BUFFER_SIZE 100 #define PORT 10000 void SysError( char * ); /* Main Program */ main ( int argc, char *argv[] ) { long input_value; int family = AF_INET; /* The default for most cases */ int type = SOCK_STREAM; /* Says it's a TCP connection */ in_port_t port = PORT; /* Use a free port to establish connection */ int result; struct sockaddr_in client; /* Structure storing socket info for client */ struct sockaddr_in server; /* Structure storing socket info for server */ int lclient = sizeof(client); 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; /* Checks first we have the correct number of arguments */ if ( argc < 2 ) { printf( "The program expects arguments\n" ); printf( "tcp_client \n" ); exit(0); } if ((host = gethostbyname(argv[1])) == (struct hostent *)NULL) { SysError("Error on gethostbyname"); return(-1); } /* Create socket*/ if ((fd = socket (family, type, 0)) < 0) { SysError ("Error on socket"); } /* Fills client socket structure with correct fields */ client.sin_family = family; client.sin_port = port; /* client & server see same port*/ client.sin_addr.s_addr = htonl(INADDR_ANY); /* the kernel assigns the IP addr*/ /* Fills server socket structure with correct fields */ server.sin_family = family; server.sin_port = port; /* client & server see same port*/ memcpy((char *)&server.sin_addr, (char *)host->h_addr, host->h_length); /* We are a client here */ /* Go directly to connect. You must have a server running*/ if (connect(fd, (struct sockaddr *)&server, sizeof(server) )) SysError ("Error on connect"); /* Until fault */ while( TRUE ) { /* Get a number from console */ printf( "> " ); bzero( console_buffer, sizeof(console_buffer )); 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 server */ if ( send( fd, ip_output_buffer, strlen(ip_output_buffer) + 1, 0 ) <= 0 ) SysError( "Error on send" ); /* Make sure input buffer is empty*/ bzero( ip_input_buffer, sizeof(ip_input_buffer) ); /* receive data back from server */ if ( recv( fd, ip_input_buffer, sizeof(ip_input_buffer) - 2, 0 ) <= 0 ) SysError( "Error on recv" ); /*Print reception */ printf( "%s\n", ip_input_buffer ); } /* End of while TRUE */ } /* End of client case */ /* End of main */ void SysError( char *string ) { printf( "Error found: String given is --> %s\n", string ); exit(0); }