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>的更多相关文章

  1. 浅谈JSP中include指令与include动作标识的区别

    JSP中主要包含三大指令,分别是page,include,taglib.本篇主要提及include指令. include指令使用格式:<%@ include file="文件的绝对路径 ...

  2. Entity Framework 6 Recipes 2nd Edition(13-9)译 -> 避免Include

    问题 你想不用Include()方法,立即加载一下相关的集合,并想通过EF的CodeFirst方式实现. 解决方案 假设你有一个如Figure 13-14所示的模型: Figure 13-14. A ...

  3. error RC1015: cannot open include file 'afxres.h' 解决办法

    在为WindowsPhone8程序添加本地化的过程中遇到这个问题: 问题原因就是afxres.h文件缺失,下载它,放到VS安装目录下的VS\include目录下就可以了(选择目录的时候注意对应对版本) ...

  4. Mybatis常用总结:参数,返回,执行sql,include等

    1.参数注入1.1用#{0},#{1}的形式,0代表第一个参数,1代表第二个参数 public List<RecordVo> queryList(String workerId, Inte ...

  5. jsp中的@include与jsp:include区别详解

    1 前言 搞java开发的人也许都知道在jsp中引入项目中其他文件有如下两种方式 <%@include file="xxx.jsp"%> <jsp:include ...

  6. JSP中编译指令include与动作指令include的区别

    include指令是编译阶段的指令,即include所包含的文件的内容是编译的时候插入到JSP文件中,JSP引擎在判断JSP页面未被修改, 否则视为已被修改.由于被包含的文件是在编译时才插入的,因此如 ...

  7. C/C++ 中的include

    当需要使用已有的方法或库时, 可以将它们的头文件#include进来. #include会在preprocess过程中被替换成它包含的代码. 头文件中包含了需要使用的函数/变量的声明. 当然声明与定义 ...

  8. 织梦多语言站点,{dede:include filename=''/}引入问题

    织梦模板include插入非模板目录文件出现"无法在这个位置找到"错误的解决办法 以下是dede V55_UTF8 查dede include标签手册 (3) include 引入 ...

  9. PHP 站点相对包含,路径的问题解决方法(include,require)

    以前看了,很多框架,基本上很少使用相对路径包含.而一般很多做php web站点,喜欢用相对路径. 认为这样,无论目录放到那里. 只要跟另外目录关系一致.那么就不会出现问题.如果一个站点,一般都认为,如 ...

  10. 如何让include标签包裹的布局置于屏幕最下方?

    如何让一个Layout 始终在屏幕的下方 我想让<include layout="@layout/bottom" />一直在屏幕下,怎么做? 1.相对布局中用属性  a ...

随机推荐

  1. codecomb 2091【路径数量】

    好久没有更新博客了啊……屯了一堆题没发呢 这是丧心病狂的hzwer每日NOI模拟赛第一天的第一题 妈蛋说好的NOIP难度图论算法两题网络流!让我说什么好 唔……codecomb的页面在晚上就会变得很奇 ...

  2. Invert Binary Tree 解答

    Quetion Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 Solution 1 -- R ...

  3. HDU1242 Rescue(BFS+优先队列)

    Rescue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  4. ZOJ3761(并查集+树的遍历)

    Easy billiards Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge Edward think a g ...

  5. Java中的native方法

    博客引用地址:Java中的native方法 今天花了两个小时把一份关于什么是Native Method的英文文章好好了读了一遍,以下是我依据原文的理解. 一. 什么是Native Method 简单地 ...

  6. hdu 5627 Clarke and MST(最大 生成树)

    Problem Description Clarke is a patient with multiple personality disorder. One day he turned into a ...

  7. Unity 移动MM自签名方式

    在使用Unity接移动MM SDK的时候,最后有一个签名.  主要是把计费文件和版权文件放入APK的根目录.  搞了半天才知道前来这么简单..... 软件使用: apk签名工具apktool

  8. 利用Python完成一个小游戏:随机挑选一个单词,并对其进行乱序,玩家要猜出原始单词

    一 Python的概述以及游戏的内容 Python是一种功能强大且易于使用的编程语言,更接近人类语言,以至于人们都说它是“以思考的速度编程”:Python具备现代编程语言所应具备的一切功能:Pytho ...

  9. UVA 12232 - Exclusive-OR(带权并查集)

    UVA 12232 - Exclusive-OR 题目链接 题意:有n个数字.一開始值都不知道,每次给定一个操作,I a v表示确认a值为v,I a b v,表示确认a^b = v,Q k a1 a2 ...

  10. MVCC图示

    磨砺技术珠矶,践行数据之道,追求卓越价值 回到上一级页面:PostgreSQL内部结构与源代码研究索引页    回到顶级页面:PostgreSQL索引页 [作者:高健@博客园 luckyjackgao ...