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. LLinux系统编程(10)——进程间通信之管道

    管道是Linux中很重要的一种通信方式,是把一个程序的输出直接连接到另一个程序的输入,常说的管道多是指无名管道,无名管道只能用于具有亲缘关系的进程之间,这是它与有名管道的最大区别.有名管道叫named ...

  2. 哈希表原理及hashmap简单实现

    哈希表也叫做散列表.在各种语言中都有hashmap的实现.其最突出的优点是查找和插入以及删除具有常数的时间复杂度 我们可以把哈希表理解为数组+链表 数组具有常数复杂度的查找,为什么呢,因为数组是在内存 ...

  3. DOTween 模仿NGUI Tween

    最近再做一个小的项目,只要使用的是DOTween 动画. 但是感到DOTween 在某些方面不如 NGUI的Twenn 比较好用 所以就模仿了一下 在此,请各位大神指点一下 public class ...

  4. linux查看用户登录信息2-who命令

    who命令与w命令相似,但要比w命令显示更加详细的信息.[root@rusky opt]# man who WHO(1) User Commands WHO(1) NAME who - show wh ...

  5. linux系统如何限制其他用户登录

    用root 用户登录到系统排查问题,这个时候不希望别的账户登录到系统,你如何处理? vim /etc/nologin 将账号添加到此文件中去,你会发现其他用户都无法登录,报错信息如下:即连接即中断.

  6. mysql操作SQL语句

    二.数据库操作SQL语句1.显示服务器上当前存在什么数据库SHOW DATABASES; 2.创建名称为rewin的数据库CREATE DATABASE rewin; 3.删除名称为rewin的数据库 ...

  7. X - A == B ?(第二季水)

    Description Give you two numbers A and B, if A is equal to B, you should print "YES", or p ...

  8. [Spring Boot Reference Guide] 读书笔记一 Getting Started

    8. Introducing Spring Boot Goals of spring boot: Provide a radically faster and widely accessible ge ...

  9. Emgu学习笔记(一)安装及运行Sample

    1.简单说明 Emgu是Dot Net平台对OpenCV的封装,本质上没有增加新功能,是通过Dot Net的平台调用技术直接调用OpenCV C++语言写的库,使用我们可以方便用.net平台通过Ope ...

  10. Spring:启动项目时加载数据库数据(总结)

    在项目中需要启动程序时,要将数据库的用户信息表加载到内存中,找到一下几种方式. 1.实现ApplicationListener接口,重写onApplicationEvent方法,可以在项目启动的时候执 ...