boost::asio 使用实例
2024-10-10 02:41:41
#include <iostream>
#include <boost/asio.hpp> using namespace std;
using namespace boost::asio; int main()
{
try
{
cout << "server start." << endl;
io_service ios; ip::tcp::acceptor acc(ios,
ip::tcp::endpoint(ip::tcp::v4(),)); cout << acc.local_endpoint().address() << endl; while (true)
{
ip::tcp::socket sock(ios);
acc.accept(sock); cout << "client:" ;
cout << sock.remote_endpoint().address() << endl; sock.write_some(buffer("hello asio"));
}
}
catch (std::exception& e)
{
cout << e.what() << endl;
} return ;
}
tcp client:
#include <boost/asio.hpp>
#include <iostream> using namespace std;
using namespace boost::asio; void client(io_service &ios)
{
try
{
cout << "client start." << endl; ip::tcp::socket sock(ios);
ip::tcp::endpoint ep(ip::address::from_string("127.0.0.1"),); sock.connect(ep); vector<char> str(,);
sock.read_some(buffer(str));
cout << "receive from " << sock.remote_endpoint().address();
cout << &str[] << endl;
}
catch (std::exception& e)
{
cout << e.what() << endl;
}
} void print(const boost::system::error_code&)
{
cout << "test wait..." << endl;
} int main()
{
io_service ios;
deadline_timer at(ios, boost::posix_time::seconds());
at.async_wait(print); cout << "it show before at exired" <<endl;
ios.run();
return ;
}
最新文章
- Celery 框架学习笔记
- 【JavaScript回顾】继承
- js统计字符串中各种字符情况
- javascript正则表达式替换字符串
- spring3.0事务的配置
- Learning Lua Programming (4) Cocos2d-x中Lua编程(一)
- Java就业前景怎么样?学了后好找工作吗?
- GIT入门笔记(6)- 向版本库添加文本文件
- CMake设置FOLDER失败及解决
- 关联管理器(RelatedManager)
- python面向对象的知识梳理
- irc 关键操作
- nginx配置静态webserver
- Xshell 无法连接虚拟机中的ubuntu的问题
- 安装配置Spark集群
- wrk 压测中请求无法响应问题解决过程
- centos中文字符集,中文日志
- JavaScript 删除 ASP.NET 设置的多值 Cookie 的方法
- C#或Python中正则表达式元字符含意
- 战争迷雾Fog Of War