accept()函数在Winsock中的工作原理是怎样的?

huangapple go评论89阅读模式
英文:

How exactly does the accept() Function in Winsock work?

问题

我正在学习使用C++和Winsock。

一旦ListenSocket通过listen()开始工作,我应该如何以清晰的方式处理多个连接?

当前,每当建立一个新连接时,它都会被移到ConnectedSocket中,当建立新连接时,它会被覆盖。

  1. //监听和成功报告
  2. result = listen(ListenSocket, 2);
  3. if (result == 0)
  4. {
  5. PRINT("正在监听...")
  6. }
  7. else
  8. {
  9. closesocket(ListenSocket);
  10. PRINT("无法监听")
  11. return;
  12. }
  13. //接受连接
  14. ConnectedSocket = accept(m_Socket, nullptr, NULL);

我的第一个想法是创建一个包含套接字的数组,但我如何知道何时建立了新连接,因为我无法自己编写处理新连接的代码。目前我唯一想到的方法是每次运行我的代码时检查连接的套接字是否无效,然后将其移到数组中(如果它是有效的),这不是一个好的选择,因为当其他客户端尝试同时连接时,其中一个可能会被覆盖。

我相信有更好的方法,不是吗?

英文:

I am learning Winsock with c++ right now.

Once the ListenSocket has started with listen(), how am i supposed to handle more than one connection in a clean way?

Right now, everytime a new connection is established it is moved into the ConnectedSocket, and when a new connectiion is established, it gets overridden.

  1. //Listen & Success Report
  2. result = listen(ListenSocket, int(2));
  3. if (result == 0)
  4. {
  5. PRINT("Listening...")
  6. }
  7. else
  8. {
  9. closesocket(ListenSocket);
  10. PRINT("Failed to Listen")
  11. return;
  12. }
  13. //Accept
  14. ConnectedSocket = accept(m_Socket, nullptr, NULL);

My first Idea was to create an Array that holds the Sockets but how do i know when a new connection has been established, since i don't get to write my own code for handling new connections. The only way i can think of right now is to check everytime i run my code, wether the connected Socket is Invalid or not, and then move it into an Array(if it is valid), which is not a good alternative because when other Clients try to Connect at the same time, one of them might get overriden.

I am sure there is a better way, isn't it?

答案1

得分: 2

任何非平凡的服务器都会像这样循环调用accept

  1. while (true) {
  2. SOCKET connected = accept(m_Socket, nullptr, nullptr);
  3. // 对连接的`connected`进行操作
  4. }

请注意,accept的返回值被分配给了while循环中的局部变量。

一个典型的“对连接进行操作”的方式可以是创建一个新线程,接收connected中的套接字的副本。或者,您可以将套接字添加到一个全局集合中,然后使用select或类似的机制进行轮询。

英文:

Any non-trivial server will call accept in a loop like so:

  1. while (true) {
  2. SOCKET connected = accept(m_Socket, nullptr, nullptr);
  3. // do something with connected
  4. }

Note that the return value of accept is assigned to a variable that is local to the while loop.

A typical "do something" could be to spawn a new thread that receives a copy of the socket in connected.
Alternatively, you might want to add the socket to a global set that you poll with select or equivalent mechanism.

答案2

得分: 0

在单线程服务器中,通常将socket描述符放入容器中,如std::vector<SOCKET>,然后使用select或类似的函数等待所有套接字上的事件,同时处理多个套接字的事件。

示例:

  1. std::vector<SOCKET> sockets;
  2. sockets.push_back(m_Socket); // 服务器套接字
  3. fd_set readfds{};
  4. for(auto s : sockets) FD_SET(s, &readfds); // 将套接字放入fd_set
  5. int ready_count = select(0 /*在WinSock中被忽略*/,
  6. &readfds, nullptr, nullptr, nullptr);
  7. // 用于新连接和关闭连接的簿记:
  8. std::vector<bool> closed_sockets(sockets.size());
  9. std::vector<SOCKET> new_sockets;
  10. for(int i = 0; i < ready_count; ++i) {
  11. if(not FD_ISSET(sockets[i], &readfds) { // 未就绪
  12. ++ready_count;
  13. continue;
  14. }
  15. // sockets[i] 已准备好读取
  16. if(sockets[i] == m_Socket) {
  17. // 服务器
  18. SOCKET ConnectedSocket = accept(m_Socket, nullptr, NULL);
  19. if(ConnectedSocket == INVALID_SOCKET) {
  20. // 错误
  21. } else {
  22. // 保存此连接
  23. new_sockets.push_back(ConnectedSocket);
  24. }
  25. } else {
  26. // 客户端套接字之一
  27. // 如果从 `socket[i]` 读取指示客户端关闭连接:
  28. closed_sockets[i] = true;
  29. }
  30. }
  31. // 从`closed_sockets`中删除设置为`true`的套接字
  32. // 从`new_sockets`中将新连接添加到`sockets`
英文:

In a single threaded server you typically put the socket descriptor in a container, like a std::vector&lt;SOCKET&gt;, and then wait for events on all the sockets using
select or a similar function that can wait on events on many sockets at the same time.

Example:

  1. std::vector&lt;SOCKET&gt; sockets;
  2. sockets.push_back(m_Socket); // the server socket
  3. fd_set readfds{};
  4. for(auto s : sockets) FD_SET(s, &amp;readfds); // put sockets in the fd_set
  5. int ready_count = select(0 /*ignored in WinSock*/,
  6. &amp;readfds, nullptr, nullptr, nullptr);
  7. // for bookkeeping of new and closed connections:
  8. std::vector&lt;bool&gt; closed_sockets(sockets.size());
  9. std::vector&lt;SOCKET&gt; new_sockets;
  10. for(int i = 0; i &lt; ready_count; ++i) {
  11. if(not FD_ISSET(sockets[i], &amp;readfds) { // not ready
  12. ++ready_count;
  13. continue;
  14. }
  15. // sockets[i] is ready to be read
  16. if(sockets[i] == m_Socket) {
  17. // server
  18. SOCKET ConnectedSocket = accept(m_Socket, nullptr, NULL);
  19. if(ConnectedSocket == INVALID_SOCKET) {
  20. // error
  21. } else {
  22. // save this connection
  23. new_sockets.push_back(ConnectedSocket);
  24. }
  25. } else {
  26. // one of the client sockets
  27. // if reading from `socket[i]` indicates that the client
  28. // closed the connection:
  29. closed_sockets[i] = true;
  30. }
  31. }
  32. // Remove sockets set to `true` in `closed_sockets`
  33. // Add new connections to `sockets` from `new_sockets`

huangapple
  • 本文由 发表于 2023年2月23日 19:43:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75544356.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定