Any reccomendations of a libary which I can call from my Linux (C) app to send simple log messages to anyone who happens to be listening on the network, using Multicast UDP?
2 Answers
You don't need a library to send a multicast packet. Sending a UDP message to a multicast socket is only marginally more complicated than sending one to a unicast address.
In particular you'll need to use setsockopt
to set the IP_MULTICAST_TTL
of your packets.
The receiving side is slightly harder - you have to join a particular multicast group using setsockopt
with IP_ADD_MEMBERSHIP
to be able to receive the messages sent to it.
These options are all documented in man 7 ip
.
As you asked "Any reccomendations of a libary" I consider you dont want just the standard c library because I never see anyone ask if someone recommends c library to c development. So obviously you dont need any thing more than c library but my answer fits your question perfectly or, maybe, the question is not so good.
You can use the excellent ZeroMQ and EPGM protocol (encapsulated inside UDP)
zmq_pgm - ØMQ reliable multicast transport using PGM
in the server side
void *context = zmq_init(1);
assert(context);
void *broadcast = zmq_socket(context, ZMQ_PUB);
assert(broadcast);
rc = zmq_bind(broadcast, "epgm://192.168.0.123:5555");
assert(rc==0);
rc = s_send(broadcast, "Hello world!");
assert(rc==0);
in client side:
void *context = zmq_init(1);
assert(context);
void *watch = zmq_socket(context, ZMQ_SUB);
assert(watch);
rc = zmq_connect(watch, "epgm://192.168.0.123:5555");
assert(rc==0);
char *message = s_receive(watch);
assert(message);