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
C++ 套接字描述符在赋值后关闭_C++_Sockets - Fatal编程技术网

C++ 套接字描述符在赋值后关闭

C++ 套接字描述符在赋值后关闭,c++,sockets,C++,Sockets,我不会发布所有的通用套接字代码——除非请求,否则没有问题 我在已设置的套接字上侦听连接: int NetworkConnection::ListenForConnections() { char s[INET6_ADDRSTRLEN]; fcntl(socketFd, F_SETFL, O_NONBLOCK); if (listen(socketFd, 15) == -1) { perror("listen"); exit(1);

我不会发布所有的通用套接字代码——除非请求,否则没有问题

我在已设置的套接字上侦听连接:

int
NetworkConnection::ListenForConnections()
{
    char s[INET6_ADDRSTRLEN];
    fcntl(socketFd, F_SETFL, O_NONBLOCK);

    if (listen(socketFd, 15) == -1) {
        perror("listen");
        exit(1);
    }

//    sigAction.sa_handler = sigchld_handler; // reap all dead processes    
//    sigemptyset(&sigAction.sa_mask);
//    sigAction.sa_flags = SA_RESTART;

    if (sigaction(SIGCHLD, &sigAction, NULL) == -1) {
        perror("sigaction");
        exit(1);
    }

    printf("server: waiting for connections...\n");


        sin_size = sizeof theirAddress;
        int new_fd = accept(socketFd, (struct sockaddr *)&theirAddress, &sin_size);
        if (new_fd == -1) {
            perror("accept");
            return 1;
        }

        inet_ntop(theirAddress.ss_family,
            get_in_addr((struct sockaddr *)&theirAddress),
            s, sizeof s);
        printf("server: got connection from %s\n", s);


            NodeConnection nc = NodeConnection();

            char ipstr[INET6_ADDRSTRLEN];
            getpeername(new_fd, (struct sockaddr*)&theirAddress, &sin_size);
            struct sockaddr_in *soc = (struct sockaddr_in *)&theirAddress;
            int port = ntohs(soc->sin_port);
            inet_ntop(AF_INET, &soc->sin_addr, nc.ipstr, sizeof ipstr);

            nc.fd = new_fd;
            nc.theirAddress = sockaddr_storage(theirAddress);
            nc.sin_size = sin_size;
            nc.port = port;
            newConnections.push_back(nc);

}
这从这里被称为:

int main(int argc, const char* argv[])
{
    RoutingManager *manager = new RoutingManager();
    manager->ParseInputFile("topo.txt", 10, 3, " ");

    manager->myConnection = new NetworkConnection("localhost", "7771");
    manager->myConnection->SetSocketHints();
    manager->myConnection->PopulateAddressInfo();
    manager->myConnection->BindSocket();

    while(1)
    {
        manager->myConnection->ListenForConnections();  // <- here
        if (manager->myConnection->newConnections.size() > 0)
        {
            manager->ActivateNewNode();
        }

    }
}
其中,
连接
是结构:

struct NodeConnection
{
    int fd;
    socklen_t sin_size;
    struct sockaddr_storage theirAddress; 
    char ipstr[INET6_ADDRSTRLEN];
    int port;
};
如果在调用
ActivateNewNode()
的过程中注释掉连接的分配,这种方法就可以了。即该行:

iter->second.connection = myConnection->newConnections.back();
但是,在未注释的情况下,当我回过头继续侦听新连接时,此代码段失败:

if (listen(socketFd, 15) == -1) {
        perror("listen");
        exit(1);
    }
出现错误:
错误的文件描述符

有人能告诉我这里缺少什么吗?

listen()
从while循环中去掉。

Hm,我为什么要这样做?我想持续监听新的连接。正如man所说:
listen()将sockfd引用的套接字标记为被动套接字,也就是说,将使用accept(2)
接受传入的连接请求。因此,
listen
应该只使用一次。RoutingManager类是什么样子的?当您执行
myConnection->newConnections.pop_back()
时,连接实例是否可能被最终确定并关闭?
if (listen(socketFd, 15) == -1) {
        perror("listen");
        exit(1);
    }