write by http://blog.csdn.net/bojie5744 bj_末雨

udp sender

  1. #include "stdafx.h"
  2. #include <string>
  3. #include <boost/asio.hpp>
  4. using namespace std;
  5. using namespace boost::asio;
  6. int _tmain(int argc, _TCHAR* argv[])
  7. {
  8. io_service my_io_service; // ip::udp::endpoint my_local_enpoint(ip::udp::v4(),0);/*another way to create endpoint*/
  9. //   my_udp_socket.open(my_login_server_endpoint.protocol());
  10. //   my_udp_socket.bind(my_local_enpoint);
  11. ip::udp::endpoint local_endpoint(ip::udp::v4(), 7777);//create endpoint,this a local endpoint
  12. ip::udp::endpoint remote_endpoint(ip::address_v4::from_string("127.0.0.1"), 2300);//create a remote endpoint
  13. //don't  fill (ip::udp::v4()) in the first parameter,it will cause that the contents are seny out the failure!
  14. ip::udp::socket socket(my_io_service, local_endpoint);//create socket and bind the endpoint
  15. char *send_data = "hello! my name is Bojie. Can you see me?";/*the contents to be sent*/
  16. try
  17. {
  18. while (1)
  19. {
  20. Sleep(500);
  21. socket.send_to(buffer(send_data, strlen(send_data) + 1/*the size of contents*/), remote_endpoint);
  22. }
  23. }
  24. catch (std::exception& e)//to get the error when sending
  25. {
  26. std::cerr << e.what() << std::endl;
  27. }
  28. return 0;
  29. }
  1. #include "stdafx.h"
  2. #include <string>
  3. #include <boost/asio.hpp>
  4. using namespace std;
  5. using namespace boost::asio;
  6. int _tmain(int argc, _TCHAR* argv[])
  7. {
  8. io_service my_io_service; // ip::udp::endpoint my_local_enpoint(ip::udp::v4(),0);/*another way to create endpoint*/
  9. // my_udp_socket.open(my_login_server_endpoint.protocol());
  10. // my_udp_socket.bind(my_local_enpoint);
  11.  
  12. ip::udp::endpoint local_endpoint(ip::udp::v4(), 7777);//create endpoint,this a local endpoint
  13.  
  14. ip::udp::endpoint remote_endpoint(ip::address_v4::from_string("127.0.0.1"), 2300);//create a remote endpoint
  15. //don't fill (ip::udp::v4()) in the first parameter,it will cause that the contents are seny out the failure!
  16. ip::udp::socket socket(my_io_service, local_endpoint);//create socket and bind the endpoint
  17.  
  18. char *send_data = "hello! my name is Bojie. Can you see me?";/*the contents to be sent*/
  19.  
  20. try
  21. {
  22. while (1)
  23. {
  24. Sleep(500);
  25. socket.send_to(buffer(send_data, strlen(send_data) + 1/*the size of contents*/), remote_endpoint);
  26. }
  27. }
  28. catch (std::exception& e)//to get the error when sending
  29. {
  30. std::cerr << e.what() << std::endl;
  31. }
  32.  
  33. return 0;
  34. }

udp recivcer

  1. #include "stdafx.h"
  2. #include <string>
  3. #include <boost/asio.hpp>
  4. using namespace std;
  5. using namespace boost::asio;
  6. int _tmain(int argc, _TCHAR* argv[])
  7. {
  8. io_service my_io_service;
  9. ip::udp::endpoint local_endpoint(ip::address_v4::from_string("127.0.0.1"), 2300);//create  a local endpoint
  10. ip::udp::endpoint romote_endpoint;
    //this enpoint is used to store the endponit from remote-computer
  11. ip::udp::socket socket(my_io_service, local_endpoint);//create socket and bind the endpoint
  12. char buffer[40000];
  13. int nAdd = 0;
  14. while (1)
  15. {
  16. memset(buffer, 0, 40000);//to initialize variables
  17. nAdd++;
  18. socket.receive_from(boost::asio::buffer(buffer, 40000), romote_endpoint);//receive data from  remote-computer
  19. printf("recv %d datapacket:%s\n",nAdd, buffer);
  20. }
  21. return 0;
  22. }
  1. #include "stdafx.h"
  2. #include <string>
  3. #include <boost/asio.hpp>
  4. using namespace std;
  5. using namespace boost::asio;
  6. int _tmain(int argc, _TCHAR* argv[])
  7. {
  8.  
  9. io_service my_io_service;
  10.  
  11. ip::udp::endpoint local_endpoint(ip::address_v4::from_string("127.0.0.1"), 2300);//create a local endpoint
  12.  
  13. ip::udp::endpoint romote_endpoint; //this enpoint is used to store the endponit from remote-computer
  14.  
  15. ip::udp::socket socket(my_io_service, local_endpoint);//create socket and bind the endpoint
  16.  
  17. char buffer[40000];
  18.  
  19. int nAdd = 0;
  20.  
  21. while (1)
  22. {
  23. memset(buffer, 0, 40000);//to initialize variables
  24. nAdd++;
  25. socket.receive_from(boost::asio::buffer(buffer, 40000), romote_endpoint);//receive data from remote-computer
  26. printf("recv %d datapacket:%s\n",nAdd, buffer);
  27. }
  28. return 0;
  29. }

see the  gif

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvYm9qaWU1NzQ0/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center">

boost::asio 之udp协议的使用的更多相关文章

  1. boost::asio译文

        Christopher Kohlhoff Copyright © 2003-2012 Christopher M. Kohlhoff 以Boost1.0的软件授权进行发布(见附带的LICENS ...

  2. #include <boost/asio.hpp>

    TCP服务端和客户端 TCP服务端 #include <iostream> #include <stdlib.h> #include <boost/asio.hpp> ...

  3. 使用Boost.Asio编写通信程序

    摘要:本文通过形像而活泼的语言简单地介绍了Boost::asio库的使用,作为asio的一个入门介绍是非常合适的,可以给人一种新鲜的感觉,同时也能让体验到asio的主要内容. Boost.Asio是一 ...

  4. boost.asio包装类st_asio_wrapper开发教程(2014.5.23更新)(一)-----转

    一:什么是st_asio_wrapper它是一个c/s网络编程框架,基于对boost.asio的包装(最低在boost-1.49.0上调试过),目的是快速的构建一个c/s系统: 二:st_asio_w ...

  5. Boost.Asio技术文档

    Christopher Kohlhoff Copyright © 2003-2012 Christopher M. Kohlhoff 以Boost1.0的软件授权进行发布(见附带的LICENSE_1_ ...

  6. boost.asio包装类st_asio_wrapper开发教程(一)

    一:什么是st_asio_wrapper它是一个c/s网络编程框架,基于对boost.asio的包装(最低在boost-1.49.0上调试过),目的是快速的构建一个c/s系统: 二:st_asio_w ...

  7. Boost.Asio 网络编程([译]Boost.Asio基本原理)

    转自:https://m.w3cschool.cn/nlzbw/nlzbw-3vs825ya.html Boost.Asio基本原理 这一章涵盖了使用Boost.Asio时必须知道的一些事情.我们也将 ...

  8. Boost.Asio基本原理(CSDN也有Markdown了,好开森)

    Boost.Asio基本原理 这一章涵盖了使用Boost.Asio时必须知道的一些事情.我们也将深入研究比同步编程更复杂.更有乐趣的异步编程. 网络API 这一部分包含了当使用Boost.Asio编写 ...

  9. BOOST.Asio——Tutorial

    =================================版权声明================================= 版权声明:原创文章 谢绝转载  啥说的,鄙视那些无视版权随 ...

随机推荐

  1. PHP获取客户端请求头信息

    获取HTTP请求头信息 Apache 如果web服务器用的是apache,可以直接用php的库函数getallheaders() Nginx 如果web服务器用的是nginx,则无法直接使用getal ...

  2. quartz定时任务,已过期的内容自动下线

    概念: Quartz是一个开源的作业调度框架,可以让计划的程序任务一个预定义的日期和时间运行.Quartz可以用来创建简单或复杂的日程安排执行几十,几百,甚至是十万的作业数. 框架架构: 简单实例: ...

  3. 深度学习应用系列(四)| 使用 TFLite Android构建自己的图像识别App

    深度学习要想落地实践,一个少不了的路径即是朝着智能终端.嵌入式设备等方向发展.但终端设备没有GPU服务器那样的强大性能,那如何使得终端设备应用上深度学习呢? 所幸谷歌已经推出了TFMobile,去年又 ...

  4. django 编码错误

    估计这个问题是2.7的问题3.0好像就统一utf编码了 报错代码: python :ascii codec can't decode byte 0xe8 in posi 当django中报这个错误的时 ...

  5. vim自动补全插件YouCompleteMe的安装及配置

    原文地址: http://blog.csdn.net/shixuehancheng/article/details/46289811

  6. 【二分答案】【DFS】【分类讨论】Gym - 100851F - Froggy Ford

    题意:河里有n块石头,一只青蛙要从左岸跳到右岸,你可以再在任意一个位置放一块石头,使得在最优方案下,青蛙单步跳的距离的最大值最小化,输出该位置. 将原图视作完全图,二分答案mid,然后在图中只保留小于 ...

  7. 【最小点覆盖】POJ3041-Asteroids

    [题目大意] 在n*n的网格上有n个点,每次删除一行或者一列,问至少要删除几次才能删除完全部的这些店? [思路] 在国庆最后一天到来前,把二分图的三个基本情况[最小点覆盖][DAG图的最小路径覆盖]和 ...

  8. 【可持久化并查集】BZOJ3673-可持久化并查集 by zky

    颓了十多天别问我再干嘛,在补学校作业 啊,开学了……我的夏天…… [题目大意] n个集合 m个操作 操作: 1 a b 合并a,b所在集合 2 k 回到第k次操作之后的状态(查询算作操作) 3 a b ...

  9. 1 Spring4 之环境搭建和HelloWorld

    1 Spring 是什么? 具体描述 Spring: 轻量级:Spring 是非侵入性的- 基于 Spring 开发的应用中的对象可以不依赖于 Spring 的 API 依赖注入(DI --- dep ...

  10. React-Native调用支付宝,微信

    https://www.pingxx.com/docs/downloads Ping++ 是为移动端应用以及 PC 网页量身打造的下一代支付系统,通过一个 SDK 便可以同时支持移动端以及 PC 端网 ...