mirror of
https://github.com/modelec/TCPSocketClient.git
synced 2026-01-18 16:37:35 +01:00
add namespace
This commit is contained in:
@@ -3,9 +3,6 @@ project(TCPSocket VERSION 0.1.3 DESCRIPTION "TCP Socket" LANGUAGES CXX)
|
|||||||
|
|
||||||
set(CMAKE_CXX_STANDARD 17)
|
set(CMAKE_CXX_STANDARD 17)
|
||||||
|
|
||||||
# Specify C++ Standard
|
|
||||||
set(CMAKE_CXX_STANDARD 17)
|
|
||||||
|
|
||||||
# Add the source files
|
# Add the source files
|
||||||
set(SOURCES
|
set(SOURCES
|
||||||
src/TCPClient.cpp
|
src/TCPClient.cpp
|
||||||
|
|||||||
@@ -8,4 +8,4 @@ pkg_check_modules(TCPSocket REQUIRED TCPSocket)
|
|||||||
|
|
||||||
add_executable(client client.test.cpp)
|
add_executable(client client.test.cpp)
|
||||||
|
|
||||||
target_link_libraries(client TCPSocket)
|
target_link_libraries(client TCPSocket)
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
#include <TCPSocket/TCPClient.hpp>
|
#include <TCPSocket/TCPClient.hpp>
|
||||||
|
|
||||||
int main() {
|
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();
|
client.start();
|
||||||
|
|
||||||
|
|||||||
@@ -12,12 +12,11 @@ class TCPClient {
|
|||||||
int clientSocket;
|
int clientSocket;
|
||||||
sockaddr_in serverAddress;
|
sockaddr_in serverAddress;
|
||||||
bool running;
|
bool running;
|
||||||
//void (*callback)(const std::string& message);
|
|
||||||
|
|
||||||
public:
|
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();
|
void receiveMessages();
|
||||||
|
|
||||||
@@ -29,5 +28,4 @@ public:
|
|||||||
|
|
||||||
virtual void handleMessage(const std::string& message);
|
virtual void handleMessage(const std::string& message);
|
||||||
|
|
||||||
//void setCallback(void (*callback)(const std::string& message));
|
|
||||||
};
|
};
|
||||||
@@ -2,11 +2,13 @@
|
|||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
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);
|
std::vector<std::string> split(const std::string& str, const std::string& delimiter);
|
||||||
|
}
|
||||||
inline std::vector<std::string> split(const std::string& str, const std::string& delimiter);
|
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
#include "TCPSocket/TCPClient.hpp"
|
#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);
|
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;
|
||||||
@@ -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);
|
send(clientSocket, message, strlen(message), 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -59,8 +59,4 @@ void TCPClient::start() {
|
|||||||
void TCPClient::stop() {
|
void TCPClient::stop() {
|
||||||
running = false;
|
running = false;
|
||||||
close(clientSocket);
|
close(clientSocket);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*void TCPClient::setCallback(void (*callback)(const std::string& message)) {
|
|
||||||
this->callback = callback;
|
|
||||||
}*/
|
|
||||||
@@ -1,11 +1,11 @@
|
|||||||
#include "TCPSocket/TCPUtils.hpp"
|
#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;
|
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())
|
if (str.length() >= end.length())
|
||||||
{
|
{
|
||||||
@@ -14,12 +14,12 @@ inline bool endsWith(const std::string& str, const std::string& end)
|
|||||||
return false;
|
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;
|
return str.find(sub) != std::string::npos;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline std::vector<std::string> split(const std::string& str, const std::string& delimiter)
|
std::vector<std::string> TCPSocket::split(const std::string& str, const std::string& delimiter)
|
||||||
{
|
{
|
||||||
std::vector<std::string> tokens;
|
std::vector<std::string> tokens;
|
||||||
size_t prev = 0, pos = 0;
|
size_t prev = 0, pos = 0;
|
||||||
|
|||||||
Reference in New Issue
Block a user