我试图让多个套接字绑定到同一台机器上的同一个地址/端口,监听多播。这是一些代码:
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);
}
&#34; mnl_socket_bind&#34;呼叫将失败第二时间运行。
最初,我没有调用setsockopt(),但我预计,一旦我拨打这个电话,问题就会消失,但事实并非如此。错误是:mnl_socket_bind: Address already in use