mirror of
https://github.com/modelec/TCPSocketClient.git
synced 2026-01-18 16:37:35 +01:00
initial commit
This commit is contained in:
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
/cmake-build-debug/
|
||||
/build/
|
||||
/example/cmake-build-debug/
|
||||
/example/build/
|
||||
39
CMakeLists.txt
Normal file
39
CMakeLists.txt
Normal file
@@ -0,0 +1,39 @@
|
||||
cmake_minimum_required(VERSION 3.25)
|
||||
project(TCPSocket VERSION 0.1.2 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
|
||||
)
|
||||
|
||||
# Add the header files
|
||||
set(HEADERS
|
||||
include/TCPSocket/TCPClient.hpp
|
||||
include/TCPSocket/TCPUtils.hpp
|
||||
)
|
||||
|
||||
add_library(TCPSocket SHARED ${SOURCES} ${HEADERS})
|
||||
|
||||
set_target_properties(TCPSocket PROPERTIES
|
||||
VERSION ${PROJECT_VERSION}
|
||||
SOVERSION ${PROJECT_VERSION_MAJOR}
|
||||
PUBLIC_HEADER "${HEADERS}"
|
||||
)
|
||||
|
||||
target_include_directories(TCPSocket PRIVATE include)
|
||||
target_include_directories(TCPSocket PRIVATE src)
|
||||
|
||||
include(GNUInstallDirs)
|
||||
|
||||
install(TARGETS TCPSocket
|
||||
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
|
||||
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/TCPSocket)
|
||||
|
||||
configure_file(TCPSocket.pc.in TCPSocket.pc @ONLY)
|
||||
|
||||
install(FILES ${CMAKE_BINARY_DIR}/TCPSocket.pc DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/pkgconfig)
|
||||
12
TCPSocket.pc.in
Normal file
12
TCPSocket.pc.in
Normal file
@@ -0,0 +1,12 @@
|
||||
prefix=@CMAKE_INSTALL_PREFIX@
|
||||
exec_prefix=@CMAKE_INSTALL_PREFIX@
|
||||
libdir=${exec_prefix}/@CMAKE_INSTALL_LIBDIR@/TCPSocket
|
||||
includedir=${prefix}/@CMAKE_INSTALL_INCLUDEDIR@
|
||||
|
||||
Name: @PROJECT_NAME@
|
||||
Description: @PROJECT_DESCRIPTION@
|
||||
Version: @PROJECT_VERSION@
|
||||
|
||||
Requires:
|
||||
Libs: -L${libdir} -lmylib
|
||||
Cflags: -I${includedir}
|
||||
11
example/CMakeLists.txt
Normal file
11
example/CMakeLists.txt
Normal file
@@ -0,0 +1,11 @@
|
||||
cmake_minimum_required(VERSION 3.25)
|
||||
project(example)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
|
||||
find_package(PkgConfig REQUIRED)
|
||||
pkg_check_modules(TCPSocket REQUIRED TCPSocket)
|
||||
|
||||
add_executable(client client.test.cpp)
|
||||
|
||||
target_link_libraries(client TCPSocket)
|
||||
22
example/client.test.cpp
Normal file
22
example/client.test.cpp
Normal file
@@ -0,0 +1,22 @@
|
||||
#include <TCPClient.hpp>
|
||||
|
||||
int main() {
|
||||
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();
|
||||
|
||||
while (true) {
|
||||
std::string message;
|
||||
std::cout << "Enter message ('quit' to exit): ";
|
||||
std::getline(std::cin, message);
|
||||
|
||||
if (message == "quit") {
|
||||
client.stop();
|
||||
break;
|
||||
}
|
||||
|
||||
client.sendMessage(message.c_str());
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
33
include/TCPSocket/TCPClient.hpp
Normal file
33
include/TCPSocket/TCPClient.hpp
Normal file
@@ -0,0 +1,33 @@
|
||||
#pragma once
|
||||
|
||||
#include <iostream>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <unistd.h>
|
||||
#include <cstring>
|
||||
#include <thread>
|
||||
|
||||
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; }*/);
|
||||
|
||||
void sendMessage(const char* message);
|
||||
|
||||
void receiveMessages();
|
||||
|
||||
void start();
|
||||
|
||||
void stop();
|
||||
|
||||
virtual ~TCPClient();
|
||||
|
||||
virtual void handleMessage(const std::string& message);
|
||||
|
||||
//void setCallback(void (*callback)(const std::string& message));
|
||||
};
|
||||
37
include/TCPSocket/TCPUtils.hpp
Normal file
37
include/TCPSocket/TCPUtils.hpp
Normal file
@@ -0,0 +1,37 @@
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
|
||||
inline bool 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)
|
||||
{
|
||||
if (str.length() >= end.length())
|
||||
{
|
||||
return (0 == str.compare(str.length() - end.length(), end.length(), end));
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
inline bool contains(const std::string& str, const std::string& sub)
|
||||
{
|
||||
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> tokens;
|
||||
size_t prev = 0, pos = 0;
|
||||
do
|
||||
{
|
||||
pos = str.find(delimiter, prev);
|
||||
if (pos == std::string::npos) pos = str.length();
|
||||
std::string token = str.substr(prev, pos - prev);
|
||||
if (!token.empty()) tokens.push_back(token);
|
||||
prev = pos + delimiter.length();
|
||||
} while (pos < str.length() && prev < str.length());
|
||||
return tokens;
|
||||
}
|
||||
66
src/TCPClient.cpp
Normal file
66
src/TCPClient.cpp
Normal file
@@ -0,0 +1,66 @@
|
||||
#include "TCPSocket/TCPClient.hpp"
|
||||
|
||||
TCPClient::TCPClient(const char* serverIP, int port/*, void (*callback)(const std::string& message)*/) : running(true)/*, callback(callback)*/ {
|
||||
clientSocket = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (clientSocket == -1) {
|
||||
std::cerr << "Socket creation failed" << std::endl;
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
serverAddress.sin_family = AF_INET;
|
||||
serverAddress.sin_port = htons(port);
|
||||
|
||||
if (inet_pton(AF_INET, serverIP, &serverAddress.sin_addr) <= 0) {
|
||||
std::cerr << "Invalid address or address not supported" << std::endl;
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
if (connect(clientSocket, (struct sockaddr*)&serverAddress, sizeof(serverAddress)) == -1) {
|
||||
std::cerr << "Connection failed" << std::endl;
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
void TCPClient::sendMessage(const char* message) {
|
||||
send(clientSocket, message, strlen(message), 0);
|
||||
}
|
||||
|
||||
void TCPClient::receiveMessages() {
|
||||
char buffer[1024] = {0};
|
||||
while (running) {
|
||||
ssize_t valread = recv(clientSocket, buffer, sizeof(buffer), 0);
|
||||
if (valread > 0) {
|
||||
handleMessage(buffer);
|
||||
memset(buffer, 0, sizeof(buffer)); // Clear buffer
|
||||
} else if (valread == 0) {
|
||||
std::cerr << "Connection closed by server" << std::endl;
|
||||
break;
|
||||
} else {
|
||||
std::cerr << "Error in receiving message" << std::endl;
|
||||
break;
|
||||
}
|
||||
}
|
||||
running = false;
|
||||
}
|
||||
|
||||
void TCPClient::handleMessage(const std::string& message) {
|
||||
std::cout << message << std::endl;
|
||||
}
|
||||
|
||||
TCPClient::~TCPClient() {
|
||||
stop();
|
||||
}
|
||||
|
||||
void TCPClient::start() {
|
||||
std::thread receiveThread(&TCPClient::receiveMessages, this);
|
||||
receiveThread.detach();
|
||||
}
|
||||
|
||||
void TCPClient::stop() {
|
||||
running = false;
|
||||
close(clientSocket);
|
||||
}
|
||||
|
||||
/*void TCPClient::setCallback(void (*callback)(const std::string& message)) {
|
||||
this->callback = callback;
|
||||
}*/
|
||||
Reference in New Issue
Block a user