diff --git a/client/client.c b/client/client.c index 5bb3eeb..46b5672 100644 --- a/client/client.c +++ b/client/client.c @@ -9,10 +9,9 @@ #include "../source/menu.h" #include "../source/utils.h" -#define PORT 8536 #define SERVER_IP "127.0.0.1" //localhost -int initialize_client() { +int initialize_client(int port) { int sock; struct sockaddr_in server_addr; @@ -24,7 +23,7 @@ int initialize_client() { // Set server address server_addr.sin_family = AF_INET; - server_addr.sin_port = htons(PORT); + server_addr.sin_port = htons(port); if (inet_pton(AF_INET, SERVER_IP, &server_addr.sin_addr) <= 0) { perror("IC: Invalid address or address not supported"); @@ -64,7 +63,7 @@ void play_game(int sock) { board_destroy(&b_enemy); } -int main() { +int main(int argc, char** argv) { int mode; // Handle menu @@ -75,17 +74,21 @@ int main() { clear_screen(); if (mode == 1) { // Computer game mode - show_message("Computer mode not yet implemented\n"); + show_message("Computer mode not yet implemented"); sleep(3); clear_screen(); //TODO Waffle: implement return 0; } + int port = 8536; + if(argc >= 2) + port = atoi(argv[1]); + if (mode == 2) { // Human vs Human (network) mode - show_message("Please wait, connecting...\n"); + show_message("Please wait, connecting..."); sleep(1); - int sock = initialize_client(); + int sock = initialize_client(port); play_game(sock); close(sock); } diff --git a/server/server.c b/server/server.c index 7616b26..8b35245 100644 --- a/server/server.c +++ b/server/server.c @@ -8,12 +8,11 @@ #include #include -#define PORT 8536 #define MAX_PLAYERS 2 atomic_int player_count = 0; // Atomic variable to track connected players -int initialize_server() +int initialize_server(int port) { int server_fd; struct sockaddr_in address; @@ -29,7 +28,7 @@ int initialize_server() // Set server address address.sin_family = AF_INET; address.sin_addr.s_addr = INADDR_ANY; - address.sin_port = htons(PORT); + address.sin_port = htons(port); // Bind the socket to the address if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0) @@ -71,11 +70,15 @@ void *handle_client(void *arg) return NULL; } -int main() +int main(int argc, char** argv) { - int server_fd = initialize_server(); + int port = 8536; + if(argc >= 2) + port = atoi(argv[1]); - printf("Server is running on port %d\n", PORT); + int server_fd = initialize_server(port); + + printf("Server is running on port %d\n", port); while (true) { diff --git a/source/menu.c b/source/menu.c index 2806134..1b4fcdc 100644 --- a/source/menu.c +++ b/source/menu.c @@ -45,6 +45,6 @@ int handle_menu(void) { void show_message(char* message) { clear_screen(); printf(" ------------------------------- \n "); - printf(message); - printf(" ------------------------------- \n"); + printf("%s", message); + printf("\n ------------------------------- \n"); }