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 diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..81575bf --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,14 @@ +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(game_logic STATIC source/game_logic.c) +add_library(utils STATIC source/utils.c) + +# Projects +add_subdirectory(client) +add_subdirectory(server) + 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/CMakeLists.txt b/client/CMakeLists.txt new file mode 100644 index 0000000..e4552ef --- /dev/null +++ b/client/CMakeLists.txt @@ -0,0 +1,2 @@ +# Project: Client +add_executable(client client.c) \ 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/CMakeLists.txt b/server/CMakeLists.txt new file mode 100644 index 0000000..b42b4be --- /dev/null +++ b/server/CMakeLists.txt @@ -0,0 +1,2 @@ +# Project: Server +add_executable(server server.c) \ No newline at end of file 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/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/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