From a21770516bc9d1951d0ac05c15f87628f27d359d Mon Sep 17 00:00:00 2001 From: bettercallous Date: Tue, 23 Apr 2024 17:58:48 +0100 Subject: [PATCH] optimizing server code --- .vscode/settings.json | 56 ------------------------------------------- Makefile | 2 +- Server.cpp | 35 ++++++++++----------------- 3 files changed, 14 insertions(+), 79 deletions(-) delete mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index 178dedb..0000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,56 +0,0 @@ -{ - "files.associations": { - "array": "cpp", - "atomic": "cpp", - "bit": "cpp", - "*.tcc": "cpp", - "cctype": "cpp", - "clocale": "cpp", - "cmath": "cpp", - "compare": "cpp", - "concepts": "cpp", - "csignal": "cpp", - "cstdarg": "cpp", - "cstddef": "cpp", - "cstdint": "cpp", - "cstdio": "cpp", - "cstdlib": "cpp", - "cstring": "cpp", - "ctime": "cpp", - "cwchar": "cpp", - "cwctype": "cpp", - "deque": "cpp", - "map": "cpp", - "string": "cpp", - "unordered_map": "cpp", - "vector": "cpp", - "exception": "cpp", - "algorithm": "cpp", - "functional": "cpp", - "iterator": "cpp", - "memory": "cpp", - "memory_resource": "cpp", - "numeric": "cpp", - "optional": "cpp", - "random": "cpp", - "string_view": "cpp", - "system_error": "cpp", - "tuple": "cpp", - "type_traits": "cpp", - "utility": "cpp", - "initializer_list": "cpp", - "iomanip": "cpp", - "iosfwd": "cpp", - "iostream": "cpp", - "istream": "cpp", - "limits": "cpp", - "new": "cpp", - "numbers": "cpp", - "ostream": "cpp", - "sstream": "cpp", - "stdexcept": "cpp", - "streambuf": "cpp", - "cinttypes": "cpp", - "typeinfo": "cpp" - } -} \ No newline at end of file diff --git a/Makefile b/Makefile index bf0747e..6312acb 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ OBJ = ${SRC:.cpp=.o} CXX = c++ -CXXFLAGS = -std=c++98 -fsanitize=address -g3 #-Wall -Wextra -Werror +CXXFLAGS = -std=c++98 #-fsanitize=address -g3 #-Wall -Wextra -Werror all : $(NAME) diff --git a/Server.cpp b/Server.cpp index bda90b9..7cfa598 100644 --- a/Server.cpp +++ b/Server.cpp @@ -111,15 +111,13 @@ void Server::addPollfd(int fd, short events, short revents) { } void Server::handleClientConnection() { - for (size_t i = 0; i < _fds.size(); ++i) { - if (_fds[i].fd == _serverSocketFd && (_fds[i].revents & POLLIN)) { - struct sockaddr_in client_addr; - socklen_t clientAddrSize = sizeof(sockaddr_in); - int newFd = accept(_serverSocketFd, (struct sockaddr *)&client_addr, &clientAddrSize); - if (newFd == -1) { - throw std::runtime_error("Error: accept() failed"); - } - std::string passwordRequest = "Please Enter The password Of This Server :\n"; + struct sockaddr_in client_addr; + socklen_t clientAddrSize = sizeof(sockaddr_in); + int newFd = accept(_serverSocketFd, (struct sockaddr *)&client_addr, &clientAddrSize); + if (newFd == -1) { + throw std::runtime_error("Error: accept() failed"); + } + std::string passwordRequest = "Please Enter The password Of This Server :\n"; @@ -137,20 +135,13 @@ std::string art = " $$ | \n" " $$/ \n"; - send(newFd, art.c_str(), art.length(), 0); - + send(newFd, art.c_str(), art.length(), 0); + + send(newFd, passwordRequest.c_str(), passwordRequest.length(), 0); + addPollfd(newFd, POLLIN, 0); + _clients.push_back(Client(newFd, inet_ntoa((client_addr.sin_addr)))); - send(newFd, passwordRequest.c_str(), passwordRequest.length(), 0); - - - - addPollfd(newFd, POLLIN, 0); - _clients.push_back(Client(newFd, inet_ntoa((client_addr.sin_addr)))); - - - std::cout << "Client <" << newFd << "> Connected" << std::endl; - } - } + std::cout << "Client <" << newFd << "> Connected" << std::endl; }