From f9f4bf5ffab597d14f57624fcd2d35ba8ba85f95 Mon Sep 17 00:00:00 2001 From: Bruno Moretti Date: Thu, 27 Jun 2024 15:08:46 -0300 Subject: [PATCH] adding Client and Server classes --- include/.gitkeep | 0 include/Client.hpp | 33 +++++++++++++++++++++++++ include/Server.hpp | 37 ++++++++++++++++++++++++++++ include/common.hpp | 57 +++++++++++++++++++++++++++++++++++++++++++ src/Client.cpp | 38 +++++++++++++++++++++++++++++ src/Server.cpp | 61 ++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 226 insertions(+) delete mode 100644 include/.gitkeep create mode 100644 include/Client.hpp create mode 100644 include/Server.hpp create mode 100644 include/common.hpp create mode 100644 src/Client.cpp create mode 100644 src/Server.cpp diff --git a/include/.gitkeep b/include/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/include/Client.hpp b/include/Client.hpp new file mode 100644 index 0000000..4090c3f --- /dev/null +++ b/include/Client.hpp @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Client.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bmoretti +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/27 14:40:02 by bmoretti #+# #+# */ +/* Updated: 2024/06/27 15:05:00 by bmoretti ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef CLIENT_HPP +# define CLIENT_HPP + +# include "common.hpp" + +class Client +{ +public: + Client(); + ~Client(); + void connectToServer(const int serverSocket); + +private: + int _clientSocket; + struct sockaddr_in _address; + + void _createClientSocket(void); + +}; + +#endif diff --git a/include/Server.hpp b/include/Server.hpp new file mode 100644 index 0000000..0b18705 --- /dev/null +++ b/include/Server.hpp @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Server.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bmoretti +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/27 10:40:41 by bmoretti #+# #+# */ +/* Updated: 2024/06/27 15:04:14 by bmoretti ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef SERVER_HPP +# define SERVER_HPP + +# include "common.hpp" +# include "Client.hpp" + +class Server +{ +public: + Server(); + ~Server(); + +private: + int _serverSocket; + struct sockaddr_in _address; + std::vector _clients; + + void _createServerSocket(void); + void _bindServer(void); + void _listen(void); + void _acceptClient(void); + +}; + +#endif diff --git a/include/common.hpp b/include/common.hpp new file mode 100644 index 0000000..b37fdbf --- /dev/null +++ b/include/common.hpp @@ -0,0 +1,57 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* common.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bmoretti +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/27 11:24:48 by bmoretti #+# #+# */ +/* Updated: 2024/06/27 14:42:52 by bmoretti ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef COMMON_HPP +# define COMMON_HPP + +# define PORT 8042 +# define BUFFER_SIZE 1024 +# define MAX_CONNECTIONS 5 + +// C libs +# include +# include +# include +# include +# include + +// C++ LIBS +# include +# include +# include +# include + +// FORMATTING +# define BOLD(text) "\033[1m" << text << "\033[0m" +# define UNDERLINE(text) "\033[4m" << text << "\033[0m" + +# define BLUE(text) "\033[34m" << text << "\033[0m" +# define RED(text) "\033[31m" << text << "\033[0m" +# define GREEN(text) "\033[32m" << text << "\033[0m" +# define YELLOW(text) "\033[33m" << text << "\033[0m" +# define MAGENTA(text) "\033[35m" << text << "\033[0m" +# define CYAN(text) "\033[36m" << text << "\033[0m" +# define BROWN(text) "\033[33m" << text << "\033[0m" +# define WHITE(text) "\033[37m" << text << "\033[0m" +# define BLACK(text) "\033[30m" << text << "\033[0m" + +# define BG_BLUE(text) "\033[44m" << text << "\033[0m" +# define BG_RED(text) "\033[41m" << text << "\033[0m" +# define BG_GREEN(text) "\033[42m" << text << "\033[0m" +# define BG_YELLOW(text) "\033[43m" << text << "\033[0m" +# define BG_MAGENTA(text) "\033[45m" << text << "\033[0m" +# define BG_CYAN(text) "\033[46m" << text << "\033[0m" +# define BG_BROWN(text) "\033[43m" << text << "\033[0m" +# define BG_WHITE(text) "\033[47m" << text << "\033[0m" +# define BG_BLACK(text) "\033[40m" << text << "\033[0m" + +#endif diff --git a/src/Client.cpp b/src/Client.cpp new file mode 100644 index 0000000..06690b0 --- /dev/null +++ b/src/Client.cpp @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Client.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bmoretti +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/27 14:46:10 by bmoretti #+# #+# */ +/* Updated: 2024/06/27 15:05:20 by bmoretti ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Client.hpp" + +Client::Client() { +} + +Client::~Client() { + close(this->_clientSocket); +} + +void Client::connectToServer(const int serverSocket) { + socklen_t addressSize = sizeof(this->_address); + + this->_clientSocket = accept(serverSocket, + (struct sockaddr *)&this->_address, &addressSize); + if (this->_clientSocket == -1) { + throw std::runtime_error("Error connecting to the server"); + } +} + +void Client::_createClientSocket(void) { + this->_clientSocket = socket(AF_INET, SOCK_STREAM, 0); + if (this->_clientSocket == -1) { + throw std::runtime_error("Error creating the client socket"); + } +} + diff --git a/src/Server.cpp b/src/Server.cpp new file mode 100644 index 0000000..a5d01bd --- /dev/null +++ b/src/Server.cpp @@ -0,0 +1,61 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Server.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bmoretti +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/27 10:40:35 by bmoretti #+# #+# */ +/* Updated: 2024/06/27 15:07:16 by bmoretti ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Server.hpp" + +// PUBLIC FUNCTIONS + +Server::Server() { + this->_address.sin_family = AF_INET; + this->_address.sin_addr.s_addr = INADDR_ANY; + this->_address.sin_port = htons(PORT); + this->_createServerSocket(); + this->_bindServer(); + this->_listen(); +} + +Server::~Server() { + for (std::vector::iterator it = this->_clients.begin(); + it != this->_clients.end(); it++) { + delete *it; + } + close(this->_serverSocket); +} + +// PRIVATE FUNCTIONS + +void Server::_createServerSocket(void) { + this->_serverSocket = socket(AF_INET, SOCK_STREAM, 0); + if (this->_serverSocket == -1) { + throw std::runtime_error("Error creating the server socket"); + } +} + +void Server::_bindServer(void) { + if (bind(this->_serverSocket, (struct sockaddr *)&this->_address, + sizeof(this->_address)) == -1) { + throw std::runtime_error("Error binding the server"); + } +} + +void Server::_listen(void) { + if (listen(this->_serverSocket, MAX_CONNECTIONS) == -1) { + throw std::runtime_error("Error listening the server"); + } +} + +void Server::_acceptClient(void) { + Client* client = new Client(); + + client->connectToServer(this->_serverSocket); + this->_clients.push_back(client); +}