// Owen Sullivan
// CPSC 3600-001 F23
// Assignment 1

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netdb.h>

#include "Practical.h"

// Main function responsible for primary server logic
int main(int argc, char **argv) {
    
    // Test for correct number of arguments
    if (argc != 2) {
        
        DieWithUserMessage("Parameter(s)", "<Server Port/Service>");
        
    }

    // First argument: local port
    char *port = argv[1];

    // Create socket for incoming connections
    int serverSocket = SetupTCPServerSocket(port);
    // Check that socket was created, error message if not
    if (serverSocket < 0) {
        
        DieWithUserMessage("SetupTCPServerSocket() failed", port);
        
    }

    // Loop infinitely
    while (true) {
        
        // New connection creates a connected client socket
        int clientSocket = AcceptTCPConnection(serverSocket);

        // Process client
        HandleTCPClient(clientSocket);

        // Close client socket
        close(clientSocket);

        break;
        
    }

    // Never reached
    close(serverSocket);
    
}