From 880f69ce7839a9ab5f01cbcfb8930afe84e4aff9 Mon Sep 17 00:00:00 2001 From: adam majerik Date: Mon, 6 Jan 2025 12:25:22 +0100 Subject: [PATCH 1/5] added cmake --- CMakeLists.txt | 15 +++++++++++++++ client/CMakeLists.txt | 5 +++++ server/CMakeLists.txt | 5 +++++ 3 files changed, 25 insertions(+) create mode 100644 CMakeLists.txt create mode 100644 client/CMakeLists.txt create mode 100644 server/CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..3280dce --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,15 @@ +cmake_minimum_required(VERSION 3.22) +project(Battleship VERSION 0.1.0 LANGUAGES C) + +# Binary +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) + +# Libs +add_library(ipc STATIC source/ipc.c) +add_library(game_logic STATIC source/game_logic.c) +add_library(utils STATIC source/utils.c) + +# Projects +add_subdirectory(client) +add_subdirectory(server) + diff --git a/client/CMakeLists.txt b/client/CMakeLists.txt new file mode 100644 index 0000000..2011029 --- /dev/null +++ b/client/CMakeLists.txt @@ -0,0 +1,5 @@ +# Project: Client +add_executable(client client.c) + +# Libs +target_link_libraries(client PUBLIC ipc game_logic utils) diff --git a/server/CMakeLists.txt b/server/CMakeLists.txt new file mode 100644 index 0000000..46e2012 --- /dev/null +++ b/server/CMakeLists.txt @@ -0,0 +1,5 @@ +# Project: Server +add_executable(server server.c) + +# Libs +target_link_libraries(server PUBLIC ipc game_logic utils) From 969b140c0c5594193ba4aee210a3919d6b44ff4e Mon Sep 17 00:00:00 2001 From: adam majerik Date: Mon, 6 Jan 2025 15:06:07 +0100 Subject: [PATCH 2/5] added ipc, game logic and utils classes --- source/game_logic.c | 1 + source/ipc.c | 20 ++++++++++++++++++++ source/utils.c | 1 + 3 files changed, 22 insertions(+) create mode 100644 source/game_logic.c create mode 100644 source/ipc.c create mode 100644 source/utils.c diff --git a/source/game_logic.c b/source/game_logic.c new file mode 100644 index 0000000..9027c67 --- /dev/null +++ b/source/game_logic.c @@ -0,0 +1 @@ +//TODO Waffle: implement \ No newline at end of file diff --git a/source/ipc.c b/source/ipc.c new file mode 100644 index 0000000..27a61d3 --- /dev/null +++ b/source/ipc.c @@ -0,0 +1,20 @@ +#include +#include + +//IPC: Inter-Process Communication + +// Init server +int initialize_server() { + //TODO Adam: imlement +} + +// Send or receive messages +void communicate() { + //TODO Adam: implement +} + +// Main +int main() { + //TODO Adam: implement + return 0; +} diff --git a/source/utils.c b/source/utils.c new file mode 100644 index 0000000..c6eac62 --- /dev/null +++ b/source/utils.c @@ -0,0 +1 @@ +//TODO Adam: is it needed? \ No newline at end of file From a0766d728c6566113695342c9bbf388efef5b6ad Mon Sep 17 00:00:00 2001 From: ArkadySK Date: Mon, 6 Jan 2025 15:38:50 +0100 Subject: [PATCH 3/5] added .gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d163863 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +build/ \ No newline at end of file From 5c37eeff216683992bb317f608a6f3d5a1e6c6db Mon Sep 17 00:00:00 2001 From: ArkadySK Date: Mon, 6 Jan 2025 17:17:17 +0100 Subject: [PATCH 4/5] added client and server, improved communication --- README.md | 10 ++++++ client/client.c | 57 ++++++++++++++++++++++++++++++++ server/server.c | 71 ++++++++++++++++++++++++++++++++++++++++ source/ipc.c | 86 +++++++++++++++++++++++++++++++++++++------------ 4 files changed, 204 insertions(+), 20 deletions(-) create mode 100644 client/client.c create mode 100644 server/server.c diff --git a/README.md b/README.md index b249acd..8c92efa 100644 --- a/README.md +++ b/README.md @@ -40,3 +40,13 @@ cmake .. ``` cmake --build . ``` + +3. Start the program +- client: +``` +./bin/client +``` +- server: +``` +./bin/server +``` \ No newline at end of file diff --git a/client/client.c b/client/client.c new file mode 100644 index 0000000..28bd043 --- /dev/null +++ b/client/client.c @@ -0,0 +1,57 @@ +#include +#include +#include +#include +#include +#include + +#define PORT 8080 +#define SERVER_IP "127.0.0.1" //localhost + +int initialize_client() { + int sock; + struct sockaddr_in server_addr; + + // Create socket + if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) { + perror("IC: Socket creation failed"); + exit(EXIT_FAILURE); + } + + // Set server address + server_addr.sin_family = AF_INET; + 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"); + exit(EXIT_FAILURE); + } + + // Connect to server + if (connect(sock, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) { + perror("IC: Connection failed"); + exit(EXIT_FAILURE); + } + + return sock; +} + +void communicate(int sock) { + char *message = "Hello from client!"; + char buffer[1024] = {0}; + + // Send message to server + send(sock, message, strlen(message), 0); + printf("Client: Message sent to server: %s\n", message); + + // Receive response from server + read(sock, buffer, 1024); + printf("Client: Message from server: %s\n", buffer); +} + +int main() { + int sock = initialize_client(); + communicate(sock); + close(sock); + return 0; +} diff --git a/server/server.c b/server/server.c new file mode 100644 index 0000000..db5864d --- /dev/null +++ b/server/server.c @@ -0,0 +1,71 @@ +#include +#include +#include +#include +#include +#include + +#define PORT 8080 + +int initialize_server() { + int server_fd; + struct sockaddr_in address; + int opt = 1; + + // Create socket + if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) { + perror("IS: Socket failed"); + exit(EXIT_FAILURE); + } + + // Set server address + address.sin_family = AF_INET; + address.sin_addr.s_addr = INADDR_ANY; + address.sin_port = htons(PORT); + + // Binds the socket to the address + if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0) { + perror("IS: Bind failed"); + exit(EXIT_FAILURE); + } + + // Server starts listening for connections + if (listen(server_fd, 3) < 0) { + perror("IS: Listen failed"); + exit(EXIT_FAILURE); + } + + return server_fd; +} + +void handle_client(int client_socket) { + char buffer[1024] = {0}; + + // Read message from client + read(client_socket, buffer, 1024); + printf("HC: Message from client: %s\n", buffer); + + // Send response to client + send(client_socket, "Hello from server", strlen("Hello from server"), 0); + printf("HC: Message sent to client\n"); + + close(client_socket); +} + +int main() { + int server_fd = initialize_server(); + int new_socket; + struct sockaddr_in address; + int addrlen = sizeof(address); + + // Accept connection from client + if ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen)) < 0) { + perror("Server: Accept failed"); + exit(EXIT_FAILURE); + } + + handle_client(new_socket); + + close(server_fd); + return 0; +} diff --git a/source/ipc.c b/source/ipc.c index 27a61d3..bb45d70 100644 --- a/source/ipc.c +++ b/source/ipc.c @@ -1,20 +1,66 @@ -#include -#include - -//IPC: Inter-Process Communication - -// Init server -int initialize_server() { - //TODO Adam: imlement -} - -// Send or receive messages -void communicate() { - //TODO Adam: implement -} - -// Main -int main() { - //TODO Adam: implement - return 0; -} +#include +#include +#include +#include +#include +#include +#include + +//IPC: Inter-Process Communication + +#define PORT 8080 + +// Function to initialize server socket +int initialize_server_socket() { + int server_fd; + struct sockaddr_in address; + int opt = 1; + + if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) { + perror("ISS: Socket failed"); + exit(EXIT_FAILURE); + } + + 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) { + perror("ISS: Bind failed"); + exit(EXIT_FAILURE); + } + + if (listen(server_fd, 3) < 0) { + perror("ISS: Listen failed"); + exit(EXIT_FAILURE); + } + + return server_fd; +} + +// Function to initialize client socket +int initialize_client_socket() { + int sock; + struct sockaddr_in server_addr; + const char *localhost_ip_address = "127.0.0.1"; // localhost address + + if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) { + perror("ICS: Socket creation failed"); + exit(EXIT_FAILURE); + } + + server_addr.sin_family = AF_INET; + server_addr.sin_port = htons(PORT); + + if (inet_pton(AF_INET, localhost_ip_address, &server_addr.sin_addr) <= 0) { + perror("ICS: Invalid address or address not supported"); + exit(EXIT_FAILURE); + } + + if (connect(sock, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) { + perror("ICS: Connection failed"); + exit(EXIT_FAILURE); + } + + return sock; +} From 63b024b1ff396d50cc752ef5a6be628a8d2cae16 Mon Sep 17 00:00:00 2001 From: Adam Majerik Date: Tue, 7 Jan 2025 13:55:47 +0100 Subject: [PATCH 5/5] removed ipc library --- CMakeLists.txt | 1 - client/CMakeLists.txt | 5 +--- server/CMakeLists.txt | 5 +--- source/ipc.c | 66 ------------------------------------------- 4 files changed, 2 insertions(+), 75 deletions(-) delete mode 100644 source/ipc.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 3280dce..81575bf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,7 +5,6 @@ project(Battleship VERSION 0.1.0 LANGUAGES C) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) # Libs -add_library(ipc STATIC source/ipc.c) add_library(game_logic STATIC source/game_logic.c) add_library(utils STATIC source/utils.c) diff --git a/client/CMakeLists.txt b/client/CMakeLists.txt index 2011029..e4552ef 100644 --- a/client/CMakeLists.txt +++ b/client/CMakeLists.txt @@ -1,5 +1,2 @@ # Project: Client -add_executable(client client.c) - -# Libs -target_link_libraries(client PUBLIC ipc game_logic utils) +add_executable(client client.c) \ No newline at end of file diff --git a/server/CMakeLists.txt b/server/CMakeLists.txt index 46e2012..b42b4be 100644 --- a/server/CMakeLists.txt +++ b/server/CMakeLists.txt @@ -1,5 +1,2 @@ # Project: Server -add_executable(server server.c) - -# Libs -target_link_libraries(server PUBLIC ipc game_logic utils) +add_executable(server server.c) \ No newline at end of file diff --git a/source/ipc.c b/source/ipc.c deleted file mode 100644 index bb45d70..0000000 --- a/source/ipc.c +++ /dev/null @@ -1,66 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include - -//IPC: Inter-Process Communication - -#define PORT 8080 - -// Function to initialize server socket -int initialize_server_socket() { - int server_fd; - struct sockaddr_in address; - int opt = 1; - - if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) { - perror("ISS: Socket failed"); - exit(EXIT_FAILURE); - } - - 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) { - perror("ISS: Bind failed"); - exit(EXIT_FAILURE); - } - - if (listen(server_fd, 3) < 0) { - perror("ISS: Listen failed"); - exit(EXIT_FAILURE); - } - - return server_fd; -} - -// Function to initialize client socket -int initialize_client_socket() { - int sock; - struct sockaddr_in server_addr; - const char *localhost_ip_address = "127.0.0.1"; // localhost address - - if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) { - perror("ICS: Socket creation failed"); - exit(EXIT_FAILURE); - } - - server_addr.sin_family = AF_INET; - server_addr.sin_port = htons(PORT); - - if (inet_pton(AF_INET, localhost_ip_address, &server_addr.sin_addr) <= 0) { - perror("ICS: Invalid address or address not supported"); - exit(EXIT_FAILURE); - } - - if (connect(sock, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) { - perror("ICS: Connection failed"); - exit(EXIT_FAILURE); - } - - return sock; -}