Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Architecture #1

Merged
merged 5 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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?