#include #include #include #include #include #include #include #include "sockets.h" /*Input: accepts port number to use as a command line arguement when * program is first run * Output: NONE * Return: NONE * Notes: Main method to use socket and file descriptor i/o library to * start a very simple to client chat server */ int main ( int argc, char ** argv ) { /*initializes all variables*/ int listen_sd; int io_sd1; int io_sd2; struct protoent * p_ptr; struct sockaddr_in serv; struct sockaddr_in client1; struct sockaddr_in client2; socklen_t cli_length1 = sizeof( struct sockaddr_in ); socklen_t cli_length2 = sizeof( struct sockaddr_in ); int port_num = 17835; int rc; int tmp; char buf1[80]; char buf2[80]; int check = 0; /*if port was specified at command line, this block sets it to * port_num */ if ( argc > 1 ) { port_num = atoi( argv[1] ); } p_ptr = getprotobyname( "tcp" ); /* calls _socket function*/ listen_sd = _socket( p_ptr , &listen_sd); /*checks if call to socket failed, and exits if it did fail*/ if ( -1 == listen_sd ) { fprintf ( stderr, "socket failed\n" ); exit(1); } /*sets up sockaddr for server*/ serv.sin_family = PF_INET; serv.sin_port = htons( port_num ); serv.sin_addr.s_addr = htonl( INADDR_ANY ); /* calls to _bind function*/ rc = _bind( serv , &rc , &listen_sd); /*checks if call to bind failed, and exits if it did fail*/ if ( -1 == rc ) { fprintf ( stderr, "bind failed\n" ); exit(2); } /* calls to _listen function */ rc = _listen( &listen_sd , &rc ); /*checks if call to listen failed, and exits if it did fail*/ if ( -1 == rc ) { fprintf ( stderr, "listen failed\n" ); exit(4); } /* accept once per connection */ io_sd1 = _accept ( &listen_sd, &io_sd1, client1, cli_length1 ); io_sd2 = _accept ( &listen_sd, &io_sd2, client2, cli_length2 ); /*checks if call to accept failed, and exits if it did fail*/ if ( -1 == io_sd1 || -1 == io_sd2 ) { fprintf ( stderr, "accept failed\n" ); exit(8); } /*do/while loop to handle communication back and forth between * clients */ do { /*calls read from client1*/ tmp = _read ( &io_sd1 , buf1 ); /*checks if call to read failed, and exits if it did fail*/ if ( tmp == -1 ) { fprintf ( stderr, "read failed\n" ); exit(16); } /*finds length of input read*/ tmp = strlen( buf1 ); /*checks if client entered 'quit' and informs other client, and * sets condition to end loop if 'quit' was entered */ if (((buf1[0]=='Q' || buf1[0]=='q') && (buf1[1]=='U' || buf1[1]=='u') && (buf1[2]=='I' || buf1[2]=='i') && (buf1[3]=='T' || buf1[3]=='t'))) { check = 1; sprintf(buf1, "connection closed\n"); } /*writes to client2's display*/ _write( &io_sd2 , buf1 , &tmp ); /*if client1 entered 'quit', nothing is read from client2*/ if ( check == 1 ) { } else { /*calls read from client2*/ tmp = _read ( &io_sd2 , buf2 ); /*checks if call to read failed, and exits if it did fail*/ if ( tmp == -1 ) { fprintf ( stderr, "read failed\n" ); exit(16); } /*finds length of input read*/ tmp = strlen( buf2 ); /*checks if client entered 'quit' and informs other client, and * sets condition to end loop if 'quit' was entered */ if (((buf2[0]=='Q' || buf2[0]=='q') && (buf2[1]=='U' || buf2[1]=='u') && (buf2[2]=='I' || buf2[2]=='i') && (buf2[3]=='T' || buf2[3]=='t'))) { check = 1; sprintf(buf1, "connection closed\n"); } /*writes to client2's display*/ _write( &io_sd1 , buf2 , &tmp ); } } while ( check != 1 ); /* close the file descriptors */ close( io_sd1 ); close( io_sd2 ); close( listen_sd ); return 0; }