管理connect()到多个主机的套接字

时间:2014-09-11 05:07:33

标签: c sockets

我的程序就像服务器(listen() s,bind()accept()来自其他IP中相同程序的多个连接一样,同时也是如此像UNIX shell一样接受用户输入。

用户输入用于命令,并且命令CONNECT具有地址和端口号规范。

我想让它连接到多个主机,我想我必须为每个连接使用一个套接字。

当我在用户CONNECT请求时创建套接字时,如何管理这些套接字?

我应该使用全局array套接字并在建立连接时为其创建和添加套接字吗?

2 个答案:

答案 0 :(得分:0)

我假设您要在服务器中管理多个客户端。对?

因此,您必须全局采用一些预定义的套接字数组,然后为每个套接字提供服务

离。

long accept_sock[10];//This should be global
                     //initilize it with -1 at entry point
while(1)
{
    struct sockaddr   client;  /* for BSDish accept() call */
    int               clientsize;
    clientsize = sizeof(struct sockaddr_in);
    long sock ;

    for(int i=0;i<10;i++)
    {
        if(accept_sock[i] == -1)
        {    
            sock= accept(server_sock, &client, &clientsize);

            if(sock != -1)
            {
                acepct_sock[i] =sock;
                break;
            }
        }
    }

    for(int i=0;i<10;i++)
    {
        if(accept_sock[i] != -1)
        {    
          //do process 
        }
    }
}

答案 1 :(得分:0)

I think you need Concurrent Servers.There are two main classes of servers, iterative and concurrent. An iterative server iterates through each client, handling it one at a time. A concurrent server handles multiple clients at the same time. The simplest technique for a concurrent server is use select/fork(). 


 #define FD_SETSIZE 20   /* MAX number of client */

  for (i = 0; i < FD_SETSIZE; i++)
                client[i] = -1;
        FD_ZERO(&allset);
        FD_SET(listenfd, &allset);

        for ( ; ; ) {
                rset = allset;
                nready = select(maxfd+1, &rset, NULL, NULL, NULL);

                if (FD_ISSET(listenfd, &rset)) {        /* new client connection */
                        printf("listening socket readable\n");
                        clilen = sizeof(cliaddr);
                        connfd = accept(listenfd, (SA *) &cliaddr, &clilen);

                        for (i = 0; i < FD_SETSIZE; i++)
                                if (client[i] < 0) {
                                        client[i] = connfd;     /* save descriptor */
                                        break;
                                }
                        if (i == FD_SETSIZE)
                                printf("too many clients");

                        FD_SET(connfd, &allset);        /* add new descriptor to set */
                        if (connfd > maxfd)
                                maxfd = connfd;                 /* for select */
                        if (i > maxi)
                                maxi = i;                               /* max index in client[] array */

                        if (--nready <= 0)
                                continue;                               /* no more readable descriptors */
                }

                for (i = 0; i <= maxi; i++) {   /* check all clients for data */
                         if ( (sockfd = client[i]) < 0)
                                continue;
                        if (FD_ISSET(sockfd, &rset)) {
                                /* do process */
                        }
                }
        }