A header-only library with socket classes:
TcpClient- template class for a TCP client socketTcpServer- template class for a TCP Server supporting multiple client connectionsUdpSocket- template class for a UDP Multicast/Unicast
- Optional dependency on
fmtfor error string formatting - C++14 or later
- CMake
- gtest and gmock for unit tests. Enable unit tests by specifying
-DBUILD_TESTS=ONwhen runningCMake
structSocketOpt {
/** * @brief Value passed to setsockopt(SO_SNDBUF) * */int m_txBufSize = TX_BUFFER_SIZE;
/** * @brief Value passed to setsockopt(SO_RCVBUF) * */int m_rxBufSize = RX_BUFFER_SIZE;
/** * @brief Socket listen address * */
std::string m_listenAddr = "0.0.0.0";
};The UdpSocket class is templated on the "callback" class which receives data via UDP.
The template argument type must support the following interface:
voidonReceiveData(constchar *data, size_t size) ;The UdpSocket class has the following interface for managing the UDP socket:
// ConstructorUdpSocket(CallbackImpl &callback, SocketOpt *options = nullptr);
// Start a multicast socket
SocketRet startMcast(constchar *mcastAddr, uint16_t port, constchar *localIpAddr = nullptr);
// Start a unicast client/server socket
SocketRet startUnicast(constchar *remoteAddr, uint16_t localPort, uint16_t port)
// Start a unicast server-only socket
SocketRet startUnicast(uint16_t localPort);
// Send data via UDP
SocketRet sendMsg(constchar *msg, size_t size);
// Shutdown the UDP socketvoidfinish();The TcpClient class is templated on the "callback" class which receives data via TCP.
The template argument type must support the following interface:
voidonReceiveData(constchar *data, size_t size);
voidonDisconnect(const sockets::SocketRet &ret);The TcpClient class has the following interface for managing the TCP client connection:
// ConstructorTcpClient(CallbackImpl &callback, SocketOpt *options = nullptr);
// Connect to a TCP server
SocketRet connectTo(constchar *remoteIp, uint16_t remotePort);
// Send data to the TCP server
SocketRet sendMsg(constchar *msg, size_t size);
// Shutdown the TCP client socketvoidfinish();The TcpServer class is templated on the "callback" class which manages the TCP server.
The template argument must support the following interface:
voidonClientConnect(const sockets::ClientHandle &client);
voidonReceiveClientData(const sockets::ClientHandle &client, constchar *data, size_t size);
voidonClientDisconnect(const sockets::ClientHandle &client, const sockets::SocketRet &ret);The TcpServer class has the following interfaces:
// Create a TCP server socketTcpServer(CallbackImpl &callback, SocketOpt *options = nullptr);
// Start the server listening on the specified port
SocketRet start(uint16_t port);
// Send a message to all connected clients
SocketRet sendBcast(constchar *msg, size_t size);
// Send a message to a specific client connection
SocketRet sendClientMessage(ClientHandle &clientId, constchar *msg, size_t size);
// Shutdown the TCP server socketvoidfinish();Enable building sample apps by specifying -DBUILD_EXAMPLES=ON when running CMake.
$ ./clientApp -a <ipAddr> -p <port>$ ./serverApp -p <port> -L <listenAddr>$ ./mcastApp -m <multicastAddr> -p <port>$ ./unicastApp -a <ipAddr> -l <localPort> -p <remotePort> -L <listenAddr>