Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ mnl_socket_bind与setsockopt(…,SO_REUSEADDR | SO_REUSEPORT…)地址已在使用?_C++_Sockets_Bind_Multicast_Netlink - Fatal编程技术网

C++ mnl_socket_bind与setsockopt(…,SO_REUSEADDR | SO_REUSEPORT…)地址已在使用?

C++ mnl_socket_bind与setsockopt(…,SO_REUSEADDR | SO_REUSEPORT…)地址已在使用?,c++,sockets,bind,multicast,netlink,C++,Sockets,Bind,Multicast,Netlink,我试图让多个套接字绑定到同一台机器上的同一地址/端口,监听多播。下面是一些代码: 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_

我试图让多个套接字绑定到同一台机器上的同一地址/端口,监听多播。下面是一些代码:

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);
}
struct mnl_socket*nl;
nl=mnl_socket_open(NETLINK_USERSOCK);
如果(NULL==nl){
perror(“mnl_插座打开”);
退出(退出失败);
}
int-one=1;
if(setsockopt(nl->fd,SOL_SOCKET,SO_REUSEADDR | SO_REUSEPORT,&one,sizeof(int))<0)
{
printf(“%s:setsockopt失败…\n”,\uuuu func\uuuu);
}
std::printf(“debugA2\n”);
if(mnl\u套接字\u绑定(nl,groups,getpid())<0){
printf(“%s:mnl\u套接字\u绑定失败…\n”,\uuuu func\uuu);
perror(“mnl_socket_bind”);
mnl_插座_关闭(nl);
退出(退出失败);
}
“mnl\u socket\u bind”调用将在运行时第二次失败。
起初,我没有调用setsockopt(),但我希望一旦我进行了此调用,问题就会消失,但事实并非如此。错误是:
mnl\u socket\u bind:Address已在使用中

是否确定在没有
setsockopt
的情况下启动的第一个进程已终止且未继续运行,并且正在使用端口?@amine.ahd它仍在运行。我想让它运行。基本上,一个端口上有多个侦听器。我的理解是,如果您使用“SO_REUSEADDR | SO_REUSEPORT”,那么您将能够做到这一点。这是否正确?是的,但您必须确保所有套接字都是使用该选项启动的,包括第一个进程。@amine.ahd是的,每次打开套接字后,我都会立即调用setsockopt,如上述代码所示。(这都在同一个进程中,不同的线程)。什么是