diff --git a/Makefile b/Makefile index dc23829..7633a0b 100644 --- a/Makefile +++ b/Makefile @@ -3,13 +3,13 @@ # ::: :::::::: # # Makefile :+: :+: :+: # # +:+ +:+ +:+ # -# By: rparodi +#+ +:+ +#+ # -# Created: 2023/11/12 11:05:05 by rparodi #+# #+# # -# Updated: 2025/05/02 15:40:00 by rparodi ### ########.fr # +# By: omoudni +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2025/05/02 15:40:00 by rparodi #+# #+# # +# Updated: 2025/05/14 23:33:58 by omoudni ### ########.fr # # # # **************************************************************************** # -# Variables # Name NAME = ircserv @@ -136,7 +136,7 @@ footer: @printf "$(GOLD) /| | | |\\_//$(END)\n" @printf "$(GOLD) \\| |._.| |/-\`$(END)\n" @printf "$(GOLD) '\"' '\"'$(END)\n" - @printf ' $(GREY)The compilation is$(END) $(GOLD)finish$(END)\n $(GREY)Have a good $(END)$(GOLD)correction !$(END)\n' + @printf ' $(GREY)The compilation is$(END) $(GOLD)finished$(END)\n $(GREY)Have a good $(END)$(GOLD)evaluation !$(END)\n' clangd: @printf "CompileFlags:\n" > ./.clangd diff --git a/diagram.puml b/diagram.puml index b54b12a..1b8652d 100644 --- a/diagram.puml +++ b/diagram.puml @@ -7,11 +7,11 @@ class Server { - _port : int - _password : string - _clients : List - - _socket_id : int - - _fd : int + - _server_fd : int + showInfo() : void + getPort() : int + + start() : void } ' ======================== diff --git a/en.subject.pdf b/en.subject.pdf new file mode 100644 index 0000000..4d94dab Binary files /dev/null and b/en.subject.pdf differ diff --git a/include/core/server.hpp b/include/core/server.hpp index 5db9f1f..e29640d 100644 --- a/include/core/server.hpp +++ b/include/core/server.hpp @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* server.hpp :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: rparodi +#+ +:+ +#+ */ +/* By: omoudni +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/05/13 11:06:56 by rparodi #+# #+# */ -/* Updated: 2025/05/13 13:04:29 by rparodi ### ########.fr */ +/* Updated: 2025/05/14 23:23:13 by omoudni ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,10 +18,13 @@ class Server { private: unsigned short int _port; std::string _password; + int server_fd; public: Server(); Server(int port, const std::string &password); ~Server(); void showInfo() const; unsigned short int getPort() const; + void setServerFd(int fd); + void start(); }; diff --git a/sources/core/main.cpp b/sources/core/main.cpp index 3dd8938..c006a8c 100644 --- a/sources/core/main.cpp +++ b/sources/core/main.cpp @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* main.cpp :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: rparodi +#+ +:+ +#+ */ +/* By: omoudni +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/05/13 11:03:13 by rparodi #+# #+# */ -/* Updated: 2025/05/13 12:56:52 by rparodi ### ########.fr */ +/* Updated: 2025/05/14 23:17:03 by omoudni ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,9 +23,8 @@ int main(int argc, char *argv[]) { std::cerr << CLR_RED << "Error: Invalid port: " << argv[1] << CLR_RESET << std::endl; return 1; } - Server server(tmp_port, argv[2]); - server.showInfo(); - while (1) - ; - return 0; + Server server(tmp_port, argv[2]); + server.showInfo(); + server.start(); + return 0; } diff --git a/sources/core/server.cpp b/sources/core/server.cpp index 8501284..a43d6df 100644 --- a/sources/core/server.cpp +++ b/sources/core/server.cpp @@ -3,17 +3,20 @@ /* ::: :::::::: */ /* server.cpp :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: rparodi +#+ +:+ +#+ */ +/* By: omoudni +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/05/13 11:11:07 by rparodi #+# #+# */ -/* Updated: 2025/05/13 13:05:07 by rparodi ### ########.fr */ +/* Updated: 2025/05/14 23:32:21 by omoudni ### ########.fr */ /* */ /* ************************************************************************** */ #include "color.hpp" +#include "server.hpp" #include "core.hpp" #include - +#include +#include +#include /** * @brief The default constructor of the Server class. */ @@ -42,6 +45,50 @@ Server::~Server() { std::cout << CLR_GREY << "Info: Server destructor called" << CLR_RESET << std::endl; } + +void Server::start() { + int fd = socket(AF_INET, SOCK_STREAM, 0); + if (server_fd == -1) { + std::cerr << CLR_RED << "Error: Failed to create socket" << CLR_RESET << std::endl; + return; + } + setServerFd(fd); + struct sockaddr_in server_addr; + server_addr.sin_family = AF_INET; + server_addr.sin_addr.s_addr = INADDR_ANY; + server_addr.sin_port = htons(this->_port); + if (bind(server_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) { + std::cerr << CLR_RED << "Error: Failed to bind socket" << CLR_RESET << std::endl; + close(server_fd); + return; + } + if (listen(server_fd, 5) == -1) { + std::cerr << CLR_RED << "Error: Failed to listen on socket" << CLR_RESET << std::endl; + close(server_fd); + return; + } + std::cout << CLR_GREEN << "Server started on port " << this->_port << CLR_RESET << std::endl; + std::cout << CLR_GREEN << "Waiting for clients..." << CLR_RESET << std::endl; + while (true) { + int client_fd = accept(server_fd, NULL, NULL); + if (client_fd == -1) { + std::cerr << CLR_RED << "Error: Failed to accept client" << CLR_RESET << std::endl; + continue; + } + std::cout << CLR_GREEN << "Client connected" << CLR_RESET << std::endl; + close(client_fd); + } + close(server_fd); + std::cout << CLR_GREEN << "Server stopped" << CLR_RESET << std::endl; +} + +/** + * @brief Show the server settings. + * + * @note This function is used to show the server settings. + * It is used for debug purpose. + */ + void Server::showInfo() const { std::cout << std::endl; std::cout << CLR_BLUE << "IRCSettings:" << CLR_RESET << std::endl; @@ -58,3 +105,9 @@ void Server::showInfo() const { unsigned short int Server::getPort() const { return this->_port; } + +void Server::setServerFd(int fd) { + this->server_fd = fd; +} + +