close fds and exit the program cleanly

This commit is contained in:
bettercallous
2024-04-11 18:02:52 +00:00
parent 92ac924f0a
commit 21f497ef0a
3 changed files with 16 additions and 0 deletions

View File

@@ -54,6 +54,7 @@ void Server::run() {
} }
} }
} }
closeFds();
} }
void Server::createServerSocket() { void Server::createServerSocket() {
@@ -163,3 +164,16 @@ void Server::clientCleanup(int fd) {
} }
} }
} }
void Server::closeFds() {
for (size_t i = 0; i < _clients.size(); i++){
int fd = _clients[i].getFd();
std::cout << "Client <" << fd << "> Disconnected" << std::endl;
close(fd);
}
if (_serverSocketFd != -1)
close(_serverSocketFd);
_fds.clear();
}

View File

@@ -35,6 +35,7 @@ class Server {
void handleClientConnection(); void handleClientConnection();
void handleClientData(int fd); void handleClientData(int fd);
void clientCleanup(int fd); void clientCleanup(int fd);
void closeFds();
}; };
#endif #endif

View File

@@ -9,6 +9,7 @@ int main(int ac, char **av)
server.init(); server.init();
server.run(); server.run();
} catch (std::exception& e) { } catch (std::exception& e) {
server.closeFds();
std::cerr << e.what() << std::endl; std::cerr << e.what() << std::endl;
return 1; return 1;
} }