-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request Architecture from ArkadySK/architecture
Architecture
- Loading branch information
Showing
9 changed files
with
159 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
build/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# Project: Client | ||
add_executable(client client.c) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# Project: Server | ||
add_executable(server server.c) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
//TODO Waffle: implement |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
//TODO Adam: is it needed? |