11#define SHUT_RDWR SD_BOTH
15#include <sys/socket.h>
19BOOL WINAPI console_handler(DWORD signal) {
20 if (signal == CTRL_C_EVENT || signal == CTRL_CLOSE_EVENT) {
32int main(
int argc,
char **argv) {
35 std::cerr <<
"Usage: " << argv[0] <<
" <path>\n";
39 if (!SetConsoleCtrlHandler(console_handler, TRUE)) {
40 std::cerr <<
"Error setting console handler\n";
44 struct sigaction sa = {};
47 if (sigaction(SIGINT, &sa,
nullptr) == -1) {
55 std::cout <<
"Listening for UDP datagram on port " << argv[1] <<
"...\n";
56 std::vector<uint8_t> buffer(65536);
59 ssize_t bytes = sock.
recvfrom(buffer.data(), buffer.size());
61 std::cout <<
"Received " << bytes <<
" bytes.\n";
62 std::string data(buffer.begin(), buffer.end());
63 std::cout <<
"Data: " << data;
64 }
else if (bytes < 0) {
67 std::cerr <<
"read error.\n";
73 std::cerr <<
"Exception: " << e.
text() <<
"\n";
Lightweight exception wrapper for MXNetwork failures.
std::string text() const
Return the stored error text.
C++ wrapper around the MXNetwork socket API.
bool bind_unix(std::string_view path)
Bind a UNIX-domain socket to a local path.
ssize_t recvfrom(void *buf, size_t bytes)
Receive a datagram using the configured socket type.
@ TYPE_UNIX_DGRAM
UNIX-domain datagram socket.
std::atomic< bool > active_loop