diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..5b521d0587015867ac1a23fa00be3f3fce080b9f --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +a6 diff --git a/magic.c b/Examples/magic.c similarity index 100% rename from magic.c rename to Examples/magic.c diff --git a/Main.c b/Main.c index a6e42bfc3f92590d602ce5bac942b439d460a6bb..bb8bd9f71e0789b8c1443522ce69a682a80ee52e 100644 --- a/Main.c +++ b/Main.c @@ -1,8 +1,8 @@ /** * Brandon Rodriguez * CS 3240 - * - * a (Assignment ) + * 12-05-17 + * a6 (Assignment 7) */ @@ -19,21 +19,26 @@ // Import headers. +#include <ctype.h> +#include <netdb.h> +#include <pthread.h> #include <stdio.h> #include <string.h> #include <stdlib.h> -#include <ctype.h> +#include <sys/types.h> +#include <sys/socket.h> #include "apue.h" // Define Vars. -//#define ??? +#define BUFFER_SIZE 4096 // Variables. // Method Declaration. +void* serve_request(); /** @@ -41,5 +46,93 @@ * Initializes and runs program. */ int main(int argc, char* argv[]) { + int return_int; + int listen_fd; + int con_fd; + unsigned short port_number; + char* media_path; + struct sockaddr_in server_address; + socklen_t address_len; + pthread_t* thread_array; + pthread_attr_t attr; + size_t stacksize; + + // Check that valid number of args have been given. + if (argc < 2) { + printf("Need to provide port number.\n"); + exit(1); + } else if (argc > 2) { + printf("Please provide exactly one arg of port number.\n"); + exit(1); + } + + printf("Building socket...\n"); + + port_number = atoi(argv[1]); + address_len = sizeof(server_address); + + // Get path to media. + media_path = calloc(BUFFER_SIZE, sizeof(char*)); + if (getcwd(media_path, BUFFER_SIZE) == NULL) { + err_sys("Failed to get absolute path."); + } else { + strcat(media_path, "/Media"); + } + printf("Media Path: %s\n", media_path); + + // Creates socket and sets listener file descriptor. + if ((listen_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) { + err_sys("Failed to set socket."); + } + + // Sets bytes at memory location to 0. Mandatory, supposedly. + memset(&server_address, '0', sizeof(server_address)); + + // Set server info. + server_address.sin_family = AF_INET; // Addressing scheme. + server_address.sin_addr.s_addr = htons(INADDR_ANY); // Allow any ip to connect. + server_address.sin_port = htons(port_number); // Port to listen on. + + // Prepare to listen. + return_int = bind(listen_fd, (struct sockaddr *) &server_address, sizeof(server_address)); + if (return_int < 0) { + err_sys("Failed to bind to socket."); + } + + // Actually listen for connections. + return_int = listen(listen_fd, 10); + if (return_int < 0) { + err_sys("Error on listen attempt."); + } + + printf("Successfully built socket. Setting up threading...\n"); + + // Setup threading. + thread_array = calloc(100, sizeof(pthread_t)); + pthread_attr_init(&attr); + pthread_attr_getstacksize(&attr, &stacksize); + if (stacksize < 8388608) { + stacksize = 8388608; + } + pthread_attr_setstacksize(&attr, (stacksize * 2)); + + printf("Threading established. Awaiting connection...\n"); + + // Run indefinitely, accepting new connections and creating new threads for each. + while (1) { + // Check for successful connection. + if ((con_fd = accept(listen_fd, (struct sockaddr*) &server_address, (socklen_t*) &address_len)) >= 0) { + printf("Recieving request...\n"); + + // A connection has been established. Open new thread. + pthread_create(&thread_array[0], &attr, serve_request, (void*) NULL); + } + } +} + +void* serve_request() { + printf("Request recieved and thread created!\n"); + printf("Not doing anything about it though. Terminating thread.\n"); + pthread_exit((void*) NULL); } diff --git a/makefile b/makefile index 2998bfcf13a8e16f26412c9f9817595e8e2cbd0e..8cd152cf11d15f66316b607e3112fde8c83fc154 100644 --- a/makefile +++ b/makefile @@ -1,2 +1,2 @@ all: - gcc -Wall -Wpedantic -std=c99 *.c -g -o a6 + gcc -Wall -Wpedantic -std=c99 *.c -g -o a6 -pthread -lmagic