Skip to content
Snippets Groups Projects
Commit 40353e6d authored by weijiel6's avatar weijiel6
Browse files

new submitt one in 21:12

parent 371ef5ac
No related branches found
No related tags found
No related merge requests found
/* /*
** http-server.c ** http-server.c
** Code completed by Weijiel6
** finished time in 04/29/2019
*/ */
#include <errno.h> #include <errno.h>
...@@ -20,6 +22,7 @@ ...@@ -20,6 +22,7 @@
#include <sys/types.h> #include <sys/types.h>
#include <unistd.h> #include <unistd.h>
// used to check whether the game is finished
bool check_end(int sockfd, char* word_putin); bool check_end(int sockfd, char* word_putin);
// constants // constants
...@@ -31,11 +34,22 @@ static int const HTTP_400_LENGTH = 47; ...@@ -31,11 +34,22 @@ static int const HTTP_400_LENGTH = 47;
static char const * const HTTP_404 = "HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\n\r\n"; static char const * const HTTP_404 = "HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\n\r\n";
static int const HTTP_404_LENGTH = 45; static int const HTTP_404_LENGTH = 45;
// set two players active states
bool player1_r , player2_r; bool player1_r , player2_r;
// set tow players socket
int player1_s, player2_s; int player1_s, player2_s;
// save players write in keywords
char player1_w[2000][200], player2_w[2000][200]; char player1_w[2000][200], player2_w[2000][200];
// save player write in keywords number
int player1_w_len, player2_w_len; int player1_w_len, player2_w_len;
// flage for end the game
bool end_game; bool end_game;
//flage for quit the game
bool quit_game; bool quit_game;
// represents the types of method // represents the types of method
...@@ -48,8 +62,6 @@ typedef enum ...@@ -48,8 +62,6 @@ typedef enum
static bool handle_http_request(int sockfd) static bool handle_http_request(int sockfd)
{ {
//printf("Here is one socket goes in !");
//printf("%d\n", sockfd);
// try to read the request // try to read the request
char buff[2049]; char buff[2049];
int n = read(sockfd, buff, 2049); int n = read(sockfd, buff, 2049);
...@@ -62,11 +74,13 @@ static bool handle_http_request(int sockfd) ...@@ -62,11 +74,13 @@ static bool handle_http_request(int sockfd)
return false; return false;
} }
// if both two players disconnected, reset the flags
if (player1_s < 0 && player2_s < 0){ if (player1_s < 0 && player2_s < 0){
end_game = false; end_game = false;
quit_game = false; quit_game = false;
} }
// set the players socked when players coming in
if (player1_s < 0 && sockfd != player2_s) { if (player1_s < 0 && sockfd != player2_s) {
player1_s = sockfd; player1_s = sockfd;
} else if (player2_s < 0 && sockfd != player1_s) { } else if (player2_s < 0 && sockfd != player1_s) {
...@@ -137,6 +151,7 @@ static bool handle_http_request(int sockfd) ...@@ -137,6 +151,7 @@ static bool handle_http_request(int sockfd)
else if (method == POST) else if (method == POST)
{ {
if(strstr(buff, "quit=") != NULL) { if(strstr(buff, "quit=") != NULL) {
// if one player quit the game reset the information for it
if ( sockfd == player1_s) { if ( sockfd == player1_s) {
player1_s = -1; player1_s = -1;
player1_r = false; player1_r = false;
...@@ -175,8 +190,15 @@ static bool handle_http_request(int sockfd) ...@@ -175,8 +190,15 @@ static bool handle_http_request(int sockfd)
// locate the username, it is safe to do so in this sample code, but usually the result is expected to be // locate the username, it is safe to do so in this sample code, but usually the result is expected to be
// copied to another buffer using strcpy or strncpy to ensure that it will not be overwritten. // copied to another buffer using strcpy or strncpy to ensure that it will not be overwritten.
char * username = strstr(buff, "user=") + 5; char * username = strstr(buff, "user=") + 5;
char *buf = "<p>";
char *endbuf = "</p>\n\n";
// get the length
int username_length = strlen(username); int username_length = strlen(username);
// the length needs to include the ", " before the username int buf_length = strlen(buf);
int endbuf_length = strlen(endbuf);
// the length needs to include the "<p>"and "</p>\n\n" before the username
long added_length = username_length + 9; long added_length = username_length + 9;
// get the size of the file // get the size of the file
...@@ -201,24 +223,19 @@ static bool handle_http_request(int sockfd) ...@@ -201,24 +223,19 @@ static bool handle_http_request(int sockfd)
return false; return false;
} }
close(filefd); close(filefd);
//printf("let me see what in the file\n");
// move the trailing part backward // move the trailing part backward
int p1, p2; int p1, p2;
for (p1 = size - 1, p2 = p1 - added_length ; p1 >= size - 212; --p1, --p2) for (p1 = size - 1, p2 = p1 - added_length ; p1 >= size - 212; --p1, --p2)
buff[p1] = buff[p2]; buff[p1] = buff[p2];
// copy the username // copy the username
char *buf = "<p>";
int buf_length = strlen(buf); // write the "<p>" and "</p>\n\n"
strncpy(buff + p2, buf, buf_length); strncpy(buff + p2, buf, buf_length);
strncpy(buff + p2 + buf_length, username, username_length); strncpy(buff + p2 + buf_length, username, username_length);
char *endbuf = "</p>\n\n";
int endbuf_length = strlen(endbuf);
strncpy(buff + p2 + buf_length + username_length, endbuf, endbuf_length); strncpy(buff + p2 + buf_length + username_length, endbuf, endbuf_length);
buff[p2+ buf_length + username_length+endbuf_length] = '<';
/*for (int i =0; i < 2048;i++){ // set the last one to "<"
printf("%c",buff[i]); buff[p2+ buf_length + username_length+endbuf_length] = '<';
}*/
if (write(sockfd, buff, size) < 0) if (write(sockfd, buff, size) < 0)
{ {
perror("write"); perror("write");
...@@ -232,8 +249,7 @@ static bool handle_http_request(int sockfd) ...@@ -232,8 +249,7 @@ static bool handle_http_request(int sockfd)
else if (*curr == '?') { else if (*curr == '?') {
if (method == GET) if (method == GET)
{ {
printf("what is sockt goes in first turn in now?"); // change the players state
printf("%d\n", sockfd);
if (sockfd == player1_s) { if (sockfd == player1_s) {
player1_r = true; player1_r = true;
} else if (sockfd == player2_s) { } else if (sockfd == player2_s) {
...@@ -266,6 +282,7 @@ static bool handle_http_request(int sockfd) ...@@ -266,6 +282,7 @@ static bool handle_http_request(int sockfd)
} }
else if (method == POST) else if (method == POST)
{ {
// if one player quit the game reset the information for it
if(strstr(buff, "quit=") != NULL) { if(strstr(buff, "quit=") != NULL) {
if ( sockfd == player1_s) { if ( sockfd == player1_s) {
player1_s = -1; player1_s = -1;
...@@ -302,7 +319,11 @@ static bool handle_http_request(int sockfd) ...@@ -302,7 +319,11 @@ static bool handle_http_request(int sockfd)
close(filefd); close(filefd);
} }
else if (strstr(buff, "guess=") != NULL) { else if (strstr(buff, "guess=") != NULL) {
// get the keyward which would be wrote in
char * getword = strstr(buff, "keyword=") + 8; char * getword = strstr(buff, "keyword=") + 8;
// if the quit game flag is true would quit the game
if (quit_game) { if (quit_game) {
// get the size of the file // get the size of the file
struct stat st; struct stat st;
...@@ -329,6 +350,8 @@ static bool handle_http_request(int sockfd) ...@@ -329,6 +350,8 @@ static bool handle_http_request(int sockfd)
} }
close(filefd); close(filefd);
} }
// if the end game flag is ture would end the game
if(end_game) { if(end_game) {
if (sockfd == player1_s) { if (sockfd == player1_s) {
player1_s = -1; player1_s = -1;
...@@ -364,8 +387,14 @@ static bool handle_http_request(int sockfd) ...@@ -364,8 +387,14 @@ static bool handle_http_request(int sockfd)
} }
close(filefd); close(filefd);
} }
// if both players are ready, would send accepted file
if (player1_r && player2_r) { if (player1_r && player2_r) {
//if one guess the same word it would end the game
if (check_end(sockfd, getword)) { if (check_end(sockfd, getword)) {
// when the player end the game, reset the information for the player
if (sockfd == player1_s) { if (sockfd == player1_s) {
player1_s = -1; player1_s = -1;
player1_r = false; player1_r = false;
...@@ -400,7 +429,7 @@ static bool handle_http_request(int sockfd) ...@@ -400,7 +429,7 @@ static bool handle_http_request(int sockfd)
} }
close(filefd); close(filefd);
} else { } else {
// keep put ward in and guess
// get the size of the file // get the size of the file
struct stat st; struct stat st;
stat("4_accepted.html", &st); stat("4_accepted.html", &st);
...@@ -427,6 +456,7 @@ static bool handle_http_request(int sockfd) ...@@ -427,6 +456,7 @@ static bool handle_http_request(int sockfd)
close(filefd); close(filefd);
} }
} else { } else {
// not both players active so send discarded file
// get the size of the file // get the size of the file
struct stat st; struct stat st;
stat("5_discarded.html", &st); stat("5_discarded.html", &st);
...@@ -468,7 +498,7 @@ static bool handle_http_request(int sockfd) ...@@ -468,7 +498,7 @@ static bool handle_http_request(int sockfd)
return true; return true;
} }
// check the word list and change the end game flag and put the word into list
bool check_end(int sockfd, char* word_putin) { bool check_end(int sockfd, char* word_putin) {
if (sockfd == player1_s) { if (sockfd == player1_s) {
for (int i = 0; i < player2_w_len; i++){ for (int i = 0; i < player2_w_len; i++){
...@@ -502,6 +532,7 @@ int main(int argc, char * argv[]) ...@@ -502,6 +532,7 @@ int main(int argc, char * argv[])
fprintf(stderr, "image_tagger server is now running at IP: %s on port %s\n", argv[1], argv[2]); fprintf(stderr, "image_tagger server is now running at IP: %s on port %s\n", argv[1], argv[2]);
} }
//set all the global information for the plaers
player1_r = player2_r = false; player1_r = player2_r = false;
player1_s = player2_s = -1; player1_s = player2_s = -1;
......
File added
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment