Libftpp
A modern C++ library
Client Class Reference

Basic TCP client class using POSIX sockets and select() for network communication. More...

#include <client.hpp>

Public Member Functions

 Client ()
 
 Client (const std::string &address, const size_t &port)
 
void connect (const std::string &address, const size_t &port)
 
void disconnect ()
 
void defineAction (const Message::Type &messageType, const std::function< void(const Message &msg)> &action)
 
void send (const Message &message)
 
void update ()
 

Detailed Description

Basic TCP client class using POSIX sockets and select() for network communication.

This class provides a simple TCP client implementation with message-based communication. It supports asynchronous message handling through callback functions and maintains connection state using file descriptors and select() for non-blocking operations.

Note
Limited by the maximum number of bytes that can be read at once (MAX_READ_BUFFER = 16000)
Uses select() for non-blocking socket operations
Supports callback-based message handling with type-specific actions
Automatically handles message parsing and reconstruction for partial reads
// Create and connect to server
Client client;
client.connect("127.0.0.1", 8080);
// Define message handler for specific message type
client.defineAction(1001, [](const Message& msg) {
std::string response;
msg >> response;
std::cout << "Received: " << response << std::endl;
});
// Send a message
Message msg(1001);
msg << std::string("Hello Server");
client.send(msg);
// Process incoming messages
client.update(); // Call regularly in your main loop
client.disconnect();
Basic TCP client class using POSIX sockets and select() for network communication.
Definition: client.hpp:63
void connect(const std::string &address, const size_t &port)
Definition: client.cpp:18
void send(const Message &message)
Definition: client.cpp:59
void update()
Definition: client.cpp:127
void disconnect()
Definition: client.cpp:43
void defineAction(const Message::Type &messageType, const std::function< void(const Message &msg)> &action)
Definition: client.cpp:53
Class representing a structured message for network communication.
Definition: message.hpp:47
Exceptions
std::out_of_rangeon message parsing errors
std::runtime_erroron network connection errors
Mayre-throw any exceptions from registered callback functions
See also
Message for message format and usage
Server for corresponding server implementation

Definition at line 62 of file client.hpp.

Constructor & Destructor Documentation

◆ Client() [1/2]

Client::Client ( )

Definition at line 3 of file client.cpp.

◆ Client() [2/2]

Client::Client ( const std::string &  address,
const size_t &  port 
)

Definition at line 5 of file client.cpp.

References connect().

Member Function Documentation

◆ connect()

void Client::connect ( const std::string &  address,
const size_t &  port 
)

Definition at line 18 of file client.cpp.

References disconnect().

Referenced by Client().

◆ defineAction()

void Client::defineAction ( const Message::Type messageType,
const std::function< void(const Message &msg)> &  action 
)

Definition at line 53 of file client.cpp.

◆ disconnect()

void Client::disconnect ( )

Definition at line 43 of file client.cpp.

Referenced by connect().

◆ send()

void Client::send ( const Message message)

Definition at line 59 of file client.cpp.

References Message::getSerializedData().

◆ update()

void Client::update ( )

Definition at line 127 of file client.cpp.