引自 this socket tutorial :
Sockets come in two primary flavors. An active socket is connected to a remote active socket via an open data connection... A passive socket is not connected, but rather awaits an incoming connection, which will spawn a new active socket once a connection is established ...
Each port can have a single passive socket binded to it, awaiting incoming connections, and multiple active sockets, each corresponding to an open connection on the port. It's as if the factory worker is waiting for new messages to arrive (he represents the passive socket), and when one message arrives from a new sender, he initiates a correspondence (a connection) with them by delegating someone else (an active socket) to actually read the packet and respond back to the sender if necessary. This permits the factory worker to be free to receive new packets. ...
然后教程解释说,连接建立后,事件套接字继续接收数据,直到没有剩余字节,然后关闭连接。
我不明白的是:假设有一个传入连接到端口,并且发送者希望每 20 分钟发送一些小数据。如果事件套接字在没有剩余字节时关闭连接,发送方是否每次要发送数据时都必须重新连接到端口?我们如何将曾经建立的连接保持更长时间?你能告诉我我在这里缺少什么吗?
我的第二个问题是,谁来确定同时工作的事件套接字的限制?
最佳答案
发送方应定期发送 KEEPALIVE 数据包以保持连接处于事件状态。 KEEPALIVE 的格式取决于协议(protocol)。它可以小到 TCP 数据段中的单个 NULL。
至于第二个问题……这取决于 I/O。如果它阻塞 I/O,那么您只希望计算机上运行一定数量的线程,因此您将无法拥有很多客户端。如果它是非阻塞的,您可以拥有更多的客户端。编程语言应该同时支持阻塞和非阻塞 I/O。 (我知道 Java 确实如此。)
它还取决于带宽、每个客户端的数据传输、内存、时钟速度等。但是非阻塞与阻塞可以在您可以接受的客户端数量上产生巨大差异。在服务器崩溃的情况下,您可能无法阻止超过 5-10 个客户端……但如果您不阻止,则可以有数千个客户端。
关于sockets - 被动和主动 socket ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4696812/