diff --git a/Main.c b/Main.c index 9acd308462512791259df0349815b888941a7e1c..4f4bf7631f9f19921925a60ad9641b7ea7a660a4 100644 --- a/Main.c +++ b/Main.c @@ -186,7 +186,7 @@ int main(int argc, char* argv[]) { // A connection has been established. Open new thread. char* ip = inet_ntoa(client_address->sin_addr); - printf("Client IP: %s\n", ip); + printf("Client IP: %s Conn FD: %d\n", ip, *conn_fd); pthread_create(&thread_array[0], &attr, serve_request, (void*) &((struct conn_obj) {copy_int(&index), conn_fd, client_address})); } @@ -211,6 +211,7 @@ void* serve_request(struct conn_obj* connection) { thread_joinable[*connection->index] = 1; free(connection->index); free(connection->client_address); + close(*connection->conn_fd); // Exit thread. pthread_exit((void*) NULL); @@ -240,23 +241,31 @@ void join_threads() { /** * Generates and sends http response to requesting client. */ -void send_response(struct conn_obj connection) { +void send_response(struct conn_obj* connection) { char* temp_string; - char* response_buffer = calloc(BUFFER_SIZE, sizeof(char*)); - - strcat(response_buffer, "HTTP/1.2 200 OK\r\n"); - - strcat(response_buffer, "Host: "); - temp_string = inet_ntoa(server_address.sin_addr); - strcat(response_buffer, temp_string); - strcat(response_buffer, ":"); - temp_string = calloc(10, sizeof(char*)); - sprintf(temp_string, "%u", port_number); - strcat(response_buffer, temp_string); - strcat(response_buffer, "\r\n"); - strcat(response_buffer, "Content-Type: text/html\r\n"); - strcat(response_buffer, "\r\n"); - - printf("%s\n", response_buffer); - // write(*connection.conn_fd, response_buffer, (strlen(response_buffer) + 1)); + char* response_buffer_1 = calloc(BUFFER_SIZE, sizeof(char*)); + char* response_buffer_2 = calloc(BUFFER_SIZE, sizeof(char*)); + + printf("Creating and sending response...\n\n"); + + // First setup desired content. + strcat(response_buffer_2, "<body><h1>This is a response.</h1></body>"); + + // Then set up response header. + strcat(response_buffer_1, "HTTP/1.2 200 Okay\r\n"); + strcat(response_buffer_1, "Server: Cat/1.0.0"); + strcat(response_buffer_1, "\r\n"); + strcat(response_buffer_1, "Content-Type: text/html\r\n"); + strcat(response_buffer_1, "Content-Length: "); + temp_string = calloc(BUFFER_SIZE, sizeof(char)); + sprintf(temp_string, "%ld", strlen(response_buffer_2)); + strcat(response_buffer_1, temp_string); + strcat(response_buffer_1, "\r\n\r\n"); + + + printf("%s\n", response_buffer_1); + // Send header. + send(*connection->conn_fd, response_buffer_1, (strlen(response_buffer_1) + 1), 0); + // Send actual content. + send(*connection->conn_fd, response_buffer_2, (strlen(response_buffer_2) + 1), 0); } diff --git a/Main_No_Threads.c b/Main_No_Threads.c index 826662d797284079f589179c6c24da3108b6889e..18b541e41882ca1be2b516bf4315285684d8c346 100644 --- a/Main_No_Threads.c +++ b/Main_No_Threads.c @@ -155,6 +155,8 @@ int main(int argc, char* argv[]) { send(*conn_fd, response_buffer_1, (strlen(response_buffer_1) + 1), 0); // Send actual content. send(*conn_fd, response_buffer_2, (strlen(response_buffer_2) + 1), 0); + + close(*conn_fd); } } }