收藏 分销(赏)

1Boost之TCP-Client-and-Server.docx

上传人:二*** 文档编号:4771442 上传时间:2024-10-12 格式:DOCX 页数:2 大小:35KB 下载积分:5 金币
下载 相关 举报
1Boost之TCP-Client-and-Server.docx_第1页
第1页 / 共2页
本文档共2页,全文阅读请下载到手机保存,查看更方便
资源描述
客户端代码 #include <boost/asio.hpp> #include <iostream> #include <stdlib.h> using namespace std; using namespace boost::asio; void main() { io_service iosev; ip::tcp::socket mysorket(iosev); ip::tcp::endpoint ep(ip::address_v4::from_string("127.0.0.1"), 1100); boost::system::error_code ec; mysorket.connect(ep, ec);//链接 while (1) { char str[1024] = { 0 }; cout << "请输入"; cin >> str; cout << endl; mysorket.write_some(buffer(str), ec); memset(str, 0, 1024);//清空字符串 mysorket.read_some(buffer(str), ec); cout << "收到" << str << endl; } cin.get(); } 客户端代码: 服务器端代码: #include <boost/asio.hpp> #include <iostream> #include <stdlib.h> using namespace std; using namespace boost::asio; void main() { io_service iosev; //这里使用ip v4协议,端口号是1100 ip::tcp::acceptor myacceptor(iosev, ip::tcp::endpoint(ip::tcp::v4(), 1100)); while (1) { //构建TCP ip::tcp::socket mysocket(iosev); //接受套接字 myacceptor.accept(mysocket); cout << "客户端" << mysocket.remote_endpoint().address() << ":" << mysocket.remote_endpoint().port() << "链接上" << endl; char recestr[1024] = { 0 }; boost::system::error_code ec; //处理网络异常 int length = mysocket.read_some(buffer(recestr), ec); cout << "收到" << recestr << "长度" << length << endl; system(recestr); length = mysocket.write_some(buffer(recestr, length), ec); cout << "发送报文长度" << length << endl; } cin.get(); } 运行结果:
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 环境建筑 > 其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服