gpt4 book ai didi

c++ - 使用 C++ 中的示例服务器源替代 winsock2

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:21:32 24 4
gpt4 key购买 nike

我正在使用在 http://tangentsoft.net/wskfaq/examples/basics/select-server.html 中找到的这个示例实现

这完成了我需要的大部分工作,无阻塞地处理连接并在其线程中完成所有工作(不像某些示例那样为每个连接创建一个新线程),但我很担心,因为有人告诉我 winsock将只支持最多 64 个客户端连接 :S

这 64 个连接是真的吗?

我还有什么其他选择?如果有一个类似实现的 C++ 示例会很酷。

谢谢

最佳答案

替代库:

您应该考虑使用 boost asio .它是一个跨平台网络库,可以简化您可能必须完成的许多任务。

您可以找到 example source code you seek here .


关于 64 位限制:

没有硬性的 64 连接限制,您将通过良好的设计体验到这一点。基本上,如果您使用某种线程模型,您将不会遇到此限制。

以下是您听说过的有关限制的一些信息:

4.9 - What are the "64 sockets" limitations?

There are two 64-socket limitations:

The Win32 event mechanism (e.g. WaitForMultipleObjects()) can only wait on 64 event objects at a time. Winsock 2 provides the WSAEventSelect() function which lets you use Win32's event mechanism to wait for events on sockets. Because it uses Win32's event mechanism, you can only wait for events on 64 sockets at a time. If you want to wait on more than 64 Winsock event objects at a time, you need to use multiple threads, each waiting on no more than 64 of the sockets.

The select() function is also limited in certain situations to waiting on 64 sockets at a time. The FD_SETSIZE constant defined in winsock.h determines the size of the fd_set structures you pass to select(). It's defined by default to 64. You can define this constant to a higher value before you #include winsock.h, and this will override the default value. Unfortunately, at least one non-Microsoft Winsock stack and some Layered Service Providers assume the default of 64; they will ignore sockets beyond the 64th in larger fd_sets.

You can write a test program to try this on the systems you plan on supporting, to see if they are not limited. If they are, you can get around this with threads, just as you would with event objects.

Source

关于c++ - 使用 C++ 中的示例服务器源替代 winsock2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/411295/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com