Skip to content

Commit

Permalink
Merge pull request Architecture from ArkadySK/architecture
Browse files Browse the repository at this point in the history
Architecture
  • Loading branch information
WaffleMaker9000 authored Jan 7, 2025
2 parents a742bc6 + 63b024b commit a56d1c7
Show file tree
Hide file tree
Showing 9 changed files with 159 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build/
14 changes: 14 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)

10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,13 @@ cmake ..
```
cmake --build .
```

3. Start the program
- client:
```
./bin/client
```
- server:
```
./bin/server
```
2 changes: 2 additions & 0 deletions client/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Project: Client
add_executable(client client.c)
57 changes: 57 additions & 0 deletions client/client.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>

#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;
}
2 changes: 2 additions & 0 deletions server/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Project: Server
add_executable(server server.c)
71 changes: 71 additions & 0 deletions server/server.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>

#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;
}
1 change: 1 addition & 0 deletions source/game_logic.c
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
//TODO Waffle: implement
1 change: 1 addition & 0 deletions source/utils.c
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
//TODO Adam: is it needed?

0 comments on commit a56d1c7

Please sign in to comment.