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

title #5

Merged
merged 1 commit into from
Jun 28, 2024
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
Empty file removed include/.gitkeep
Empty file.
33 changes: 33 additions & 0 deletions include/Client.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Client.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bmoretti <bmoretti@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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
37 changes: 37 additions & 0 deletions include/Server.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Server.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bmoretti <bmoretti@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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<Client*> _clients;

void _createServerSocket(void);
void _bindServer(void);
void _listen(void);
void _acceptClient(void);

};

#endif
57 changes: 57 additions & 0 deletions include/common.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* common.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bmoretti <bmoretti@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <arpa/inet.h>
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# include <unistd.h>

// C++ LIBS
# include <iostream>
# include <stdexcept>
# include <string>
# include <vector>

// 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
38 changes: 38 additions & 0 deletions src/Client.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Client.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bmoretti <bmoretti@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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");
}
}

61 changes: 61 additions & 0 deletions src/Server.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Server.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bmoretti <bmoretti@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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<Client*>::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);
}