epoll演示样本
server参考是别人的代码
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
#include <fcntl.h>
#include <sys/epoll.h>
#include <sys/time.h>
#include <sys/resource.h>
#define MAXBUF 1024
#define MAXEPOLLSIZE 10000
/*
setnonblocking - 设置句柄为非堵塞方式
*/
int setnonblocking(int sockfd)
{
if (fcntl(sockfd, F_SETFL, fcntl(sockfd, F_GETFD, 0)|O_NONBLOCK) == -1)
{
return -1;
}
return 0;
}
/*
handle_message - 处理每一个 socket 上的消息收发
*/
int handle_message(int new_fd)
{
char buf[MAXBUF + 1];
int len;
/* 開始处理每一个新连接上的数据收发 */
bzero(buf, MAXBUF + 1);
/* 接收client的消息 */
len = recv(new_fd, buf, MAXBUF, 0);
if (len > 0)
{
printf("%d接收消息成功:'%s'。共%d个字节的数据\n",
new_fd, buf, len);
}
else
{
if (len < 0)
printf
("消息接收失败!错误代码是%d。错误信息是'%s'\n",
errno, strerror(errno));
close(new_fd);
return -1;
}
/* 处理每一个新连接上的数据收发结束 */
return len;
} int main()
{
int listener, new_fd, kdpfd, nfds, n, ret, curfds;
socklen_t len;
struct sockaddr_in my_addr, their_addr;
unsigned int myport, lisnum;
struct epoll_event ev;
struct epoll_event events[MAXEPOLLSIZE];
struct rlimit rt;
myport = 6000;
lisnum = 2;
/* 设置每一个进程同意打开的最大文件数 */
rt.rlim_max = rt.rlim_cur = MAXEPOLLSIZE;
if (setrlimit(RLIMIT_NOFILE, &rt) == -1)
{
perror("setrlimit");
exit(1);
}
else
{
printf("设置系统资源參数成功! \n");
}
/* 开启 socket 监听 */
if ((listener = socket(PF_INET, SOCK_STREAM, 0)) == -1)
{
perror("socket");
exit(1);
}
else
{
printf("socket 创建成功!\n");
}
setnonblocking(listener);
bzero(&my_addr, sizeof(my_addr));
my_addr.sin_family = PF_INET;
my_addr.sin_port = htons(myport);
my_addr.sin_addr.s_addr = INADDR_ANY;
if (bind(listener, (struct sockaddr *) &my_addr, sizeof(struct sockaddr)) == -1)
{
perror("bind");
exit(1);
}
else
{
printf("IP 地址和端口绑定成功\n");
}
if (listen(listener, lisnum) == -1)
{
perror("listen");
exit(1);
}
else
{
printf("开启服务成功! \n");
}
/* 创建 epoll 句柄,把监听 socket 增加到 epoll 集合里 */
kdpfd = epoll_create(MAXEPOLLSIZE);
len = sizeof(struct sockaddr_in);
ev.events = EPOLLIN | EPOLLET;
ev.data.fd = listener;
if (epoll_ctl(kdpfd, EPOLL_CTL_ADD, listener, &ev) < 0)
{
fprintf(stderr, "epoll set insertion error: fd=%d\n", listener);
return -1;
}
else
{
printf("监听 socket 增加 epoll 成功!\n");
}
curfds = 1;
while (1)
{
/* 等待有事件发生 */
nfds = epoll_wait(kdpfd, events, curfds, -1);
if (nfds == -1)
{
perror("epoll_wait");
break;
}
/* 处理全部事件 */
for (n = 0; n < nfds; ++n)
{
if (events[n].data.fd == listener)
{
new_fd = accept(listener, (struct sockaddr *) &their_addr,&len);
if (new_fd < 0)
{
perror("accept");
continue;
}
else
{
printf("有连接来自于: %s:%d, 分配的 socket 为:%d\n",
inet_ntoa(their_addr.sin_addr), ntohs(their_addr.sin_port), new_fd);
}
setnonblocking(new_fd);
ev.events = EPOLLIN | EPOLLET;
ev.data.fd = new_fd;
if (epoll_ctl(kdpfd, EPOLL_CTL_ADD, new_fd, &ev) < 0)
{
fprintf(stderr, "把 socket '%d' 增加 epoll 失败。%s\n",
new_fd, strerror(errno));
return -1;
}
curfds++;
}
else
{
ret = handle_message(events[n].data.fd);
if (ret < 1 && errno != 11)
{
epoll_ctl(kdpfd, EPOLL_CTL_DEL, events[n].data.fd,&ev);
curfds--;
}
}
}
}
close(listener);
return 0;
}
client代码
// echo_client.cpp
// g++ -o echo_client -O3 echo_client.cpp -lboost_system -lboost_thread
#include <boost/asio.hpp>
namespace asio = boost::asio;
using asio::ip::tcp;
#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <stdio.h>
#include<sys/time.h>
#include<unistd.h> FILE* g_hLog = 0;
class session
: public boost::enable_shared_from_this<session>
{
public:
session(asio::io_service& io)
: socket_(io)
{ } tcp::socket& socket()
{ return socket_; } void start(int nindex)
{
m_nIndex = nindex;
snprintf(output_buffer_, sizeof(output_buffer_), "this is %d client.", m_nIndex);
//fprintf(g_hLog, "%s\n", output_buffer_);
asio::async_write(socket_, asio::buffer(output_buffer_, 12), boost::bind(&session::handle_write, shared_from_this(), _1, _2));
} void handle_write(const boost::system::error_code& ec, std::size_t bytes_transfered)
{
fprintf(g_hLog, "%d write %s\n", m_nIndex, ec.message().data());
if(!ec)
{
//asio::async_read(socket_, asio::buffer(input_buffer_, 12), boost::bind(&session::handle_read, shared_from_this(), _1, _2));
}
else {
std::cerr << "write error:" << ec.message() << std:: endl;
}
} void handle_read(const boost::system::error_code& ec, std::size_t bytes_transfered)
{
if(ec)
{
std::cerr << "read error:" << ec.message() << std::endl;
}
} private:
tcp::socket socket_;
int m_nIndex;
char output_buffer_[50];
char input_buffer_[50];
}; void handle_connect(boost::shared_ptr<session> session_ptr, int nindex, const boost::system::error_code& ec)
{
if(ec)
{
fprintf(g_hLog, "%d connect error: %s\n", nindex, ec.message().data());
std::cerr << "connect error:" << ec.message() << std::endl;
} else {
session_ptr->start(nindex);
}
} int main(/*int argc, char* argv[]*/)
{
g_hLog = fopen("log.txt", "wr");
if(!g_hLog)
return 0; struct timeval time1, time2;
gettimeofday(&time1, 0); asio::io_service io;
tcp::resolver resolver(io);
tcp::resolver::iterator endpoint = resolver.resolve(tcp::resolver::query("localhost", "6000"));
boost::shared_ptr<session> session_ptr;
for(int i = 0; i < 10000; i++)
{
session_ptr.reset(new session(io));
asio::async_connect(session_ptr->socket(), endpoint, boost::bind(handle_connect, session_ptr, i, _1));
}
io.run(); gettimeofday(&time2, 0);
std::cout << "has time:" << double((time2.tv_sec-time1.tv_sec)+(time2.tv_usec-time1.tv_usec)/1000000.0) << std::endl;
fprintf(g_hLog, "has time: %f s", double((time2.tv_sec-time1.tv_sec)+(time2.tv_usec-time1.tv_usec)/1000000.0)); fclose(g_hLog);
}
Linux默认最大的文件句柄数是1024,须要改动此值才干正常測试。打开终端运行例如以下命令
echo ulimit -n 65535 >> /etc/profile
注销账户并又一次登录能够。
版权声明:本文博主原创文章。博客,未经同意不得转载。
epoll演示样本的更多相关文章
- 模式识别 - 处理多个演示样本研究(MIL)特点(matlab)
处理多个演示样本研究(MIL)特点(matlab) 本文地址: http://blog.csdn.net/caroline_wendy/article/details/27206325 多演示样例学习 ...
- Cocos2d-x 3.2 Lua演示样本CocosDenshionTest(音频测试)
Cocos2d-x 3.2 Lua演示样本CocosDenshionTest(音频测试) 本篇博客介绍Cocos2d-x 3.2中Lua演示样例的音频測试.Cocos2d-x使用SimpleAudi ...
- java注意事项演示 地图产生表 演示样本 来自thinking in java 4 20代码的章
java注意事项演示 地图产生表 演示样本 来自thinking in java 4 20代码的章 thinking in java 4免费下载:http://download.csdn.net/d ...
- Cocos2d-x 3.2 Lua演示样本 ActionTest(操作测试)
Cocos2d-x 3.2 Lua演示样本 ActionTest(操作测试) 2014年博文大赛,请投上您宝贵的一票:http://vote.blog.csdn.net/Article/Details ...
- Asp.net固定功能位充满了零(解决,演示样本)!
Asp.net固定功能位充满了零(解决.演示样本)! 在发展,其中,数实现出现8数字.但是,需要使用0加厚.例如:123,代表:0000123. 真实的比例,如下: decimal aaa = 123 ...
- WebGL自学教程——WebGL演示样本:开始
最终开始WebGL样品演示,...... 开始 使用WebGL步骤,非常easy: 1. 获得WebGL的渲染环境(也叫渲染上下文). 2. 发挥你的想象力,利用<WebGL參考手冊>中的 ...
- MVC模式编程演示样本-登录认证(静态)
好,部分博客分享我的总结JSP-Servlet-JavaBean思想认识和三层编程模型的基本流程,ZH- CNMVC该示例实现演示的编程模式-登录身份验证过程,在这里,我仍在使用静态验证usernam ...
- Java8的日期和时间的库20经常使用的演示样本
除了lambda表达,stream以及从一些小的改进,Java 8还推出了新的日期和时间API,在本教程中,我们将展示通过几个简单的任务来学习如何使用示例Java 8这组API.Java至今.日历和时 ...
- HTML 5 在Web SQL 使用演示样本
Web sql 这是一个模拟数据库浏览器.可以使用JS操作SQL完成数据读取和写入,但是这件事情并不多,现在支持的浏览器,而其W3C规格已经停止支持.外形似它的前景不是很亮. W3C 规范:http: ...
随机推荐
- JVM指令集(指令码、助记符、功能描述)(转)
JVM指令集(指令码.助记符.功能描述) 指令码 助记符 功能描述 0x00 nop 无操作 0x01 aconst_null 指令格式: aconst_null 功能描述: null进栈. 指令 ...
- SecureCRT学习之道:SecureCRT 常用技巧
快捷键: 1. ctrl + a : 移动光标到行首 2. ctrl + e :移动光标到行尾 3. ctrl + d :删除光标之后的一个字符 4. ctrl + w : 删除行首到当前光标所在位 ...
- 上市ASCII 表省内发现!
表格来自,这里 扩展码表:
- Linux/Unix使用valgrind内存泄漏检测
c\c++程序设计.内存管理是一个比较头疼的问题.相信它会导致内存泄漏.除了外部养成良好的编程习惯(使用智能指针),使用该工具还可以帮助检测内存泄漏,valgrind这是Unix\Linux在一个很好 ...
- Gitblit配置
Gitblit的安装配置及访问-windows (2013-09-11 11:52:31) 转载▼ 分类: android基础 Git 是现在很流行的分布式版本控制工具,github更是人人皆知. ...
- ios发电子邮件
ios发电子邮件 by 吴雪莹 第一: NSString *myEmail = @"3423423423@qq.com"; NSString *toemail = @"a ...
- java.nio分析软件包(三)---Charset理解力
前面的分析后,2一个基本的封装类型.现在我们就来揭开Java.nio魔法知识的最后一块,CharsetEncoding类,他的主要功能是实现字节Unicode之间的转换转码. 让我们来看看他同样的封装 ...
- SharePoint 2010 BCS - 概要
博客地址 http://blog.csdn.net/foxdave SharePoint 2010首次引入了BCS的概念 - Business Connectivity Service.即业务连接服务 ...
- DB2 “The transaction log for the database is full” 存在的问题及解决方案
DB2在执行一个大的insert/update操作的时候报"The transaction log for the database is full.. "错误,查了一下文档是DB ...
- 【我的书】Unity Shader的书 — 文件夹(2015.12.21更新)
写在前面 感谢全部点进来看的朋友.没错.我眼下打算写一本关于Unity Shader的书. 出书的目的有以下几个: 总结我接触Unity Shader以来的历程,给其它人一个借鉴.我非常明确学Shad ...