diff --git a/client.c b/client.c index 6c001a1..2b267ef 100644 --- a/client.c +++ b/client.c @@ -3,12 +3,12 @@ #include #include #ifdef __linux__ - #include - #include - #define SOCK int sock +#include +#include +#define SOCK int sock #elif _WIN32 - #include - #define SOCK unsigned int sock +#include +#define SOCK unsigned int sock #endif #include "include/main.h" @@ -30,46 +30,53 @@ char IP_ADDRESS[15]; char recv_msg[1024]; char send_msg[128]; -int client_connect() { // connect to the sock - #ifdef _WIN32 - WSADATA Data; - WSAStartup(MAKEWORD(2, 2), &Data); - #endif +int client_connect() +{ // connect to the sock +#ifdef _WIN32 + WSADATA Data; + WSAStartup(MAKEWORD(2, 2), &Data); +#endif struct sockaddr_in server_id; server_id.sin_addr.s_addr = inet_addr(IP_ADDRESS); server_id.sin_family = AF_INET; server_id.sin_port = htons(PORT); sock = socket(AF_INET, SOCK_STREAM, 0); - connect(sock, (struct sockaddr *) &server_id, sizeof(server_id)); + connect(sock, (struct sockaddr *)&server_id, sizeof(server_id)); return 0; } -void* client_comm() { // communicating data with the server (mostly receiving) +void *client_comm() +{ // communicating data with the server (mostly receiving) // initializing the game listen(sock, 1); - send(sock, (char *) &user_name, sizeof(user_name), 0); - recv(sock, (char *) &user0, sizeof(user0), 0); - recv(sock, (char *) &user1, sizeof(user1), 0); - recv(sock, (char *) &user_id, 4, 0); + send(sock, (char *)&user_name, sizeof(user_name), 0); + recv(sock, (char *)&user0, sizeof(user0), 0); + recv(sock, (char *)&user1, sizeof(user1), 0); + recv(sock, (char *)&user_id, 4, 0); // communication loop - while (1) { + while (1) + { // read game data - recv(sock, (char *) &is_game_over, 4, 0); - recv(sock, (char *) &turn, 4, 0); - recv(sock, (char *) &winsP0, 4, 0); - recv(sock, (char *) &winsP1, 4, 0); - recv(sock, (char *) &winner, 4, 0); - for (int i = 0; i < 9; i++) { - recv(sock, (char *) &game_grid[i], 4, 0); + recv(sock, (char *)&is_game_over, 4, 0); + recv(sock, (char *)&turn, 4, 0); + recv(sock, (char *)&winsP0, 4, 0); + recv(sock, (char *)&winsP1, 4, 0); + recv(sock, (char *)&winner, 4, 0); + for (int i = 0; i < 9; i++) + { + recv(sock, (char *)&game_grid[i], 4, 0); } // write events - if (turn == user_id) click_position = -1; // set click only if it's client's turn - send(sock, (char *) &click_position, 4, 0); - if (click_position >= 0 || is_game_over) click_position = -1; - send(sock, (char *) &ready, 4, 0); - if (ready == 1 && is_game_over == 0) ready = 0; + if (turn == user_id) + click_position = -1; // set click only if it's client's turn + send(sock, (char *)&click_position, 4, 0); + if (click_position >= 0 || is_game_over) + click_position = -1; + send(sock, (char *)&ready, 4, 0); + if (ready == 1 && is_game_over == 0) + ready = 0; } pthread_exit(NULL); } \ No newline at end of file diff --git a/gui.c b/gui.c index 82abc82..1475e1d 100644 --- a/gui.c +++ b/gui.c @@ -110,78 +110,9 @@ void join_window() EndDrawing(); } printf("%lu\n", strlen(IP_ADDRESS)); - if (strlen(IP_ADDRESS) < 1) - printf("ajdv"); - // sprintf(IP_ADDRESS, "127.0.0.1"); - /* int letterCountUser = 0, letterCountIP = 0; - while (!WindowShouldClose()) - { - if (IsKeyDown(KEY_ENTER)) - break; - if (CheckCollisionPointRec(GetMousePosition(), nickBox)) - { - SetMouseCursor(MOUSE_CURSOR_IBEAM); - int key = GetCharPressed(); - while (key > 0) - { - if ((key >= 32) && (key <= 125) && (letterCountUser < USERN_LENGTH)) - { - user_name[letterCountUser] = (char)key; - letterCountUser++; - } - key = GetCharPressed(); - } - if (IsKeyPressed(KEY_BACKSPACE)) - { - letterCountUser--; - if (letterCountUser < 0) - letterCountUser = 0; - user_name[letterCountUser] = '\0'; - } - } - if (CheckCollisionPointRec(GetMousePosition(), ipBox)) - { - SetMouseCursor(MOUSE_CURSOR_IBEAM); - int key = GetCharPressed(); - while (key > 0) - { - if ((key >= 32) && (key <= 125) && (letterCountIP < 15)) - { - IP_ADDRESS[letterCountIP] = (char)key; - letterCountIP++; - } - key = GetCharPressed(); - } - if (IsKeyPressed(KEY_BACKSPACE)) - { - letterCountIP--; - if (letterCountIP < 0) - letterCountIP = 0; - IP_ADDRESS[letterCountIP] = '\0'; - } - } - BeginDrawing(); - DrawText("Nickname:", 10, 10, 20, DARKGRAY); - DrawText("IP:", 10, 45, 20, DARKGRAY); - DrawRectangleRec(nickBox, LIGHTGRAY); - DrawRectangleRec(ipBox, LIGHTGRAY); - DrawText(user_name, nickBox.x + 5, nickBox.y + 4, 20, MAROON); - DrawText(IP_ADDRESS, ipBox.x + 5, ipBox.y + 4, 20, MAROON); - if (CheckCollisionPointRec(GetMousePosition(), nickBox)) - { - if (letterCountUser < USERN_LENGTH) - DrawText("|", nickBox.x + 8 + MeasureText(user_name, 20), nickBox.y + 6, 20, MAROON); - } - if (CheckCollisionPointRec(GetMousePosition(), ipBox)) - { - if (letterCountIP < 15) - DrawText("|", ipBox.x + 8 + MeasureText(IP_ADDRESS, 20), ipBox.y + 6, 20, MAROON); - } - ClearBackground(RAYWHITE); - EndDrawing(); - } */ + if (strlen(IP_ADDRESS) <= 1) + sprintf(IP_ADDRESS, "127.0.0.1"); CloseWindow(); - // if (letterCountIP < 4) sprintf(letterCountIP, "127.0.0.1"); // for default ip, might add offline mode later return; } diff --git a/main.c b/main.c index e7f9f59..7f834a1 100644 --- a/main.c +++ b/main.c @@ -29,13 +29,13 @@ char user_name[USERN_LENGTH] = {0}; char user0[USERN_LENGTH]; char user1[USERN_LENGTH]; Rectangle game[9]; +pthread_t tid[4]; void log_level() {} int main() { join_window(); client_connect(); - pthread_t tid[4]; pthread_create(&tid[0], 0, client_comm, NULL); pthread_create(&tid[1], 0, window_main, NULL); for (int i = 0; i <= 1; i++) @@ -64,6 +64,7 @@ void *window_main() //DrawFPS(10, 10); EndDrawing(); } + pthread_cancel(tid[0]); // end of the program CloseWindow(); close(sock); diff --git a/server.c b/server.c index 8ddd10d..9258264 100644 --- a/server.c +++ b/server.c @@ -20,9 +20,10 @@ int winner = 0; char user0[USERN_LENGTH]; char user1[USERN_LENGTH]; -void* communication(void*); +void *communication(void *); -int main(void) { +int main(void) +{ // creating socket and connecting to it struct sockaddr_in address; int server_fd, opt = 1, addrlen = sizeof(address); @@ -31,64 +32,75 @@ int main(void) { address.sin_family = AF_INET; address.sin_addr.s_addr = INADDR_ANY; address.sin_port = htons(PORT); - if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0) return -1; + if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0) + return -1; // accepting clients listen(server_fd, 3); - clientfd[0] = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen); - recv(clientfd[0], (char *) &user0, sizeof(user0), 0); + clientfd[0] = accept(server_fd, (struct sockaddr *)&address, (socklen_t *)&addrlen); + recv(clientfd[0], (char *)&user0, sizeof(user0), 0); printf("Connected %s\n", user0); listen(server_fd, 3); - clientfd[1] = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen); - recv(clientfd[1], (char *) &user1, sizeof(user1), 0); + clientfd[1] = accept(server_fd, (struct sockaddr *)&address, (socklen_t *)&addrlen); + recv(clientfd[1], (char *)&user1, sizeof(user1), 0); printf("Connected %s\n", user1); // initializing connection listen(server_fd, 3); - send(clientfd[0], (char *) &user0, sizeof(user0), 0); - send(clientfd[0], (char *) &user1, sizeof(user1), 0); - send(clientfd[0], (char *) &turn, 4, 0); + send(clientfd[0], (char *)&user0, sizeof(user0), 0); + send(clientfd[0], (char *)&user1, sizeof(user1), 0); + send(clientfd[0], (char *)&turn, 4, 0); turn++; - send(clientfd[1], (char *) &user0, sizeof(user0), 0); - send(clientfd[1], (char *) &user1, sizeof(user1), 0); - send(clientfd[1], (char *) &turn, 4, 0); + send(clientfd[1], (char *)&user0, sizeof(user0), 0); + send(clientfd[1], (char *)&user1, sizeof(user1), 0); + send(clientfd[1], (char *)&turn, 4, 0); // creating and joining threads pthread_t tid[2]; - for (int i = 0; i <= 1; i++) { + for (int i = 0; i <= 1; i++) + { int *arg = malloc(sizeof(*arg)); *arg = i; pthread_create(&tid[i], 0, communication, arg); } - for (int i = 0; i <= 1; i++) pthread_join(tid[i], NULL); + for (int i = 0; i <= 1; i++) + pthread_join(tid[i], NULL); return 0; } -void* communication(void* arg) { // communicating data with the client (mostly sending) - int i = *((int *) arg); +void *communication(void *arg) +{ // communicating data with the client (mostly sending) + int i = *((int *)arg); free(arg); - while (1) { + while (1) + { // send game data - send(clientfd[i], (char *) &is_game_over, 4, 0); - send(clientfd[i], (char *) &turn, 4, 0); - send(clientfd[i], (char *) &winsP0, 4, 0); - send(clientfd[i], (char *) &winsP1, 4, 0); - send(clientfd[i], (char *) &winner, 4, 0); - for (int j = 0; j < 9; j++) { + send(clientfd[i], (char *)&is_game_over, 4, 0); + send(clientfd[i], (char *)&turn, 4, 0); + send(clientfd[i], (char *)&winsP0, 4, 0); + send(clientfd[i], (char *)&winsP1, 4, 0); + send(clientfd[i], (char *)&winner, 4, 0); + for (int j = 0; j < 9; j++) + { send(clientfd[i], &game_grid[j], 4, 0); } // read events - recv(clientfd[i], (char *) &click, 4, 0); - recv(clientfd[i], (char *) &ready_check[i], sizeof(ready_check), 0); + recv(clientfd[i], (char *)&click, 4, 0); + recv(clientfd[i], (char *)&ready_check[i], sizeof(ready_check), 0); winner = checkwinner(); - if (turn == i) click = -1; // accepting click only from player's turn client - if (click != -1 && game_grid[click] == 0 && is_game_over == 0 ) { // handling clicks - if (turn) game_grid[click] = 1; - else game_grid[click] = 2; + if (turn == i) + click = -1; // accepting click only from player's turn client + if (click != -1 && game_grid[click] == 0 && is_game_over == 0) + { // handling clicks + if (turn) + game_grid[click] = 1; + else + game_grid[click] = 2; turn = !turn; } - if (ready_check[0] && ready_check[1]) endGame(winner); // checks if all clients are ready to continue + if (ready_check[0] && ready_check[1]) + endGame(winner); // checks if all clients are ready to continue } close(clientfd[i]); pthread_exit(NULL);