c++ - Cc 套接字编程 select() 的第一个参数

标签 c++ sockets select

我一直在尝试在 Linux 中进行一些网络编程,但我似乎又陷入了困境。我似乎没有得到 select() 函数的第一个参数。据我所知,它应该是最后创建的套接字filedescriptor + 1。大多数情况下,最高的 filedescriptor 包含数字 6。当我将 filedescriptor 添加到 fd_set 时,它突然变为 64——可能是因为它变为fd_bytes(如果我错了,请纠正我)。因此,6+1 似乎不适用于 select 语句,因为它一直阻塞,但如果我从 fd_set 获取 fd_bytes 则不会。我对这一切感到非常困惑,希望就这个主题获得一些建议,或者如果有人有的话,一个很好的 sigio 教程,可以让事情变得更容易。

服务器.cpp

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h> 
#include <sys/socket.h>
#include <netinet/in.h>
#include <boost/thread/thread.hpp>
#include <sys/time.h>
#include <sys/ioctl.h>

using namespace std;

void error(char *msg,int socket)
{
    perror(msg);
    close(socket);
    exit(1);
}

int main(int argc, char** argv) {
    int sockfd, newsockfd, portno, n;
    socklen_t clilen;
    fd_set readfds;
    FD_ZERO(&readfds);
/*
 * Sockfd, newsockfd contain values returned by the socket
 * portno stores the port number on which the server accepts connections
 * clilen stores the size of the address of the client
 * n contains the amount of character written of read
 */
    char buffer[256];
    /* buffer contains the characters read from the socket*/

    struct sockaddr_in serv_addr, cli_addr;
    /* 
     * sockaddr_in contains an internet address
     * serv_addr contains the servers address
     * cli addr contains the clients address
     */
    if(argc < 2)
    {
        fprintf(stderr,"ERROR no port provided");
       exit(1);
    }
    /*
     * error if no argument
     */
    sockfd = socket(AF_INET,SOCK_STREAM,0);
    int opt = 1;
    ioctl(sockfd, FIONBIO, &opt);

    if(sockfd < 0){
        error("ERROR opening socket", sockfd);
    }    
    /*
     * socket() creates a new socket
     * argument 1 contains the address domain
     * argument 2 contains the socket type
     * argument 3 contains the protocol should be 0
     * socket() returns a reference for itself
     */
    bzero((char*) &serv_addr, sizeof(serv_addr));
    /* empty the serv_addr variable*/
    portno = atoi(argv[1]);
    /*converts the port argument from string  to int*/
    serv_addr.sin_family = AF_INET;
    /*set the code for the address family*/
    serv_addr.sin_port = htons(portno);
    /*htons converts the portno to network bytes and gives it to the server address*/
    serv_addr.sin_addr.s_addr = INADDR_ANY;
    /*set the server ip to the ip of the running machine*/
    if(bind(sockfd,(struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
        error("ERROR on binding", sockfd);
    /*
     * bind() binds a socket to an address, in this case the
     * addess of the current host
     */
    listen(sockfd,5);
    /*the listen system call allows the process to listen on the socket for 
    connections*/
    clilen = sizeof(cli_addr);
    do{
    newsockfd = accept(sockfd,(struct sockaddr *) &cli_addr, &clilen);
    }while(newsockfd < 0);
    /*
     * accept() lets the system wait until a client connects to the server
     */
    FD_SET(newsockfd,&readfds);
    int sockcount = pselect(readfds.fds_bits[0] + 1,&readfds,NULL,NULL,NULL,NULL);

    bzero(buffer,256);
    n = read(newsockfd,buffer,255);
    if(n < 0) error("ERROR reading from socket", sockfd);
    printf("Here is the message: %s", buffer);
    /*
     * bzero empties the buffer
     * read obviously reads data from the new socket descriptor
     */
    n = write(newsockfd,"I got your message",18);
    if(n < 0) error("ERROR writing to socket", sockfd);

    close(sockfd);
    return 0;
}

一些(或全部)代码可能没有意义,但这主要是因为我只是测试代码的行为方式,然后使其成为多线程。上面的代码工作得更多或更少:它可以使用 telnet 或使用此处教程中的客户端进行测试 http://www.linuxhowtos.org/C_C++/socket.htm .

最佳答案

我之前从未见过任何人执行过 readfds.fds_bits[0] + 1 操作。对于您的情况,您可以在 pselect() 中使用 newsockfd + 1

关于c++ - Cc 套接字编程 select() 的第一个参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13051644/

相关文章:

c++ - 隐藏私有(private)重载虚函数?

python - 任何人都可以帮助我澄清我对 TCP/IP 系统调用的理解吗?

c# - Lambda 多个条件从 List<T> 中选择对象

sql - 计算列 sql 中的寄存器

c++ - C 预处理器作为语言创建工具的长度/限制是什么?我在哪里可以了解更多关于这些的信息?

c++ - noexcept 关键字和 _NOEXCEPT 宏有什么区别?

c++ - 在执行代码之前进入函数时引发异常

java - Android 客户端/Java 服务器套接字; android发送但不接收?

安卓媒体播放器 "Connection reset by peer"

javascript - 选择元素没有 'options' 数组