Skip to content
Snippets Groups Projects
Commit 9767a5c8 authored by ChouTatsumi's avatar ChouTatsumi
Browse files

test for sockfd comming in

parent 3585c22d
No related branches found
No related tags found
No related merge requests found
......@@ -53,6 +53,7 @@ int create_server_socket(const char* ip, const int port);
static bool handle_http_request(int sockfd);
bool show_page(int sockfd, const char* htmldir);
bool show_start_page(int sockfd, char* username);
void print_html_request(char* request, int sockfd);
int main(int argc, char** argv) {
int port;
......@@ -79,7 +80,7 @@ void run_server(const char* ip, const int port) {
sockfd = create_server_socket(ip, port);
// listen on the socket
if (listen(sockfd, 2) < 0) {
if (listen(sockfd, 5) < 0) {
perror("listen");
exit(EXIT_FAILURE);
}
......@@ -187,9 +188,9 @@ static bool handle_http_request(int sockfd) {
char* curr = buff;
for (int i = 0; i < 2049; i++) {
printf("%c", curr[i]);
}
// print the HTML request for test
print_html_request(buff, sockfd);
// parse the method
METHOD method = UNKNOWN;
if (strncmp(curr, "GET ", 4) == 0) {
......@@ -224,7 +225,11 @@ static bool handle_http_request(int sockfd) {
if (method == GET) {
return show_page(sockfd, FIRST_TURN_PAGE);
}
// else if (method == POST) {}
else if (method == POST) {
if (strstr(buff, "quit=") != NULL) {
return show_page(sockfd, GAMEOVER_PAGE);
}
}
else
// never used, just for completeness
fprintf(stderr, "no other methods supported");
......@@ -318,3 +323,11 @@ bool show_start_page(int sockfd, char* username) {
return true;
}
void print_html_request(char* request, int sockfd) {
printf("New request from %d\r\n", sockfd);
for (int i = 0; i < strlen(request); i++) {
printf("%c", request[i]);
}
printf("\r\nEND\r\n");
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment