Overview
UdpDataChannel C++ library provides point-to-point high bitrate communication between two applications based on UDP. The library includes UdpDataChannel (works as server or client) class and two additional classes: UdpDataClient (connects to server, for compatibility with previous versions) and UdpDataServer (wait connection from client, for compatibility with previous versions). All classes provide two-way communication. The library uses C++17 standard and only depends on open source UdpSocket (source code included, Apache 2.0 license) which provide functions to work with UDP sockets. This library compatible with Linux and Window. Server principles: the server initializes the user-specified UDP port and waits for the client to connect from any port. The server supports connection of only one client. When a client connects the server remembers the client’s IP and UDP port to exchange messages with them. The client sends special commands to connect. Client principles: the client initialized any first available UDP port in the OS. After initialization client sends connection messages to server port (to connect the user must say to client the server’s UDP port and IP). Once connection established the client can exchanges messages with server. To send data server and client split data into separate UDP packets (max length 1030 bytes: 1024 bytes payload + 6 bytes header) with special header data. Receiver (client or server) collect UDP packets and detect when whole input data being send by sender collected. The receiver sends confirmation to sender about receiving each block of 8 packets and confirmation about whole data being received. Sender sends data four times until it will get confirmation from receiver. If particular packet was confirmed the sender doesn’t resend it. The library can handle intensive data exchange close to channel bandwidth limit. The library allows user to set maximum channel bandwidth to prevent network overloading. Sender (client or server) controls interval between UDP packets being sent. The library allows to send only up to 67106815 bytes in one time.
Documentation
Documentation: GO TO DOCUMENTATION
Simple interface
class UdpDataChannel
{
public:
/// Get current library version.
static std::string getVersion();
/// Initialize in client or server mode.
bool init(std::string serverIp,
uint16_t serverPort,
int channelBandwidthKbps = 1000000);
/// Close client.
void close();
/// Get input data from connected client.
bool get(uint8_t* data, int bufferSize, int& dataSize, int timeoutMsec = 0);
/// Send data to the client.
bool send(uint8_t* data, int size);
/// Get connection status.
bool isConnected();
/// Get open status.
bool isInit();
};
Simple example for client and server
#include <iostream>
#include <thread>
#include "UdpDataChannel.h"
void serverThreadFunction()
{
// Init as server.
cr::clib::UdpDataChannel server;
if (!server.init("0.0.0.0", 58002))
return;
// Thread loop.
uint8_t buffer[256];
while (true)
{
// Wait data from client for 2 sec.
int size = 0;
if (!server.get(buffer, 256, size, 2000))
continue;
// Send the same data back to client.
if (!server.send(buffer, size))
std::cout << "Can't send data to client" << std::endl;
}
}
int main(void)
{
// Run server thread.
std::thread serverThread(&serverThreadFunction);
// Init as client.
cr::clib::UdpDataChannel client;
if (!client.init("127.0.0.1", 58002))
return -1;
// Main loop.
uint8_t buffer[256];
while (true)
{
// Prepare random data for server.
int size = (rand() % 128) + 1;
memset(buffer, (rand() % 255), size);
// Send data to server.
if (!client.send(buffer, size))
std::cout << "Can't send data to server" << std::endl;
else
std::cout << size << " bytes to server" << std::endl;
// Wait data from server. 500 msec.
size = 0;
if (client.get(buffer, 256, size, 500))
std::cout << size << " bytes from server" << std::endl;
else
std::cout << "No data from server" << std::endl;
}
return 1;
}