0

所以这是我的问题,我有一个服务器在客户端连接时调用此代码,问题是,一旦客户端调用,它就会进入无限循环,我不知道为什么。我希望代码在 Select 上再次等待另一个客户端连接,但是,初始触发器继续导致永无止境的循环。

当一个客户端连接时输出:

Lets startYOOHELLO WORLD WE MATCH
Lets startYOOHELLO WORLD WE MATCH
...
.
.
.
..
4

2 回答 2

0

如果您的服务器只想接受连接,那么您应该只选择原始套接字 FD。您不应该从接受中选择新的 FD。

于 2013-10-14T16:51:01.567 回答
0

您陷入无限循环的原因是因为you are not accepting new connections from the clients在您的无限循环和结构数组中的文件/套接字描述符中,are not valid any more因为这些客户端已经终止。您选择了一种非常差的使用 select 的机制。

你应该这样做:

while(1)
{
    FD_ZERO(&readfds);

    //add master socket to set
    FD_SET(master_socket, &readfds);
    max_sd = master_socket;

    //add child sockets to set
    for ( i = 0 ; i < max_clients ; i++) 
    {
        //socket descriptor
        sd = client_socket[i];

        //if valid socket descriptor then add to read list
        if(sd > 0)
            FD_SET( sd , &readfds);

        //highest file descriptor number, need it for the select function
        if(sd > max_sd)
            max_sd = sd;
    }

    //wait for an activity on one of the sockets , timeout is NULL , so wait indefinitely
    activity = select( max_sd + 1 , &readfds , NULL , NULL , NULL);

    if ((activity < 0) && (errno!=EINTR)) 
    {
        printf("select error");
    }

    //If something happened on the master socket , then its an incoming connection
    if (FD_ISSET(master_socket, &readfds)) 
    {
        if ((new_socket = accept(master_socket, (struct sockaddr *)&address, (socklen_t*)&addrlen))<0)
        {
            perror("accept");
            exit(EXIT_FAILURE);
        }

        //inform user of socket number - used in send and receive commands
        printf("New connection , socket fd is %d , ip is : %s , port : %d \n" , new_socket , inet_ntoa(address.sin_addr) , ntohs(address.sin_port));

        //send new connection greeting message
        if( send(new_socket, message, strlen(message), 0) != strlen(message) ) 
        {
            perror("send");
        }

        puts("Welcome message sent successfully");

        //add new socket to array of sockets
        for (i = 0; i < max_clients; i++) 
        {
            //if position is empty
            if( client_socket[i] == 0 )
            {
                client_socket[i] = new_socket;
                printf("Adding to list of sockets as %d\n" , i);

                break;
            }
        }
    }

    //else its some IO operation on some other socket :)
    for (i = 0; i < max_clients; i++) 
    {
        sd = client_socket[i];

        if (FD_ISSET( sd , &readfds)) 
        {
            //Check if it was for closing , and also read the incoming message
            if ((valread = read( sd , buffer, 1024)) == 0)
            {
                //Somebody disconnected , get his details and print
                getpeername(sd , (struct sockaddr*)&address , (socklen_t*)&addrlen);
                printf("Host disconnected , ip %s , port %d \n" , inet_ntoa(address.sin_addr) , ntohs(address.sin_port));

                //Close the socket and mark as 0 in list for reuse
                close( sd );
                client_socket[i] = 0;
            }

            //Echo back the message that came in
            else
            {
                //set the string terminating NULL byte on the end of the data read
                buffer[valread] = '\0';
                send(sd , buffer , strlen(buffer) , 0 );
            }
        }
    }
}

return 0;
} 
于 2013-10-14T16:51:10.287 回答