diff --git a/CMakeLists.txt b/CMakeLists.txt index 41a8d97..92757e4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,9 +3,6 @@ project(TCPSocket VERSION 0.1.3 DESCRIPTION "TCP Socket" LANGUAGES CXX) set(CMAKE_CXX_STANDARD 17) -# Specify C++ Standard -set(CMAKE_CXX_STANDARD 17) - # Add the source files set(SOURCES src/TCPClient.cpp diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt index 2700e0c..0a0c3af 100644 --- a/example/CMakeLists.txt +++ b/example/CMakeLists.txt @@ -8,4 +8,4 @@ pkg_check_modules(TCPSocket REQUIRED TCPSocket) add_executable(client client.test.cpp) -target_link_libraries(client TCPSocket) +target_link_libraries(client TCPSocket) \ No newline at end of file diff --git a/example/client.test.cpp b/example/client.test.cpp index 0239c6d..bd4a05f 100644 --- a/example/client.test.cpp +++ b/example/client.test.cpp @@ -1,7 +1,7 @@ #include int main() { - TCPClient client("127.0.0.1", 8080); // Replace "127.0.0.1" with the IP address of your server and 8080 with the port number + TCPClient client("127.0.0.1", 8082); // Replace "127.0.0.1" with the IP address of your server and 8080 with the port number client.start(); diff --git a/include/TCPSocket/TCPClient.hpp b/include/TCPSocket/TCPClient.hpp index 178fa9d..61c24b0 100644 --- a/include/TCPSocket/TCPClient.hpp +++ b/include/TCPSocket/TCPClient.hpp @@ -12,12 +12,11 @@ class TCPClient { int clientSocket; sockaddr_in serverAddress; bool running; - //void (*callback)(const std::string& message); public: - TCPClient(const char* serverIP = "127.0.0.1", int port = 8080/*, void (*callback)(const std::string& message) = [](const std::string& message) { std::cout << message << std::endl; }*/); + TCPClient(const char* serverIP = "127.0.0.1", int port = 8080); - void sendMessage(const char* message); + void sendMessage(const char* message) const; void receiveMessages(); @@ -29,5 +28,4 @@ public: virtual void handleMessage(const std::string& message); - //void setCallback(void (*callback)(const std::string& message)); }; \ No newline at end of file diff --git a/include/TCPSocket/TCPUtils.hpp b/include/TCPSocket/TCPUtils.hpp index 71b9458..eb72e48 100644 --- a/include/TCPSocket/TCPUtils.hpp +++ b/include/TCPSocket/TCPUtils.hpp @@ -2,11 +2,13 @@ #include #include +namespace TCPSocket +{ + bool startWith(const std::string& str, const std::string& start); -inline bool startWith(const std::string& str, const std::string& start); + bool endsWith(const std::string& str, const std::string& end); -inline bool endsWith(const std::string& str, const std::string& end); + bool contains(const std::string& str, const std::string& sub); -inline bool contains(const std::string& str, const std::string& sub); - -inline std::vector split(const std::string& str, const std::string& delimiter); \ No newline at end of file + std::vector split(const std::string& str, const std::string& delimiter); +} \ No newline at end of file diff --git a/src/TCPClient.cpp b/src/TCPClient.cpp index 558ff3d..53c8365 100644 --- a/src/TCPClient.cpp +++ b/src/TCPClient.cpp @@ -1,6 +1,6 @@ #include "TCPSocket/TCPClient.hpp" -TCPClient::TCPClient(const char* serverIP, int port/*, void (*callback)(const std::string& message)*/) : running(true)/*, callback(callback)*/ { +TCPClient::TCPClient(const char* serverIP, int port) { clientSocket = socket(AF_INET, SOCK_STREAM, 0); if (clientSocket == -1) { std::cerr << "Socket creation failed" << std::endl; @@ -21,7 +21,7 @@ TCPClient::TCPClient(const char* serverIP, int port/*, void (*callback)(const st } } -void TCPClient::sendMessage(const char* message) { +void TCPClient::sendMessage(const char* message) const { send(clientSocket, message, strlen(message), 0); } @@ -59,8 +59,4 @@ void TCPClient::start() { void TCPClient::stop() { running = false; close(clientSocket); -} - -/*void TCPClient::setCallback(void (*callback)(const std::string& message)) { - this->callback = callback; -}*/ \ No newline at end of file +} \ No newline at end of file diff --git a/src/TCPUtils.cpp b/src/TCPUtils.cpp index c5b4585..cd99ed2 100644 --- a/src/TCPUtils.cpp +++ b/src/TCPUtils.cpp @@ -1,11 +1,11 @@ #include "TCPSocket/TCPUtils.hpp" -inline bool startWith(const std::string& str, const std::string& start) +bool TCPSocket::startWith(const std::string& str, const std::string& start) { return str.rfind(start, 0) == 0; } -inline bool endsWith(const std::string& str, const std::string& end) +bool TCPSocket::endsWith(const std::string& str, const std::string& end) { if (str.length() >= end.length()) { @@ -14,12 +14,12 @@ inline bool endsWith(const std::string& str, const std::string& end) return false; } -inline bool contains(const std::string& str, const std::string& sub) +bool TCPSocket::contains(const std::string& str, const std::string& sub) { return str.find(sub) != std::string::npos; } -inline std::vector split(const std::string& str, const std::string& delimiter) +std::vector TCPSocket::split(const std::string& str, const std::string& delimiter) { std::vector tokens; size_t prev = 0, pos = 0;