当前位置:网站首页>Socket programming -- poll model
Socket programming -- poll model
2022-06-25 08:56:00 【Programming segment】
utilize poll The model implements multiple clients and one server CS Model —— multiple IO Multiplexing technology
【 Last one 】:select Model
【 Read before reading 】:poll Detailed explanation
- poll function
Same as select, Delegate the kernel to monitor read and write events
int poll(struct pollfd* fds, nfds_t nfds, int timeout);
param:
fds: Incoming and outgoing parameters
fds.fd: File descriptor to monitor , If fd=-1, Indicates that the kernel is no longer monitored
fds.events: POLLIN- Read events
POLLOUT- Write events
fds.revents: Events returned , Indicates that the kernel tells the program which file descriptors have events
nfds: Tell the kernel the scope of monitoring , The concrete is fds Maximum value of array subscript +1
timeout: Timeout time
-1: Permanent blocking
0: Go back to
>0: Blocking time
return:
>0: Indicates the number of file descriptors
=0: No file descriptor changes
-1: Abnormal said
Be careful
- struct pollfd In structure fd If the member is assigned to -1, Will not be monitored by the kernel ;
- Compare with select,poll There is no essential change , however poll Not subject to 1024 The limitation of ;
- select It can be used across platforms ,poll Only in linux Upper use
The server code is as follows , The client code remains unchanged
#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;
}
// Delegate the listening file descriptor to the kernel for monitoring
client[0].fd = lfd;
client[0].events = POLLIN;
int nready;
int max=0;// Indicates the scope of kernel monitoring , In the beginning 1 individual
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);
}
// There is a client connection request coming
if((client[0].fd == lfd) && (client[0].revents & POLLIN))
{
cfd = accept(lfd, NULL, NULL);
// seek client Available positions in the array
for(i=1; i<1024; i++)
{
if(client[i].fd == -1)
{
client[i].fd = cfd;
client[i].events = POLLIN;
break;
}
}
// If no location is available , Close the connection
if(i == 1024)
{
close(cfd);
continue;
}
max = i > max ? i : max;
// Description only the listening descriptor is set to 1, You can skip
if(--nready == 0)
{
continue;
}
}
// Some data has been sent
for(i=1; i<=max; i++)
{
// if fd=-1, Indicates that the connection is closed or there is no connection
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;
}
【 Next 】:epoll Model
边栏推荐
- 声纹技术(五):声纹分割聚类技术
- matplotlib matplotlib中决策边界绘制函数plot_decision_boundary和plt.contourf函数详解
- 《乔布斯传》英文原著重点词汇笔记(一)【 Introduction 】
- 4、 Convolution neural networks
- [MySQL] understanding and use of indexes
- Discrimination of configuration, software configuration items and software configuration management items
- 【无标题】**数据库课设:三天完成学生信息管理系统**
- matplotlib matplotlib中plt.grid()
- 《乔布斯传》英文原著重点词汇笔记(三)【 chapter one】
- Sharepoint:sharepoint server 2013 and adrms Integration Guide
猜你喜欢

Nodejs using the express framework demo

Numpy numpy中的meshgrid()函数

Oracle-单行函数大全

UEFI: repair efi/gpt bootloader

(translation) the use of letter spacing to improve the readability of all capital text

浏览器查看当前页面所有的监听事件
![[operation tutorial] how does the tsingsee Qingxi video platform import the old database into the new database?](/img/21/08194ac26dec50c222b88f1f64f894.png)
[operation tutorial] how does the tsingsee Qingxi video platform import the old database into the new database?

Oracle one line function Encyclopedia

关于I/O——内存与CPU与磁盘之间的关系

检测点是否在多边形内
随机推荐
RMB 3000 | record "tbtools" video, make a friend and get a cash prize!
证券开户风险大吗,安全靠谱吗?
WebGL谷歌提示内存不够(RuntimeError:memory access out of bounds,火狐提示索引超出界限(RuntimeError:index out of bounds)
Sharepoint:sharepoint server 2013 and adrms Integration Guide
Numpy numpy中的meshgrid()函数
Lvs-dr mode multi segment case
QSS buttons of different styles
C language "recursive series": recursive implementation of 1+2+3++ n
Notes on key words in the original English work biography of jobs (V) [chapter three]
cazy長安戰役八卦迷宮
Oracle-单行函数大全
cazy长安战役八卦迷宫
jmeter中csv参数化
flutter 多语言的intl: ^0.17.0导不进去
How to design test cases
Jmeter接口测试,关联接口实现步骤(token)
The city chain technology platform is realizing the real value Internet reconstruction!
tp6自动执行的文件是哪个?tp6核心类库有什么作用呢?
关掉一个线程
【MYSQL】索引的理解和使用