better closing

This commit is contained in:
ackimixs
2024-05-15 22:36:18 +02:00
parent 5aacadd076
commit 7850913bae
2 changed files with 5 additions and 2 deletions

View File

@@ -15,6 +15,8 @@ private:
protected: protected:
bool running; bool running;
bool _stoped;
public: public:
explicit TCPClient(const char* serverIP = "127.0.0.1", int port = 8080); explicit TCPClient(const char* serverIP = "127.0.0.1", int port = 8080);

View File

@@ -2,7 +2,7 @@
#include "TCPSocket/TCPUtils.hpp" #include "TCPSocket/TCPUtils.hpp"
TCPClient::TCPClient(const char* serverIP, int port) : running(false) { TCPClient::TCPClient(const char* serverIP, int port) : running(false), _stoped(false) {
clientSocket = socket(AF_INET, SOCK_STREAM, 0); clientSocket = socket(AF_INET, SOCK_STREAM, 0);
if (clientSocket == -1) { if (clientSocket == -1) {
std::cerr << "Socket creation failed" << std::endl; std::cerr << "Socket creation failed" << std::endl;
@@ -77,9 +77,10 @@ void TCPClient::start() {
} }
void TCPClient::stop() { void TCPClient::stop() {
if (running) { if (!_stoped) {
running = false; running = false;
close(clientSocket); close(clientSocket);
_stoped = true;
} }
} }