Skip to content

Commit

Permalink
Merge pull request #7 from ArkadySK/custom-ports
Browse files Browse the repository at this point in the history
Added custom port functionality
  • Loading branch information
ArkadySK authored Jan 11, 2025
2 parents a81f2d1 + 3b80277 commit 73016f8
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 15 deletions.
17 changes: 10 additions & 7 deletions client/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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");
Expand Down Expand Up @@ -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
Expand All @@ -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);
}
Expand Down
15 changes: 9 additions & 6 deletions server/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@
#include <stdatomic.h>
#include <stdbool.h>

#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;
Expand All @@ -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)
Expand Down Expand Up @@ -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)
{
Expand Down
4 changes: 2 additions & 2 deletions source/menu.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

0 comments on commit 73016f8

Please sign in to comment.