Skip to content
Snippets Groups Projects
Select Git revision
  • a2e1a5579f9f9bad1ba4016aff99c05b6a06266c
  • master default protected
2 results

image_tagger.c

Blame
  • image_tagger.c 16.62 KiB
    #include <errno.h>
    #include <stdbool.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <stddef.h>
    #include <arpa/inet.h>
    #include <fcntl.h>
    #include <netdb.h>
    #include <netinet/in.h>
    #include <strings.h>
    #include <sys/select.h>
    #include <sys/sendfile.h>
    #include <sys/socket.h>
    #include <sys/stat.h>
    #include <sys/types.h>
    #include <unistd.h>
    
    // constants
    static char const * const HTTP_200_FORMAT = "HTTP/1.1 200 OK\r\n\
    Content-Type: text/html\r\n\
    Content-Length: %ld\r\n\r\n";
    static char const * const HTTP_400 = "HTTP/1.1 400 Bad Request\r\nContent-Length: 0\r\n\r\n";
    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 int const HTTP_404_LENGTH = 45;
    
    int player1;
    int player2;
    
    bool player1_ready;
    bool player2_ready;
    
    char player1_k[2005][205];
    char player2_k[2005][205];
    
    int player1_knum;
    int player2_knum;
    
    // represents the types of method
    typedef enum
    {
        GET,
        POST,
        UNKNOWN
    } METHOD;
    
    static bool handle_http_request(int sockfd)
    {
        // try to read the request
        char buff[2049];
        int n = read(sockfd, buff, 2049);
        //fwrite(buff,1,2049,stdout);
        if (n <= 0)
        {
            if (n < 0)
                perror("read");
            else
                printf("socket %d close the connection\n", sockfd);
            return false;
        }
        if( player1 <0 && player2 != sockfd){
          player1 = sockfd;
        }
        if( player2 <0 && player1 != sockfd){
          player2 = sockfd;
        }
    
        // terminate the string
        buff[n] = 0;