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. }
#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

  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. }
#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协议的使用的更多相关文章

  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. Python网络模块Paramiko基本使用

    一.Paramiko简介 首先来看谁创造了paramiko,是一个名叫Jeff Forcier创建了paramiko项目.项目主页:http://www.paramiko.org,可以去看上面有很多相 ...

  2. scrapy抓取拉勾网职位信息(六)——反爬应对(随机UA,随机代理)

    上篇已经对数据进行了清洗,本篇对反爬虫做一些应对措施,主要包括随机UserAgent.随机代理. 一.随机UA 分析:构建随机UA可以采用以下两种方法 我们可以选择很多UserAgent,形成一个列表 ...

  3. 对java前后端分离的理解

    到目前为止,身为一个java后端开发人员的我, 在工作期间,无非就是ui设计页面,前端开发html,之后将做好的页面交给我,我负责后台逻辑一件html的页面渲染. 好好滴一个后台开发人员,莫名其妙的做 ...

  4. 【BZOJ 1449】 1449: [JSOI2009]球队收益 (最小费用流)

    1449: [JSOI2009]球队收益 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 841  Solved: 483 Description Inpu ...

  5. NOIP 初赛笔记

    // zj蒟蒻瑟瑟发抖.. // 停课了.要好好努力!——10月8日8:29于机房 1. 1946 年 美国 -> 第一台计算机 2. 真空电子管 -> 晶体管 -> 集成 -> ...

  6. [BZOJ3992][SDOI2015]序列统计(DP+原根+NTT)

    3992: [SDOI2015]序列统计 Time Limit: 30 Sec  Memory Limit: 128 MBSubmit: 1888  Solved: 898[Submit][Statu ...

  7. bozj 1449/2895: 球队预算 -- 费用流

    2895: 球队预算 Time Limit: 10 Sec  Memory Limit: 256 MB Description 在一个篮球联赛里,有n支球队,球队的支出是和他们的胜负场次有关系的,具体 ...

  8. 1089 Intervals(中文版)

    开始前先讲几句废话:这个题我开始也没看懂,后来借助百度翻译,明白了大概是什么意思. 试题描述 输入一个n,然后输入n组数据,每个数据有两个数,代表这个闭区间是从几到几.然后看,如果任意两个闭区间有相重 ...

  9. 转:IntelliJ IDEA 2016.1.3注册破解激活

    IntelliJ IDEA 2016.1.3下载地址 https://download.jetbrains.8686c.com/idea/ideaIU-2016.1.3.exe 用注册码激活: 激活码 ...

  10. VS2012项目中使用CocoStudio相关文件的设置

    开发环境说明: win7  vs2012  coco2d-x 3.0 alpha1 cocos2d-x 3.0 alpha 1搭配CocoStudio使用,效果更佳.CocoStudio包含了游戏开发 ...