c - 在套接字编程中如何让服务器监听有限的客户端?

标签 c linux network-programming

我正在使用 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/

相关文章:

linux - 文件重命名问题

c# - 从 NetworkStream 调用 BeginRead

java - 用 Java 广播 UDP 数据包的正确方法是什么?

C 预处理器 : #define in C. .. 建议

python - 通过API调用xbacklight

c - C 中 intBitsTofloat 中丢弃的精度值

c - 为什么这样的结构体包含两个仅包含一个元素的数组字段?

linux - 在 oracle linux (oel) 上安装本地 rpm 包的 Chef 问题

java - 将 Netty 服务器与 Akka 集成?

c - PowerPC 编译器的结构体数组