/**************************** * Programmer: Joe Gakenheimer * Date: February 25, 2004 * Course: Comp 470 - Computer Networks * Assignment: Lab 1, Balderdash Client * File Name: gakenh01/cs470/Lab1/lab1.cpp * Description: Balderdash Client * The client makes a connection to the server. * It then reads and player1 and line_of_text from the user and sends them to the server. * It then reads the verse back from the server and displays it. * Input: Username and Sentence. * Output: Username and Sentence. * Compile: g++ lab1.cpp -lsocket -lnsl -o lab1 * Execute: lab1 * *FYI: Palikir is capital of Micronesia * Truk, Chuuk a city/state in Micronesia * where many U.S. and Japanese WWII planes * are lying in the shallow waters *****************************/ #include #include #include #include #include #include #include #define PORT 51388 // Port on which server is listening #define HOST "einstein.franklin.edu" // Host where server is running #define BUFFER_LC 100 // max buffer length int main( int argc, char *argv[] ) { char data_buff[BUFFER_LC]; string player1, line_of_text, line; int sd; // socket descriptor int i; int linenum; struct sockaddr_in server_addr; // server address descriptor struct hostent *host_ptr; // server information structure int rc; // return code const int input_tries = 5; //5 tries to enter your name cout << "dasher1 51388" << endl; cout << "dasher1: Starting" << endl; cout << "dasher1: port = 51388" << endl; /*get info from target host*/ host_ptr = gethostbyname(HOST); if ( host_ptr == 0 ) { perror( "dasher1: Error in gethostbyname!" ); exit( EXIT_FAILURE ); } /*address structure for the server*/ server_addr.sin_family = AF_INET; server_addr.sin_port = htons(PORT);// port number memcpy(&server_addr.sin_addr, host_ptr->h_addr, host_ptr->h_length ); // IP address for( i=0; i<8; i++ ) server_addr.sin_zero[i] = 0; /*create socket*/ sd = socket(PF_INET, SOCK_STREAM, 0); if ( sd < 0 ) { perror( "dasher1: Error creating Socket!\n" ); exit( EXIT_FAILURE ); } /*connect to server*/ rc = connect( sd, (struct sockaddr *)&server_addr, sizeof(server_addr) ); if ( rc < 0 ) { perror( "dasher1: Error during Connection!\n" ); exit( EXIT_FAILURE ); } /*get player 1*/ /*validate players name against empty*/ cout << "Please enter your name: " << endl; for (int count = 1; ; count++) { getline(cin, player1); if (player1 != "") { break;//exit input loop } else if (count < input_tries) { cout << "Please try re-entering your name again..."; } else { cout << "Sorry, I gave you five tries - program terminated..." << endl; exit(1); } } if((player1 == "exit")) { cout << "Closing Connection." << endl; exit(1); } cout << "Welcome to the game, " << player1 << endl; /*get line of text from user*/ line = player1 + "\0\n";//\0 is null so compiler knows end of string //was: line = player1 + "/" + line_of_text + "/" + "\0\n";//\0 is null so compiler knows end of string line += "\n";//endline onn server rc = send(sd, line.c_str(), line.length()+0, 0); cout << "Please enter a line of text: \n"; getline( cin, line_of_text ); if(line_of_text == "exit") { cout << "Closing Connection." << endl; exit(1); } /*begin loop*/ for (; ;) { /*send message to server*/ line = line_of_text + "\0\n";//\0 is null so compiler knows end of string //was: line = player1 + "/" + line_of_text + "/" + "\0\n";//\0 is null so compiler knows end of string line += "\n";//endline onn server rc = send(sd, line.c_str(), line.length()+0, 0); if ( rc < 0 ) { perror( "dasher1: Error during Sending Process!\n" ); exit( EXIT_FAILURE ); } rc = recv( sd, data_buff, BUFFER_LC, 0 ); if( rc < 0 ) { perror( "dasher1: Error during Receiving!\n" ); exit( EXIT_FAILURE ); } cout << "rc = " << rc << endl; /*check for disconnect*/ if( rc == 0 ) { break; } /*copy answer out of buffer*/ line = data_buff; cout << line; cout << "You entered \"" + line_of_text + "\" enter another line: \n"; //rc = send(sd, line.c_str(), line.length()+0, 0); /*output of line 2 and all subsequnt lines potential bug: must hit return before entering new text*/ for (; ;) { /*did it this way to save message to server server knows if connection goes down*/ //player1="";//marker, not really needed //getline( cin, player1 ); getline(cin, line_of_text); if(line_of_text == "exit" ) { cout << "Closing Connection." << endl; exit(2); } cout << "You entered \"" + line_of_text + "\" enter another line: \n"; line = line_of_text + "\0\n"; //was: line = player1 + "/" + line_of_text + "/" + "\0\n";//\0 is null so compiler knows end of string line += "\n";//endline onn server rc = send( sd, line.c_str(), line.length()+0, 0 ); /*read line from server*/ /*clear the buffer*/ for( i = 0; i < BUFFER_LC; i++ ) data_buff[i] = 0; /*read a line from the server*/ rc = recv( sd, data_buff, BUFFER_LC, 0 ); if( rc < 0 ) { perror( "dasher1: Error during Receiving!\n" ); exit( EXIT_FAILURE ); } //cout << "rc = " << rc << endl; /*check for disconnect*/ if( rc == 0 ) { break; } /*copy answer out of buffer*/ line = data_buff; cout << line; } /*read line from server*/ } cout << "dasher1: Closing Connection!" << endl; close(sd); exit( EXIT_SUCCESS ); }