optimizing server code
This commit is contained in:
56
.vscode/settings.json
vendored
56
.vscode/settings.json
vendored
@@ -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"
|
|
||||||
}
|
|
||||||
}
|
|
2
Makefile
2
Makefile
@@ -6,7 +6,7 @@ OBJ = ${SRC:.cpp=.o}
|
|||||||
|
|
||||||
CXX = c++
|
CXX = c++
|
||||||
|
|
||||||
CXXFLAGS = -std=c++98 -fsanitize=address -g3 #-Wall -Wextra -Werror
|
CXXFLAGS = -std=c++98 #-fsanitize=address -g3 #-Wall -Wextra -Werror
|
||||||
|
|
||||||
all : $(NAME)
|
all : $(NAME)
|
||||||
|
|
||||||
|
35
Server.cpp
35
Server.cpp
@@ -111,15 +111,13 @@ void Server::addPollfd(int fd, short events, short revents) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Server::handleClientConnection() {
|
void Server::handleClientConnection() {
|
||||||
for (size_t i = 0; i < _fds.size(); ++i) {
|
struct sockaddr_in client_addr;
|
||||||
if (_fds[i].fd == _serverSocketFd && (_fds[i].revents & POLLIN)) {
|
socklen_t clientAddrSize = sizeof(sockaddr_in);
|
||||||
struct sockaddr_in client_addr;
|
int newFd = accept(_serverSocketFd, (struct sockaddr *)&client_addr, &clientAddrSize);
|
||||||
socklen_t clientAddrSize = sizeof(sockaddr_in);
|
if (newFd == -1) {
|
||||||
int newFd = accept(_serverSocketFd, (struct sockaddr *)&client_addr, &clientAddrSize);
|
throw std::runtime_error("Error: accept() failed");
|
||||||
if (newFd == -1) {
|
}
|
||||||
throw std::runtime_error("Error: accept() failed");
|
std::string passwordRequest = "Please Enter The password Of This Server :\n";
|
||||||
}
|
|
||||||
std::string passwordRequest = "Please Enter The password Of This Server :\n";
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -137,20 +135,13 @@ std::string art =
|
|||||||
" $$ | \n"
|
" $$ | \n"
|
||||||
" $$/ \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);
|
std::cout << "Client <" << newFd << "> Connected" << std::endl;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
addPollfd(newFd, POLLIN, 0);
|
|
||||||
_clients.push_back(Client(newFd, inet_ntoa((client_addr.sin_addr))));
|
|
||||||
|
|
||||||
|
|
||||||
std::cout << "Client <" << newFd << "> Connected" << std::endl;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user