I am trying to get multiple sockets to bind to the same address/port on the same machine, listening for multicasts. Here is a bit of the code:
struct mnl_socket *nl;
nl = mnl_socket_open(NETLINK_USERSOCK);
if (NULL == nl) {
perror("mnl_socket_open");
exit(EXIT_FAILURE);
}
int one = 1;
if (setsockopt(nl->fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &one, sizeof(int)) < 0)
{
printf("%s: setsockopt failed...\n", __func__);
}
std::printf("debugA2\n");
if (mnl_socket_bind(nl, groups, getpid()) < 0) {
printf("%s: mnl_socket_bind failed...\n", __func__);
perror("mnl_socket_bind");
mnl_socket_close(nl);
exit(EXIT_FAILURE);
}
The "mnl_socket_bind" call will fail the second time it is run.
Initially, I didn't call setsockopt(), but I expected that once I made this call, the problem would go away, but it does not. The error is: mnl_socket_bind: Address already in use