Im trying to listen broadcast udp messages using cesanta mongoose in C/C++.
mgr = new mg_mgr();
struct ip_mreq group;
mg_mgr_init(mgr, this);
{
char listen[256];
snprintf(listen, sizeof(listen), "udp://%d", _port);
nc = mg_bind(mgr, listen, ev_handler);
}
if (nc == NULL)
{
strerror(errno);
return errno;
}
group.imr_multiaddr.s_addr = inet_addr("0.0.0.0");
group.imr_interface.s_addr = inet_addr(INADDR_ANY);
if (setsockopt(nc->sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&group, sizeof(group)) < 0)
{
strerror(errno);
return errno;
}
while(true)
mg_mgr_poll(mgr, 0);
It works fine on windows, and I'm able to catch the messages. BUT it fails on linux (Debian) on the setsockopt with error 22 (invalid argument).
I thought the mongoose library was crossplatform, is there something I might be missing here? I read that the option IP_ADD_MEMBERSHIP might give problems, but I don't know how should I set the socket options to receive the broadcast.