0

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

user207421
  • 305,947
  • 44
  • 307
  • 483
Fuad
  • 1,419
  • 1
  • 16
  • 31
  • 1
    Are you sure the first process that you started without `setsockopt` is terminated and not still running and using the port? – amine.ahd May 05 '17 at 23:02
  • @amine.ahd It is still running. I want it to be running. Basically many listeners on one port. It was my understanding that if you use "SO_REUSEADDR | SO_REUSEPORT", then you will be able to do that. Is this correct? – Fuad May 05 '17 at 23:13
  • 1
    Yes, but you have to make sure ALL sockets are started with that option including the first process. – amine.ahd May 05 '17 at 23:16
  • @amine.ahd Yes, everytime I open a socket, immediately after it, I am calling setsockopt as in the code above. (This is all in the same process, different threads). – Fuad May 05 '17 at 23:18
  • What is `groups`? – user207421 May 06 '17 at 00:06
  • @EJP uint32_t groups = 0xFFFFFFFF; because I want to listen to all the multicast groups. – Fuad May 06 '17 at 00:18
  • I don't know the API or transport you're using, but in UDP you would bind to 0.0.0.0 and *join* the groups *individually*. Why do you want multiple sockets listening to all the groups? – user207421 May 06 '17 at 00:22

0 Answers0