在C ++网络中,使用select,我首先必须listen()和accept()?

时间:2010-02-17 21:10:47

标签: c++ networking sockets select

我正在尝试允许多个客户端使用select连接到主机。我是否必须连接每一个,告诉他们移动到另一个端口,然后重新连接新端口?或者会选择允许我将多个客户端连接到同一个端口?

这是客户端代码:

    int rv;
int sockfd, numbytes;

if ((rv = getaddrinfo(hostName, hostPort, &hints, &servinfo)) != 0) {
    cout << "Could not get server address.\n";
    exit(1);
}

// loop through all the results and connect to the first we can
for(p = servinfo; p != NULL; p = p->ai_next) {
    if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
        perror("Client: no socket");
        continue;
    }

    if (connect(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
        close(sockfd);
        perror("Client: connect");
        continue;
    }

    break;
}

if (p == NULL) {
    fprintf(stderr, "Unable to connect to server.\n");
    exit(2);
}

FD_SET(sockfd, &masterSet);

这是服务器代码:

        int rv = getaddrinfo(NULL, port, &hints, &res);
    int yes = 1;//Not sure what this is for, found it in Beej's
    if(rv != 0){
            cout<< "Error, nothing matches criteria for file descriptor.\n";
            exit(1);
    }
    int fdInit;
    for(temp = res; temp != NULL; temp = temp->ai_next){
            if((fdInit = socket(temp->ai_family, temp->ai_socktype, temp->ai_protocol)) == -1){
                    cout << "This is not the fd you're looking for.  Move along.\n";
                    continue; //This is not the fd you're looking for, move along.
            }

            if(setsockopt(fdInit, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1){
                    cout << "Doom has fallen upon this set socket.\n";
                    perror("setsockopt");
                    exit(1); //Unable to set socket, exit program with code 1
            }

            if(bind(fdInit, temp->ai_addr, temp->ai_addrlen) == -1){
                    cout << "Could not bind fd\n";
                    close(fdInit);
                    continue; //Could not bind fd, continue looking for valid fd
            }
            break; //If a valid fd has been found, stop checking the list
    }
    if(temp==NULL){
            cout<<"Server failed to bind a socket\n";
            exit(2);
    }

    cout << fdInit << endl;
    //Setup the file descriptor for initial connections on specified port
    freeaddrinfo(res);
    FD_SET(fdInit, &masterSet);

任何帮助都会很棒!感谢。

3 个答案:

答案 0 :(得分:12)

TCP连接由连接的两个端的IP地址和端口号标识。因此,有很多客户端(通常会随机分配端口号)连接到单个服务器端口,这很好。

您创建一个套接字并将bind()创建到listen()的端口,然后等待客户端敲击它。如果你不介意阻止,你可以直接在它上面调用accept(),但你不会做任何超时循环或任何事情。否则,您可以在侦听套接字上select(),当客户端尝试连接时,它将变为可读,然后调用accept()

accept()将返回一个新创建的套接字,这是与客户端通信的实际套接字。原始侦听套接字继续侦听,并且可以接受更多连接。

通常使用select()循环来查找侦听套接字和任何连接套接字的可读性。然后当select()返回时,您只需检查侦听套接字是否可读,如果是,accept();否则寻找一个可读的连接套接字并处理它。

fd_set fds;
int max = 0, reuse = 1;
struct timeval tv;
int server;
std::vector<int> connected;

// create server listening socket
server = socket(PF_INET, SOCK_STREAM, getprotobyname("tcp")->p_proto);
setsockopt(server, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(int)); // optional, but recommended
if (bind(server, (struct sockaddr *)&addr, sizeof(struct sockaddr_in)) < 0) {
    // error, could not bind server socket
}
if (listen(server, 8) < 0) {
    // error, could not listen on server port
}

// loop looking for connections / data to handle
while (running) {
    FD_ZERO(&fds);
    FD_SET(server, &fds);
    if (server >= max) max = server + 1;

    for (std::vector<int>::iterator it = connected.begin(); it != connected.end(); ++it) {
        FD_SET(*it, &fds);
        if (*it >= max) max = *it + 1;
    }

    tv.tv_sec = 2; tv.tv_usec = 0;
    if (select(max, &fds, NULL, NULL, &tv) > 0) {
        // something is readable
        if (FD_ISSET(server, &fds)) {
            // it's the listener
            connected.push_back(accept(server, (struct sockaddr *)&addr));
        }
        for (std::vector<int>::iterator it = connected.begin(); it != connected.end(); ++it) {
            if (FD_ISSET(*it, &fds)) {
                // handle data on this connection
            }
        }
    }
}

答案 1 :(得分:3)

许多客户端可以连接到同一个端口

你必须先听,然后选择

当select告诉您有新连接时,请接受。它通过标记套接字fd上的读取来告诉您客户端已连接。

答案 2 :(得分:1)

您必须将服务器套接字标记为listen(2),但只有在accept(2)变为可读时才会调用select(2) - 这意味着新的连接请求待处理。

请注意,如果您不使用非阻塞套接字select(2)返回之间会有 竞选机会 并且调用accept(2) - 连接客户端可能会在此期间放弃尝试 - 因此您仍然可以阻止。