服务器比较简陋,为了学习poll的使用,只向客户端回写一条html语句。启动服务器后,浏览器发起请求,服务端向浏览器写回html,响应字符串,然后可以看到,浏览器解析并显示 Hello Poll!.

启动服务端:

用浏览器访问:

浏览器解析出字符串:

完整代码:

 #include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <poll.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/stat.h> #define POLLFD_SIZE 1024 /* struct pollfd 结构体数组最大上限 */ /* 关心描述符集事件数组*/
struct pollfd array_pollfd[POLLFD_SIZE]; /* 结构体成员详情
struct pollfd
{
int fd; // 关心的描述符
short events; // 关心的事件
short revents; // 发生的事件
};
*/ /* 获取一个监听连接的sockfd */
int run_getsockfd(const char*ip, int port); /* 执行poll检测 */
void run_poll(int listen_sockfd); /* 响应客户端的连接,并添加新的描述符到关心事件中 */
void run_accept(int listen_sock); /* 当与客户端连接的描述符有事件就绪时,做出响应 */
void run_action( int index); int main(int argc, char **argv)
{
if(argc != )
{
printf("usage: [server_ip] [server_port]");
return ;
} int listen_sockfd = run_getsockfd(argv[], atoi(argv[])); run_poll(listen_sockfd); return ;
} /* 调用poll 并检测返回事件 */
void run_poll(int listen_sockfd)
{
/* 将负责监听连接的sockfd注册事件 */
array_pollfd[].fd = listen_sockfd;
array_pollfd[].events = POLLIN; /* 初始化数组中的描述符 */
int idx_init = ;
for(; idx_init < POLLFD_SIZE; ++idx_init)
{
array_pollfd[idx_init].fd = -;
}
int timeout = ; /* 设定一秒后超时 */ while()
{
int ret_poll = poll(array_pollfd, POLLFD_SIZE, timeout); if(ret_poll == ) /* 超时 */
printf("timeout\n");
else if(ret_poll < ) /* 执行出错*/
perror("poll()");
else
{/* 有关心的事件就绪 */ /* 遍历数组,轮询检测poll的结果 */
int idx_check = ;
for(idx_check = ; idx_check < POLLFD_SIZE; ++idx_check)
{
if(idx_check == && array_pollfd[].revents & POLLIN)
{/* listen_sockfd 读事件就绪 */
run_accept(listen_sockfd);
}
else if(idx_check != )
{/* 与客户端连接的sockfd 有事件就绪 */
run_action(idx_check);
}
}
}
} // end while 1
} /* 当与客户端连接的描述符有事件就绪时,做出响应 */
void run_action( int index)
{
if(array_pollfd[index].revents & POLLIN)
{/* 客户端读事件发生 */
char buf[]; /* 存储从客户端读来的消息 */
memset(buf, , sizeof(buf));
ssize_t s = read(array_pollfd[index].fd, buf, sizeof(buf)-);
if(s > )
{
buf[s-] = '\0';
printf("client say$ %s \n", buf);
array_pollfd[index].events = POLLOUT;
}
else if( s <= )
{
printf("client quit!\n");
close(array_pollfd[index].fd);
array_pollfd[index].fd = -;
} }
else if (array_pollfd[index].revents & POLLOUT)
{/* 客户端写事件发生 */
/* 使用浏览器测试,写回到客户端,浏览器会解析字符串,显示 Hellp Epoll! */
const char* msg = "HTTP/1.1 200 OK\r\n\r\n<html><br/><h1>Hello poll!</h1></html>";
write(array_pollfd[index].fd, msg, strlen(msg));
close(array_pollfd[index].fd);
array_pollfd[index].fd = -;
}
} /* 响应客户端的连接,并添加新的描述符到关心事件中 */
void run_accept(int listen_sock)
{
struct sockaddr_in cliaddr;
socklen_t clilen = sizeof(cliaddr); int new_sock = accept(listen_sock, (struct sockaddr*)&cliaddr, &clilen);
if( new_sock < )
{
perror("accept");
return ;
} printf("与客户端连接成功: ip %s port %d \n", inet_ntoa(cliaddr.sin_addr), ntohs(cliaddr.sin_port));
/* 将新socket描述符添加到数组中 */
int idx_find = ;
for(; idx_find < POLLFD_SIZE; ++idx_find)
{
if(array_pollfd[idx_find].fd < )
{
array_pollfd[idx_find].fd = new_sock;
array_pollfd[idx_find].events = POLLIN ;
break;
}
}
if(idx_find == POLLFD_SIZE)
{
perror("连接超出最大限度,add array_pollfd[]");
return;
} } /* 获取一个监听socket */
int run_getsockfd(const char* ip, int port)
{
int sock = socket(AF_INET, SOCK_STREAM, );
if( sock < ){
perror("socket()");
exit();
} int opt = ;
setsockopt(sock , SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); struct sockaddr_in server;
bzero(&server, sizeof(server));
server.sin_addr.s_addr = inet_addr(ip);
server.sin_port = htons(port);
server.sin_family = AF_INET; if(bind(sock, (struct sockaddr *)&server, sizeof(server) ) < ){
perror("bind()");
exit();
} if(listen(sock, ) < ){
perror("listen()");
exit();
} return sock;
}

最新文章

  1. C# Math类简介
  2. php产生随机数函数
  3. 【TYVJ】1359 - 收入计划(二分)
  4. 出现segment fault 错误的几种原因
  5. Hibernate4搭建Log4J日志管理(附Log4j.properties配置详解)
  6. __stdcall,__cdecl,_cdecl,_stdcall,。__fastcall,_fastcall 区别简介
  7. 为什么windows dos和Linux shell有这样的差别??
  8. phpcms通过URL传参
  9. java循环、数组练习
  10. jQuery实现拼图小游戏
  11. [CVPR2017] Weakly Supervised Cascaded Convolutional Networks论文笔记
  12. pip安装问题
  13. C/C++中宏定义#pragma once与 #ifndef的区别
  14. java依赖的斗争:依赖倒置、控制反转和依赖注入
  15. 如何在你的Vue项目配置vux
  16. scrapy --&gt;CrawlSpider 介绍
  17. 如何实现圆形的进度条(ProgressBar)
  18. 【Python】Python-基础语法学习
  19. OMShell常用命令及遇到的问题
  20. 使用VisualSVN建立SVN服务器

热门文章

  1. CSS3 3D旋转下拉菜单
  2. RHCE学习笔记 管理1 (第六章 第七章)
  3. mysql错误总结-ERROR 1067 (42000): Invalid default value for TIMESTAMP
  4. Excel下载打不开
  5. java定时任务Quartz Demo(2.X)
  6. javascript 内置对象及常见API
  7. 【jsoi】第一季 [略]精简题解
  8. scapy学习笔记(2)
  9. $.ajax应用之请求头headers
  10. OSGi类加载问题