如何知道新客户端是否正在使用 select() 连接到套接字

问题描述 投票:0回答:1

我的问题是:(使用 select())我如何知道是否有新客户端正在连接我的服务器? A 不能只使用accept,因为accept() 是阻塞的...

示例: 我在 fd user1 (fd = 4) 和 user2 (fd = 5) 上设置了两个客户端。因此,我使用 select 来知道谁在套接字上写入:

  FD_ZERO(read_fds);
  FD_SET(user1, read_fds);
  FD_SET(user2, read_fds);
  error = select(user2 + 1, read_fds, NULL, NULL, NULL);
  if (FD_ISSET(user1, read_fds) == 1)
     printf("user1 talks.\n");
  ...

但是我想知道如果有新用户连接到服务器,我该怎么办? 我已经尝试过了:

  FD_ZERO(read_fds);
  FD_SET(user1, read_fds);
  FD_SET(user2, read_fds);
  FD_SET((user2 + 1), read_fds);
  error = select(user2 + 2, read_fds, NULL, NULL, NULL);
  if (FD_ISSET(users2 + 1) == 1)
  {
      printf("New user.\n");
      accept(..., ...);
  }
  ...

但是使用这段代码,每次都选择return -1...

解决方案? ^^

c sockets posix-select
1个回答
2
投票

您必须将连接的客户端存储在某个地方,例如列表。

struct client_node
{
    int sockfd;
    /* Other data that might be needed */
    struct client_node *next;
};

struct client_node *client_list = NULL;

/* ... */

FD_ZERO(&read_fds);
FD_SET(listening_socket, &read_fds);

int maxfd = listening_socket;

for (struct client_node *c = client_list; c; c = c->next)
{
    FD_SET(c->sockfd, &read_fds);
    maxfd = MAX(maxfd, c->sockfd);
}

int res = select(maxfd + 1, &read_fds, NULL, NULL, NULL);

至于

accept
的阻塞问题,如您所见,我添加了一个套接字(变量
listening_socket
),它是您正在侦听的套接字。当
select
说你可以从中读取时,这意味着有一个新的连接,你可以调用accept。

© www.soinside.com 2019 - 2024. All rights reserved.