当前位置:网站首页>socket编程——poll模型
socket编程——poll模型
2022-06-25 08:07:00 【编程小段】
利用poll模型实现多个客户端和一个服务端的CS模型——多路IO复用技术
- poll函数
同select,委托内核监控可读可写事件
int poll(struct pollfd* fds, nfds_t nfds, int timeout);
param:
fds: 传入传出参数
fds.fd: 要监控的文件描述符,如果fd=-1,表示内核不再监控
fds.events: POLLIN-读事件
POLLOUT-写事件
fds.revents: 返回的事件,表示内核告诉程序有哪些文件描述符有事件发生
nfds: 告诉内核监控的范围,具体是fds数组下标的最大值+1
timeout: 超时时间
-1:永久阻塞
0:立刻返回
>0:阻塞时间
return:
>0: 表示发生文件描述符的个数
=0: 没有文件描述符发生变化
-1: 表示异常
注意
- struct pollfd结构体中的fd成员若被赋值成-1,是不会被内核监控的;
- 相较于select,poll没有本质上的变化,但是poll不受1024的限制;
- select可以跨平台使用,poll只能在linux上使用
服务端代码如下,客户端代码不变
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
#include<sys/types.h>
#include<arpa/inet.h>
#include<unistd.h>
#include<netinet/in.h>
#include<errno.h>
#include<poll.h>
int main()
{
int lfd = socket(AF_INET, SOCK_STREAM, 0);
if(lfd < 0)
{
perror("socket error");
return -1;
}
struct sockaddr_in serv;
bzero(&serv, sizeof(serv));
serv.sin_family = AF_INET;
serv.sin_port = htons(8888);
serv.sin_addr.s_addr = htonl(INADDR_ANY);
int ret = bind(lfd, (struct sockaddr*)&serv, sizeof(serv));
if(ret < 0)
{
perror("bind error");
return -1;
}
listen(lfd, 128);
int i;
struct pollfd client[1024];
for(i=0; i<1024; i++)
{
client[i].fd = -1;
}
//将监听文件描述符委托内核监控
client[0].fd = lfd;
client[0].events = POLLIN;
int nready;
int max=0;//表示内核监控的范围,一开始有1个
int cfd;
int sockfd;
int n;
char buf[1024];
while(1)
{
nready = poll(client, max+1, -1);
if(nready < 0)
{
if(errno == EINTR)
continue;
perror("poll error\n");
exit(-1);
}
//有客户端连接请求到来
if((client[0].fd == lfd) && (client[0].revents & POLLIN))
{
cfd = accept(lfd, NULL, NULL);
//寻找client数组中的可用位置
for(i=1; i<1024; i++)
{
if(client[i].fd == -1)
{
client[i].fd = cfd;
client[i].events = POLLIN;
break;
}
}
//若没有可用位置,则关闭连接
if(i == 1024)
{
close(cfd);
continue;
}
max = i > max ? i : max;
//说明只有监听描述符被置为1,后面可跳过
if(--nready == 0)
{
continue;
}
}
//有数据发来
for(i=1; i<=max; i++)
{
//若fd=-1,表示连接已关闭或者没有连接
if(client[i].fd == -1)
{
continue;
}
if(client[i].revents == POLLIN)
{
sockfd = client[i].fd;
memset(buf, 0, sizeof(buf));
n = read(sockfd, buf, sizeof(buf));
if(n <= 0)
{
close(sockfd);
client[i].fd = -1;
printf("read error or client close\n");
}
printf("n == [%d], buf == [%s]\n", n, buf);
for(int j=0; j<n; j++)
{
buf[j] = toupper(buf[j]);
}
write(sockfd, buf, n);
}
}
}
close(lfd);
return 0;
}
【下一篇】:epoll模型
边栏推荐
- Paper:Generating Hierarchical Explanations on Text Classification via Feature Interaction Detection
- Summary of hardfault problem in RTOS multithreading
- mysql之Unknown table ‘COLUMN_STATISTICS‘ in information_schema (1109)
- Super simple case: how to do hierarchical chi square test?
- C language: find all integers that can divide y and are odd numbers, and put them in the array indicated by B in the order from small to large
- 在华泰证券上面开户好不好,安不安全?
- LVS-DR模式单网段案例
- 在哪个平台买股票开户安全?求分享
- What are the indicators of VIKOR compromise?
- ¥3000 | 录「TBtools」视频,交个朋友&拿现金奖!
猜你喜欢

C language "Recursion Series": recursively realizing the n-th power of X

cazy長安戰役八卦迷宮

Day 5 script and UI System

2021 "Ai China" selection

What do various optimizers SGD, adagrad, Adam and lbfgs do?

对常用I/O模型进行比较说明

Find the nearest common ancestor (Sword finger offer) of two nodes in the binary tree (search tree)

【515. 在每个树行中找最大值】

WebGL谷歌提示内存不够(RuntimeError:memory access out of bounds,火狐提示索引超出界限(RuntimeError:index out of bounds)

城链科技平台,正在实现真正意义上的价值互联网重构!
随机推荐
四、卷积神经网络(Convolution Neural Networks)
Unity addressable batch management
Hyper-v:Hyper-v 第 1 代或第 2 代虚拟机
Lvs-dr mode multi segment case
在哪个平台买股票开户安全?求分享
Stimulsoft Ultimate呈现报告和仪表板
WebGL谷歌提示内存不够(RuntimeError:memory access out of bounds,火狐提示索引超出界限(RuntimeError:index out of bounds)
[reinforcement learning notes] common symbols in reinforcement learning
C#程序终止问题CLR20R3解决方法
UEFI:修复 EFI/GPT Bootloader
Meaning of Jieba participle part of speech tagging
C#启动程序传递参数丢失双引号,如何解决?
Summary of NLP data enhancement methods
How to implement a system call
Sharepoint:sharepoint server 2013 and adrms Integration Guide
Is it safe for Huatai Securities to open a stock account on it?
EasyPlayer流媒体播放器播放HLS视频,起播速度慢的技术优化
声纹技术(二):音频信号处理基础
TrendMicro:Apex One Server 工具文件夹
【MYSQL】事务的理解