开发者

select and some clients

开发者 https://www.devze.com 2023-03-02 01:23 出处:网络
i\'m confused that i have a server by开发者_开发技巧 select(). and i can accept some clients. but i just only read the information from the client when connect the sever in the end.

i'm confused that i have a server by开发者_开发技巧 select(). and i can accept some clients. but i just only read the information from the client when connect the sever in the end. for example: client of A,B,C are connect the server in turn. the server just print the data from C.

when i gdb it, it block in the select() all the time. can you tell me why? thank you. the main code is:


thank you, i have updated the question, and i have changed the code by http://www.gnu.org/s/hello/manual/libc/Server-Example.html

but the problem is still come. i still only get the data from the end one. and i can't get the data from former one.

the main code is:

int read_from_client( int filedes )

{ char buffer[MAXMSG];

int nbytes;

    nbytes = read( filedes, buffer, MAXMSG );

    if ( nbytes < 0 )
    {
            /* Read error */
            perror( "read" );
            exit( EXIT_FAILURE );
    }
    else if ( nbytes == 0 )
    /* End-of-file. */
            return -1;
    else
    {
            /* Data read. */
            fprintf( stderr, "Server: got message: '%s'\n", buffer );
            return 0;
    }

}

int ts_server_poll( struct tcp_server *server, struct timeval *timeout ) {

unsigned int i;

int nready, sockfd, k, j;

    <br>//reset the fd set
    _server_set_fd( server );

    printf( "maxfd is %d\n", maxfd );
    FD_SET( server->fd, server->fd_set );

    while ( 1 )
    {
            /* Block until input arrives on one or more active sockets. */
            server->fd_readset = server->fd_set;

            if ( select( FD_SETSIZE, server->fd_readset, NULL, NULL, NULL ) < 0 )
            {
                    perror ( "select" );
                    exit( EXIT_FAILURE );
            }



            /* Service all the sockets with input pending */
            for ( i = 0; i < FD_SETSIZE; ++i )
                    if ( FD_ISSET( i, server->fd_readset ) )
                    {
                            if ( i == server->fd )
                            {
                                    /* Connection request on original socket. */
                                    int new;
                                    new = (int)accept( server->fd, 0, 0 );

                                    if ( new < 0 )
                                    {
                                            perror( "accept" );
                                            exit( EXIT_FAILURE );
                                    }

                                    FD_SET( new, server->fd_set );
                            }
                            else
                            {
                                    /* Data arriving on an already-connected socket. */
                                    if ( read_from_client( i ) < 0 )
                                    {
                                            close( i );
                                            FD_CLR( i, server->fd_set );
                                    }


Your code is missing several things ... accepting new connections, and going through all the descriptors in the read set.

Here's a complete example of how to use select in a server:

http://www.gnu.org/s/hello/manual/libc/Server-Example.html


You should be doing that in a loop. I mean, checking FD_ISSET from 0 to maxfd + 1 and then all that jazz.

for(sockfd = 0; sockfd < maxfd + 1; sockfd++) {
    if(FD_ISSET(sockfd, server->fd_readset)) {
        /* there is an event on sockfd */
    }
}

I haven't used select in a while but you should be ok with that. Also, you might want to skip the descriptors 0, 1, 2 but that would be plain ugly.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号