我正在使用 c 语言和 linux 平台进行套接字编程,我的要求是让服务器只监听两个或三个客户端。怎么可能?
最佳答案
您可以通过在 listen() 调用中指定第二个参数来实现。
我假设您使用的是 TCP 协议(protocol)。
来自手册页:
int listen(int sockfd, int backlog);
The backlog argument defines the maximum length to which the queue of pending connections for sockfd may grow. If a connection request arrives when the queue is full, the client may receive an error with an indication of ECONNREFUSED or, if the underlying protocol supports retransmission, the request may be ignored so that a later reattempt at connection succeeds.
所以对于两个客户 - 你像这样调用 listen: listen(fd, 2);
建议阅读:https://beej.us/guide/bgnet/html/multi/syscalls.html#listen
关于c - 在套接字编程中如何让服务器监听有限的客户端?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24133844/