2

我一直在尝试在 Linux 中进行一些网络编程,但我似乎又被卡住了。我似乎没有得到 select() 函数的第一个参数。filedescriptor + 1据我所知,它应该是最后一个制作的套接字。大多数情况下,最高filedescriptor值包含数字 6。当我将它添加filedescriptor到时,fd_set它突然变为 64——可能是因为它变为fd_bytes(如果我错了,请纠正我)。因此, 6+1 似乎没有用于 select 语句,因为它一直阻塞,但如果我fd_bytesfd_set. 我对所有这一切都感到非常困惑,并且想就这个主题提供一些建议,或者如果有人有它,一个很好的 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的教程中的客户端进行测试。

4

2 回答 2

1

我以前从未播种过任何人readfds.fds_bits[0] + 1。对于您的情况,您可以只使用newsockfd + 1in pselect()

于 2012-10-24T14:51:04.537 回答
1

根据 pselect 的文档,如果您不提供 timespec 参数,它将阻塞,这就是您的代码中发生的情况。无论如何,您对 pselect 的调用实际上没有任何意义,因为 a)您不会根据对 pselect 的调用结果做出任何决定,并且 b)您随后立即调用 recv,这是一个阻塞调用。

于 2012-10-24T14:51:12.930 回答