linux网络编程----->高并发--->poll多路I/O转接服务器

简介:

做网络服务的时候并发服务端程序的编写必不可少。前端客户端应用程序是否稳定一部分取决于客户端自身,而更多的取决于服务器是否相应时间够迅速,够稳定.

    常见的linux并发服务器模型;


  • 多进程并发服务器

  • 多线程并发服务器

  • select多路I/O转接服务器

  • poll多路I/O转接服务器

  • epool多路I/O转接服务器.


    本次主要讨论poll多路I/转接并发服务器模型:

    wKioL1dlV3ORUbUGAAISrviO-QM731.png-wh_50


    前几章介绍完了多进程并发服务器,  多线程并发服务器selete多路I/O转接服务器,  本章开始介绍poll(linux特有)多路I/O转接模型.

    由于多进程和多线程模型在实现中相对简单, 但由于其开销和CPU高度中比较大, 所以一般不用多线程和多进程来实现服务模型. select由于其跨平台, 但其最高上限默认为1024, 修改突破1024的话需要重新编译linux内核, poll完美解决了1024的限制.


    主要用到API:

        poll(struct pollfd *fds, nfds_t nfds, int timeout);

        fds:  传入传出结构体数组

        nfds: 结构体数组数量

        timeout: 监听时间        

                            -1 阻塞等待

                            0 立刻返回, 不阻塞

                            >0 等待毫秒数

        struct pollfd{

                int fd;              //监听的文件描述符

                int events;      //监听的事件 POLLIN监听读 POLLOUT 监听写 POLLERR 监听异常

                int revents;    // 监听事件中满足条件返回的事件


    .server[以下代码都没有做错误判断,  加上错误判断会造成代码翻倍, 实际开发中需要特别注意返回值]

    

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <poll.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <string.h>
#include <ctype.h>
 
#define CLIENT_MAX 1024       //定义最大客户端监听
#define SERV_PORT 9096        //监听端口
 
int  main( int  argc,  char * argv[]){
     int  listenfd, connfd;
     struct  sockaddr_in serv_addr, clie_addr;
     socklen_t clie_addr_len;
     struct  pollfd event[CLIENT_MAX];
     int  maxi, opt, i, j, nready, n;
     char  buf[BUFSIZ];
     
     //创建tcp监听套接字
     listenfd = socket(AF_INET, SOCK_STREAM, 0);
 
     //设置端口复用
     opt = 1;
     setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &opt,  sizeof (opt));
 
     //初始化为0
     bzero(&serv_addr,  sizeof (serv_addr));
     serv_addr.sin_family = AF_INET;
     //设置端口并转换为网络字节序
     serv_addr.sin_port = htons(SERV_PORT);
     //设置本机任意ip
     serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
     //绑定listenfd
     bind(listenfd, ( struct  sockaddr*)&serv_addr,  sizeof (serv_addr));
 
     //设置同时连接上限
     listen(listenfd, SOMAXCONN);    //#define SOMAXCONN 128
     
     //将监听套接字连接至事件
     event[0].fd = listenfd;
     event[0].events = POLLIN;
 
     //初始化
     for (i = 1; i < CLIENT_MAX; i++){
         event[i].fd = -1;
     }
 
     maxi = 0;
     for (;;){
         nready = poll(event, maxi+1, -1);
         
         //客户端请求连接
         if (event[0].revents & POLLIN){
             clie_addr_len =  sizeof (clie_addr);
             //获取连接
             connfd = accept(listenfd, ( struct  sockaddr*)&clie_addr, &clie_addr_len);
             //打印提示
             printf ( "%s:%d client connect successfully!\n" , inet_ntoa(clie_addr.sin_addr), ntohs(clie_addr.sin_port));
 
             //添加至监听
             for (i = 1; i < CLIENT_MAX; i++){
                 if (0 > event[i].fd){
                     event[i].fd = connfd;
                     break ;
                 }      
             }
             //判断监听是否已满
             if (CLIENT_MAX == i){
                 //关闭连接
                 printf ( "too many clients!\n" );
                 close(connfd);
                 continue ;
             }
             //监听读事件
             event[i].events = POLLIN;
             if (i > maxi)
                 maxi = i;
 
             if (0 == (--nready))
                 continue ;
         }
 
         for (i = 1; i <= maxi; i++){
             if (0 > event[i].fd)
                 continue ;
 
             //是否有事件
             if (event[i].revents & POLLIN){
                 //清空
                 bzero(buf,  sizeof (buf));
                 n = read(event[i].fd, buf,  sizeof (buf));
                 //对方是否已关闭
                 if (0 == n){
                     clie_addr_len =  sizeof (clie_addr);
                     //获取客户端信息
                     getpeername(event[i].fd, ( struct  sockaddr*)&clie_addr, &clie_addr_len);
                     printf ( "%s:%d client disconnect successfully!\n" , inet_ntoa(clie_addr.sin_addr), ntohs(clie_addr.sin_port));
                     //关闭客户端连接
                     close(event[i].fd);
                     //将事件数组中初始化为-1
                     event[i].fd = -1;
                 } else  if (0 < n){
                     //转换为大写
                     for (j = 0; j < n; j++){
                         buf[j] =  toupper (buf[j]);
                     }
                     //回写给客户端
                     write(event[i].fd, buf, n);
                 }
                 if (0 == (--nready)){
                     break ;
                 }
             }
         }
     }
     close(listenfd);
     return  0; 
}



      本文转自asd1123509133 51CTO博客,原文链接:http://blog.51cto.com/lisea/1791967,如需转载请自行联系原作者





相关文章
|
15天前
|
Java Linux
Springboot 解决linux服务器下获取不到项目Resources下资源
Springboot 解决linux服务器下获取不到项目Resources下资源
|
18天前
|
Linux
linux下搭建tftp服务器教程
在Linux中搭建TFTP服务器,需安装`tftp-server`(如`tftpd-hpa`)。步骤包括:更新软件包列表,安装`tftpd-hpa`,启动并设置开机自启,配置服务器(编辑`/etc/default/tftpd-hpa`),添加选项,然后重启服务。完成后,可用`tftp`命令进行文件传输。例如,从IP`192.168.1.100`下载`file.txt`: ``` tftp 192.168.1.100 &lt;&lt;EOF binary put file.txt quit EOF ```
28 4
|
23小时前
|
监控 安全 Linux
Linux系统之安装ServerBee服务器监控工具
【4月更文挑战第22天】Linux系统之安装ServerBee服务器监控工具
14 2
|
5天前
|
网络协议 安全 Linux
IDEA通过内网穿透实现固定公网地址远程SSH连接本地Linux服务器
IDEA通过内网穿透实现固定公网地址远程SSH连接本地Linux服务器
|
11天前
|
Linux 数据安全/隐私保护
Linux基础与服务器架构综合小实践
【4月更文挑战第9天】Linux基础与服务器架构综合小实践
1234 8
|
30天前
|
Ubuntu JavaScript 关系型数据库
在阿里云Ubuntu 20.04服务器中搭建一个 Ghost 博客
在阿里云Ubuntu 20.04服务器上部署Ghost博客的步骤包括创建新用户、安装Nginx、MySQL和Node.js 18.x。首先,通过`adduser`命令创建非root用户,然后安装Nginx和MySQL。接着,设置Node.js环境,下载Nodesource GPG密钥并安装Node.js 18.x。之后,使用`npm`安装Ghost-CLI,创建Ghost安装目录并进行安装。配置过程中需提供博客URL、数据库连接信息等。最后,测试访问前台首页和后台管理页面。确保DNS设置正确,并根据提示完成Ghost博客的配置。
在阿里云Ubuntu 20.04服务器中搭建一个 Ghost 博客
|
1月前
|
存储 弹性计算 数据可视化
要将ECS中的文件直接传输到阿里云网盘与相册(
【2月更文挑战第31天】要将ECS中的文件直接传输到阿里云网盘与相册(
417 4
|
1月前
|
SQL 弹性计算 安全
购买阿里云活动内云服务器之后设置密码、安全组、增加带宽、挂载云盘教程
当我们通过阿里云的活动购买完云服务器之后,并不是立马就能使用了,还需要我们设置云服务器密码,配置安全组等基本操作之后才能使用,有的用户还需要购买并挂载数据盘到云服务器上,很多新手用户由于是初次使用阿里云服务器,因此并不知道这些设置的操作流程,下面给大家介绍下这些设置的具体操作流程。
购买阿里云活动内云服务器之后设置密码、安全组、增加带宽、挂载云盘教程
|
19天前
|
弹性计算
阿里云ECS使用体验
在申请高校学生免费体验阿里云ECS云服务器后的一些使用体验和感受。
|
1月前
|
弹性计算
阿里云3M带宽云服务器并发多大?阿里云3M带宽云服务器测评参考
在探讨云服务器3M带宽能支持多大并发这一问题时,我们首先要明白一个关键点:并发量并非仅由带宽决定,还与网站本身的大小密切相关。一般来说,一个优化良好的普通网站页面大小可能只有几K,为便于计算,我们可以暂且假定每个页面大小为50K。
819 1