套接字客户端使用Select

时间:2016-09-08 12:54:53

标签: c sockets tcp

我试图实现一个客户端功能,它可以使用select()功能与多个服务器建立连接。但我不是使用select()功能的专家。 但是,我的客户端可以启用与多个服务器的连接,但它无法从这些多个服务器读取消息。

根据以下示例,我将客户端与两台服务器( 192.168.100.136, 192.168.100.138)连接起来。 与这两台服务器连接后,我的客户端只从一台服务器(192.168.100.136)接收消息,而不是从多台服务器接收消息。

我不确定我在示例程序中使用select()函数的方式。 请更正我的示例应用程序以获得所需的o / p .. 提前致谢

int main()
{
    int port = 10001 ;
    char ip[][32] = {"192.168.100.136","192.168.100.138"};
    int count = 2 ;
    int ret = clientSelect(ip,port,count);
    return 0 ;
}   
int clientSelect(char **ipAddr ,int port , int count)
    {
    SOCKET max_sd = 0;
    SOCKET socketId[10] = {0};
    SOCKET sd = 0 ;
    SOCKET client_sock[1024] = {0} ;
    fd_set readfds;
    int i ,j , ret;
    char recvBuf[1024] = "";
    char errMsg[256] = "" ;
    struct sockaddr_in server ;

    FD_ZERO(&readfds);
    // Socket Initialization
    for(i = 0; i<count ; i++)
    {
        ret = initSocket(&socketId[i]);//Small function to create socket
        if (ret != 1)
        {
            return ret ;
        }
        //Server info
        server.sin_addr.s_addr  = inet_addr(ipAddr[i]);
        server.sin_family       = AF_INET;
        server.sin_port         = htons(port);
        // Conect to server
        if (connect(socketId[i], (struct sockaddr *)&server , sizeof(server)) < 0)
        {
            printf("connect ::Failed to connect to server %s:%d",ipAddr[i],port);
            return -1;
        }
        // Set Socket fd
        FD_SET(socketId[i], &readfds); 

        max_sd = (max_sd>socketId[i])?max_sd:socketId[i];
    }
    while(TRUE)
    {
        ret = select(max_sd + 1, &readfds, NULL, NULL, NULL);
        if (ret < 0)
        {
            printf("select failed\n ");
            return -1;
        }
        for ( j = 0 ; j<max_sd ; j++)
        {
            sd = client_sock[j] ;
            if (FD_ISSET(sd, &readfds)) 
            {
                ret = recv(sd,(char *)recvBuf,sizeof(recvBuf), 0);
                if(ret > 0 )
                {
                    printf("Message received from socket %d : %s\n",sd,recvBuf);
                    send(sd,(char *)recvBuf,strlen(recvBuf),0);
                }
            }
        }
    }

    return ret ;

}

1 个答案:

答案 0 :(得分:2)

每次使用前必须初始化fd_set

您的代码应该是:

int clientSelect(char **ipAddr ,int port , int count)
{
    SOCKET max_sd = 0;
    SOCKET socketId[10] = {0};
    SOCKET sd = 0 ;

    fd_set readfds;
    int i ,j , ret;
    char recvBuf[1024] = "";
    char errMsg[256] = "" ;
    struct sockaddr_in server ;

    // Socket Initialization
    for(i = 0; i<count ; i++)
    {
        ret = initSocket(&socketId[i]);//Small function to create socket
        if (ret != 1)
        {
            return ret ;
        }
        //Server info
        server.sin_addr.s_addr  = inet_addr(ipAddr[i]);
        server.sin_family       = AF_INET;
        server.sin_port         = htons(port);
        // Conect to server
        if (connect(socketId[i], (struct sockaddr *)&server , sizeof(server)) < 0)
        {
            printf("connect ::Failed to connect to server %s:%d",ipAddr[i],port);
            return -1;
        }       
    }


    while(TRUE)
    {
        // init fd_set
        FD_ZERO(&readfds); // added by siva to initialize socket descriptors
        for(i = 0; i<count ; i++)
        {
            //FD_ZERO(&readfds); // Commented by siva to avoid initialization for each socket
            FD_SET(socketId[i], &readfds); 
            max_sd = (max_sd>socketId[i])?max_sd:socketId[i];
        }
        ret = select(max_sd + 1, &readfds, NULL, NULL, NULL);
        if (ret < 0)
        {
            printf("select failed\n ");
            return -1;
        }
        // warning: you don't know the max_sd value
        for(i = 0; i<count ; i++)
        {
            sd = socketId[i] ;
            if (FD_ISSET(sd, &readfds)) 
            {
                ret = recv(sd,(char *)recvBuf,sizeof(recvBuf), 0);
                if(ret > 0 )
                {
                    printf("Message received from socket %d : %s\n",sd,recvBuf);
                    send(sd,(char *)recvBuf,strlen(recvBuf),0);
                }
            }
        }
    }

    return ret ;

}