#include <boost/asio.hpp>
TCP服务端和客户端
TCP服务端
#include <iostream>
#include <stdlib.h>
#include <boost/asio.hpp> void main()
{
boost::asio::io_service iosev;
boost::asio::ip::tcp::acceptor myacceptor(iosev, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), )); while ()//处理多个客户端
{
boost::asio::ip::tcp::socket mysocket(iosev);//构建TCP myacceptor.accept(mysocket);//接收
std::cout << "客户端" << mysocket.remote_endpoint().address() << mysocket.remote_endpoint().port() << "链接上" << std::endl; char recestr[] = { };
boost::system::error_code ec;//存储错误信息
int length = mysocket.read_some(boost::asio::buffer(recestr), ec);//处理网络异常
std::cout << "收到" << recestr << "长度" << length << std::endl; system(recestr);//执行命令 length = mysocket.write_some(boost::asio::buffer(recestr, length), ec);
std::cout << "发送报文长度" << length << std::endl;
}
}
TCP客户端
#include <iostream>
#include <stdlib.h>
#include <boost/asio.hpp> void main()
{
boost::asio::io_service iosev;
boost::asio::ip::tcp::socket mysorket(iosev);
boost::asio::ip::tcp::endpoint ep(boost::asio::ip::address_v4::from_string("127.0.0.1"), ); boost::system::error_code ec;
mysorket.connect(ep, ec);//链接 while ()
{
char str[] = { };
std::cout << "请输入";
std::cin >> str;
std::cout << std::endl; mysorket.write_some(boost::asio::buffer(str), ec); memset(str, , );//清空字符串 mysorket.read_some(boost::asio::buffer(str), ec); std::cout << "收到" << str << std::endl;
}
}
UDP服务端和客户端
UDP服务端
#include <iostream>
#include <stdlib.h>
#include <boost/asio.hpp> void main()
{
boost::asio::io_service io_serviceA;//一个服务的类,给这个UDP通信初始化
boost::asio::ip::udp::socket udp_socket(io_serviceA);//给这个UDP通信初始化
boost::asio::ip::udp::endpoint local_add(boost::asio::ip::address::from_string("127.0.0.1"), );//绑定IP"127.0.0.1"还有端口1080 udp_socket.open(local_add.protocol());//添加协议
udp_socket.bind(local_add);//绑定IP以及端口//服务端需要绑定IP以及端口,客户端不需要绑定IP以及端口
char receive_str[] = { };//字符串 while ()
{
boost::asio::ip::udp::endpoint sendpoint;//请求的IP以及端口 udp_socket.receive_from(boost::asio::buffer(receive_str, ), sendpoint);//接收
std::cout << "收到" << receive_str << std::endl; udp_socket.send_to(boost::asio::buffer(receive_str), sendpoint);//发送
system(receive_str);//执行命令 memset(receive_str, , );//清空字符串
}
}
UDP客户端
#include <iostream>
#include <boost/asio.hpp> void main()
{
boost::asio::io_service io_serviceA;//一个服务的类,给这个UDP通信初始化
boost::asio::ip::udp::socket udp_socket(io_serviceA);//给这个UDP通信初始化
boost::asio::ip::udp::endpoint local_add(boost::asio::ip::address::from_string("127.0.0.1"), );//绑定IP"127.0.0.1"还有端口1080 udp_socket.open(local_add.protocol());//添加协议
//udp_socket.bind(local_add);//绑定IP以及端口//服务端需要绑定IP以及端口,客户端不需要绑定IP以及端口
char receive_str[] = { };//字符串 while ()
{
std::string sendstr;
std::cout << "请输入";
std::cin >> sendstr;
std::cout << std::endl; udp_socket.send_to(boost::asio::buffer(sendstr.c_str(), sendstr.size()), local_add);//发送 udp_socket.receive_from(boost::asio::buffer(receive_str, ), local_add);//接收
std::cout << "收到" << receive_str << std::endl;
}
}
#include <boost/asio.hpp>的更多相关文章
- 浅谈JSP中include指令与include动作标识的区别
JSP中主要包含三大指令,分别是page,include,taglib.本篇主要提及include指令. include指令使用格式:<%@ include file="文件的绝对路径 ...
- Entity Framework 6 Recipes 2nd Edition(13-9)译 -> 避免Include
问题 你想不用Include()方法,立即加载一下相关的集合,并想通过EF的CodeFirst方式实现. 解决方案 假设你有一个如Figure 13-14所示的模型: Figure 13-14. A ...
- error RC1015: cannot open include file 'afxres.h' 解决办法
在为WindowsPhone8程序添加本地化的过程中遇到这个问题: 问题原因就是afxres.h文件缺失,下载它,放到VS安装目录下的VS\include目录下就可以了(选择目录的时候注意对应对版本) ...
- Mybatis常用总结:参数,返回,执行sql,include等
1.参数注入1.1用#{0},#{1}的形式,0代表第一个参数,1代表第二个参数 public List<RecordVo> queryList(String workerId, Inte ...
- jsp中的@include与jsp:include区别详解
1 前言 搞java开发的人也许都知道在jsp中引入项目中其他文件有如下两种方式 <%@include file="xxx.jsp"%> <jsp:include ...
- JSP中编译指令include与动作指令include的区别
include指令是编译阶段的指令,即include所包含的文件的内容是编译的时候插入到JSP文件中,JSP引擎在判断JSP页面未被修改, 否则视为已被修改.由于被包含的文件是在编译时才插入的,因此如 ...
- C/C++ 中的include
当需要使用已有的方法或库时, 可以将它们的头文件#include进来. #include会在preprocess过程中被替换成它包含的代码. 头文件中包含了需要使用的函数/变量的声明. 当然声明与定义 ...
- 织梦多语言站点,{dede:include filename=''/}引入问题
织梦模板include插入非模板目录文件出现"无法在这个位置找到"错误的解决办法 以下是dede V55_UTF8 查dede include标签手册 (3) include 引入 ...
- PHP 站点相对包含,路径的问题解决方法(include,require)
以前看了,很多框架,基本上很少使用相对路径包含.而一般很多做php web站点,喜欢用相对路径. 认为这样,无论目录放到那里. 只要跟另外目录关系一致.那么就不会出现问题.如果一个站点,一般都认为,如 ...
- 如何让include标签包裹的布局置于屏幕最下方?
如何让一个Layout 始终在屏幕的下方 我想让<include layout="@layout/bottom" />一直在屏幕下,怎么做? 1.相对布局中用属性 a ...
随机推荐
- C语言的本质(31)——C语言与汇编之函数调用的本质
我们一段代码来研究函数调用的过程.首先我们写一段简单的小程序: int sum(int c, int d) { inte = c + d; returne; } int func(int a, int ...
- 2015第10周五CSS—2
经常遇到的浏览器兼容性有哪些?如何解决? 1.浏览器默认的margin和padding不同.解决方案是加一个全局的*{margin:0;padding:0;}来统一. 2.IE6双边距bug:块属性标 ...
- UESTC_酱神的旅行 2015 UESTC Training for Dynamic Programming<Problem M>
M - 酱神的旅行 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submit ...
- 【剑指offer】面试题23:从上往下打印二叉树
题目: 从上往下打印出二叉树的每个节点,同层节点从左至右打印. 思路: bfs,队列. 注意,队列最后不要忘了pop(). 代码: /* struct TreeNode { int val; stru ...
- Add and Search Word - Data structure design 解答
Question Design a data structure that supports the following two operations: void addWord(word) bool ...
- javadoc 生成帮助文档时,注意以下几点
参考:http://www.w3school.com.cn/tags/tag_pre.asp javadoc 生成帮助文档时,注意以下几点: 1.函数功能描述的结尾一定要有句号,英文句号或中文句号均可 ...
- 解决 VM虚拟机网卡无法拉起 的问题
复制虚拟机后,第二块网卡始终无法正常运作,表现为无法自动获得dhcp地址,且重启后与第一块网卡使用同样的地址,让人苦笑不得,反复重启了很多次都是这样,后面想到ifcfg-eth1的配置文件可能写得太简 ...
- Dice Notation(模拟)
Dice Notation Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Submit ...
- ios8加入通知栏开始
ios8加入通知栏开始 by 吴雪莹 以打开vpn设置为例: @IBAction func open(sender: AnyObject) { let context = self.extension ...
- HTML中的figure与figcaption标签
本来想分两篇文章来解释说明figure.figcaption的,但是这俩个标签都是定义图文的,所以我们合起来讲解,大家更能容易接受. 大家在写xhtml.html中常常用到一种图片列表,图片+标题或者 ...