C 套接字programming:single 服务器多个客户端

C socket programming:single server multiple clients

for (i = 0; i < climax; i++) //input output operations from clients
{
    sockfd = client_socket[i];
    if (FD_ISSET( sockfd , &fds))
    {
        if ((valread = read( sockfd , request, 1024)) == 0) //check for disconnection of client
        {
            getpeername(sockfd , (struct sockaddr*)&cliaddr , &length); //obtain details of disconnected client
            printf("Client Disconnected: IP::%s %s , port %d . \n", ipv6client,
                   ipv4client, ntohs(cliaddr.sin_port));
            close( sockfd );
            client_socket[i] = 0; //socket closing and set socket to null to be reused
        } else{
            request[valread] = '[=11=]'; //convert request to string and prepare for parsing

            printf("Request from ::%s %s , port %d: ", ipv6client,
                   ipv4client, ntohs(cliaddr.sin_port));

            printf("%s",request);
        }
    }
}

当客户端断开连接时,服务器会重复客户端发送的最后一条消息。我如何修改它以使其不再重新打印客户的消息?提前谢谢你

检查 valread == -1。你忽略那种情况,写在数组的开头之前,然后输出之前的请求。一种简单的方法是将 == 0 更改为 <= 0.