boost::asio 之udp协议的使用
write by http://blog.csdn.net/bojie5744 bj_末雨
udp sender
- #include "stdafx.h"
- #include <string>
- #include <boost/asio.hpp>
- using namespace std;
- using namespace boost::asio;
- int _tmain(int argc, _TCHAR* argv[])
- {
- io_service my_io_service; // ip::udp::endpoint my_local_enpoint(ip::udp::v4(),0);/*another way to create endpoint*/
- // my_udp_socket.open(my_login_server_endpoint.protocol());
- // my_udp_socket.bind(my_local_enpoint);
- ip::udp::endpoint local_endpoint(ip::udp::v4(), 7777);//create endpoint,this a local endpoint
- ip::udp::endpoint remote_endpoint(ip::address_v4::from_string("127.0.0.1"), 2300);//create a remote endpoint
- //don't fill (ip::udp::v4()) in the first parameter,it will cause that the contents are seny out the failure!
- ip::udp::socket socket(my_io_service, local_endpoint);//create socket and bind the endpoint
- char *send_data = "hello! my name is Bojie. Can you see me?";/*the contents to be sent*/
- try
- {
- while (1)
- {
- Sleep(500);
- socket.send_to(buffer(send_data, strlen(send_data) + 1/*the size of contents*/), remote_endpoint);
- }
- }
- catch (std::exception& e)//to get the error when sending
- {
- std::cerr << e.what() << std::endl;
- }
- return 0;
- }
- #include "stdafx.h"
- #include <string>
- #include <boost/asio.hpp>
- using namespace std;
- using namespace boost::asio;
- int _tmain(int argc, _TCHAR* argv[])
- {
- io_service my_io_service; // ip::udp::endpoint my_local_enpoint(ip::udp::v4(),0);/*another way to create endpoint*/
- // my_udp_socket.open(my_login_server_endpoint.protocol());
- // my_udp_socket.bind(my_local_enpoint);
- ip::udp::endpoint local_endpoint(ip::udp::v4(), 7777);//create endpoint,this a local endpoint
- ip::udp::endpoint remote_endpoint(ip::address_v4::from_string("127.0.0.1"), 2300);//create a remote endpoint
- //don't fill (ip::udp::v4()) in the first parameter,it will cause that the contents are seny out the failure!
- ip::udp::socket socket(my_io_service, local_endpoint);//create socket and bind the endpoint
- char *send_data = "hello! my name is Bojie. Can you see me?";/*the contents to be sent*/
- try
- {
- while (1)
- {
- Sleep(500);
- socket.send_to(buffer(send_data, strlen(send_data) + 1/*the size of contents*/), remote_endpoint);
- }
- }
- catch (std::exception& e)//to get the error when sending
- {
- std::cerr << e.what() << std::endl;
- }
- return 0;
- }
udp recivcer
- #include "stdafx.h"
- #include <string>
- #include <boost/asio.hpp>
- using namespace std;
- using namespace boost::asio;
- int _tmain(int argc, _TCHAR* argv[])
- {
- io_service my_io_service;
- ip::udp::endpoint local_endpoint(ip::address_v4::from_string("127.0.0.1"), 2300);//create a local endpoint
- ip::udp::endpoint romote_endpoint;
//this enpoint is used to store the endponit from remote-computer - ip::udp::socket socket(my_io_service, local_endpoint);//create socket and bind the endpoint
- char buffer[40000];
- int nAdd = 0;
- while (1)
- {
- memset(buffer, 0, 40000);//to initialize variables
- nAdd++;
- socket.receive_from(boost::asio::buffer(buffer, 40000), romote_endpoint);//receive data from remote-computer
- printf("recv %d datapacket:%s\n",nAdd, buffer);
- }
- return 0;
- }
- #include "stdafx.h"
- #include <string>
- #include <boost/asio.hpp>
- using namespace std;
- using namespace boost::asio;
- int _tmain(int argc, _TCHAR* argv[])
- {
- io_service my_io_service;
- ip::udp::endpoint local_endpoint(ip::address_v4::from_string("127.0.0.1"), 2300);//create a local endpoint
- ip::udp::endpoint romote_endpoint; //this enpoint is used to store the endponit from remote-computer
- ip::udp::socket socket(my_io_service, local_endpoint);//create socket and bind the endpoint
- char buffer[40000];
- int nAdd = 0;
- while (1)
- {
- memset(buffer, 0, 40000);//to initialize variables
- nAdd++;
- socket.receive_from(boost::asio::buffer(buffer, 40000), romote_endpoint);//receive data from remote-computer
- printf("recv %d datapacket:%s\n",nAdd, buffer);
- }
- return 0;
- }
see the gif
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvYm9qaWU1NzQ0/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center">
boost::asio 之udp协议的使用的更多相关文章
- boost::asio译文
Christopher Kohlhoff Copyright © 2003-2012 Christopher M. Kohlhoff 以Boost1.0的软件授权进行发布(见附带的LICENS ...
- #include <boost/asio.hpp>
TCP服务端和客户端 TCP服务端 #include <iostream> #include <stdlib.h> #include <boost/asio.hpp> ...
- 使用Boost.Asio编写通信程序
摘要:本文通过形像而活泼的语言简单地介绍了Boost::asio库的使用,作为asio的一个入门介绍是非常合适的,可以给人一种新鲜的感觉,同时也能让体验到asio的主要内容. Boost.Asio是一 ...
- boost.asio包装类st_asio_wrapper开发教程(2014.5.23更新)(一)-----转
一:什么是st_asio_wrapper它是一个c/s网络编程框架,基于对boost.asio的包装(最低在boost-1.49.0上调试过),目的是快速的构建一个c/s系统: 二:st_asio_w ...
- Boost.Asio技术文档
Christopher Kohlhoff Copyright © 2003-2012 Christopher M. Kohlhoff 以Boost1.0的软件授权进行发布(见附带的LICENSE_1_ ...
- boost.asio包装类st_asio_wrapper开发教程(一)
一:什么是st_asio_wrapper它是一个c/s网络编程框架,基于对boost.asio的包装(最低在boost-1.49.0上调试过),目的是快速的构建一个c/s系统: 二:st_asio_w ...
- Boost.Asio 网络编程([译]Boost.Asio基本原理)
转自:https://m.w3cschool.cn/nlzbw/nlzbw-3vs825ya.html Boost.Asio基本原理 这一章涵盖了使用Boost.Asio时必须知道的一些事情.我们也将 ...
- Boost.Asio基本原理(CSDN也有Markdown了,好开森)
Boost.Asio基本原理 这一章涵盖了使用Boost.Asio时必须知道的一些事情.我们也将深入研究比同步编程更复杂.更有乐趣的异步编程. 网络API 这一部分包含了当使用Boost.Asio编写 ...
- BOOST.Asio——Tutorial
=================================版权声明================================= 版权声明:原创文章 谢绝转载 啥说的,鄙视那些无视版权随 ...
随机推荐
- PHP获取客户端请求头信息
获取HTTP请求头信息 Apache 如果web服务器用的是apache,可以直接用php的库函数getallheaders() Nginx 如果web服务器用的是nginx,则无法直接使用getal ...
- quartz定时任务,已过期的内容自动下线
概念: Quartz是一个开源的作业调度框架,可以让计划的程序任务一个预定义的日期和时间运行.Quartz可以用来创建简单或复杂的日程安排执行几十,几百,甚至是十万的作业数. 框架架构: 简单实例: ...
- 深度学习应用系列(四)| 使用 TFLite Android构建自己的图像识别App
深度学习要想落地实践,一个少不了的路径即是朝着智能终端.嵌入式设备等方向发展.但终端设备没有GPU服务器那样的强大性能,那如何使得终端设备应用上深度学习呢? 所幸谷歌已经推出了TFMobile,去年又 ...
- django 编码错误
估计这个问题是2.7的问题3.0好像就统一utf编码了 报错代码: python :ascii codec can't decode byte 0xe8 in posi 当django中报这个错误的时 ...
- vim自动补全插件YouCompleteMe的安装及配置
原文地址: http://blog.csdn.net/shixuehancheng/article/details/46289811
- 【二分答案】【DFS】【分类讨论】Gym - 100851F - Froggy Ford
题意:河里有n块石头,一只青蛙要从左岸跳到右岸,你可以再在任意一个位置放一块石头,使得在最优方案下,青蛙单步跳的距离的最大值最小化,输出该位置. 将原图视作完全图,二分答案mid,然后在图中只保留小于 ...
- 【最小点覆盖】POJ3041-Asteroids
[题目大意] 在n*n的网格上有n个点,每次删除一行或者一列,问至少要删除几次才能删除完全部的这些店? [思路] 在国庆最后一天到来前,把二分图的三个基本情况[最小点覆盖][DAG图的最小路径覆盖]和 ...
- 【可持久化并查集】BZOJ3673-可持久化并查集 by zky
颓了十多天别问我再干嘛,在补学校作业 啊,开学了……我的夏天…… [题目大意] n个集合 m个操作 操作: 1 a b 合并a,b所在集合 2 k 回到第k次操作之后的状态(查询算作操作) 3 a b ...
- 1 Spring4 之环境搭建和HelloWorld
1 Spring 是什么? 具体描述 Spring: 轻量级:Spring 是非侵入性的- 基于 Spring 开发的应用中的对象可以不依赖于 Spring 的 API 依赖注入(DI --- dep ...
- React-Native调用支付宝,微信
https://www.pingxx.com/docs/downloads Ping++ 是为移动端应用以及 PC 网页量身打造的下一代支付系统,通过一个 SDK 便可以同时支持移动端以及 PC 端网 ...