#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#define INFO printf
#define LISTENPORT 7777 //监听端口
#define BACKLOG 5 //最大监听数
#define MAXLEN 2048 //recv缓冲区长度
int listen_socket(int listen_port);
int main(int argc, char const *argv[])
{
int connfd = 0; //客户端的fd
int n = 0; //recv的返回值
char buff[MAXLEN]; //存储客户端发送过来的信息
int listenfd = listen_socket(LISTENPORT);
if (listenfd == -1)
{
INFO("listen socket error: %s(errno: %d)\n", strerror(errno), errno);
return -1;
}
// 用fd_set实现并发
fd_set rfds, rset;
FD_ZERO(&rfds);
FD_SET(listenfd, &rfds);
int max_fd = listenfd;
while (1)
{
rset = rfds;
int nready = select(max_fd + 1, &rset, NULL,
用select实现并发
最新推荐文章于 2022-09-26 20:01:10 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)