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. Django+Nginx+uwsgi搭建自己的博客(三)

    (本来打算在这篇博文中介绍Users App的前端部分的,但写着写着就发现还需要铺垫很多东西才能把整个项目串的比较流畅些,因此这篇就继续介绍了后端的一些东西,前端的部分只好跳票到下一篇了-) 在上一篇 ...

  2. 【SpringBoot】关闭HttpClient无用日志

    环境: SpringBoot pom依赖了apache.commons.HttpClient: <!--httpclient--> <dependency> <group ...

  3. SpringBoot学习(五)

    package org.springboot.sample.config; import org.slf4j.Logger; import org.slf4j.LoggerFactory; impor ...

  4. POJ 3162 Walking Race 树形dp 优先队列

    http://poj.org/problem?id=3162 题意 :  一棵n个节点的树.wc爱跑步,跑n天,第i天从第i个节点开始跑步,每次跑到距第i个节点最远的那个节点(产生了n个距离),现在要 ...

  5. SEL和IMP

    http://www.jianshu.com/p/4a09d5ebdc2c SEL : 类成员方法的指针,但不同于C语言中的函数指针,函数指针直接保存了方法的地址,但SEL只是方法编号. IMP:一个 ...

  6. [转载]C++内存管理

    [导语] 内存管理是C++最令人切齿痛恨的问题,也是C++最有争议的问题,C++高手从中获得了更好的性能,更大的自由,C++菜鸟的收获则是一遍一遍的检查代码和对C++的痛恨,但内存管理在C++中无处不 ...

  7. Problem B: 深入浅出学算法003-计算复杂度

    Description 算法复杂度一般分为:时间复杂度.空间复杂度.编程复杂度. 这三个复杂度本身是矛盾体,不能一味地追求降低某一复杂度,否则会带来其他复杂度的增加.在权衡各方面的情况下,降低时间复杂 ...

  8. 【洛谷】1972:[SDOI2009]HH的项链【莫队+树状数组】

    P1972 [SDOI2009]HH的项链 题目背景 无 题目描述 HH 有一串由各种漂亮的贝壳组成的项链.HH 相信不同的贝壳会带来好运,所以每次散步完后,他都会随意取出一段贝壳,思考它们所表达的含 ...

  9. SQL Server 事务复制爬坑记

    SQL Server 复制功能折腾了好几天了,现特将其配置过程以及其间遇到的问题记录下来,以备日后查阅.同时,也让“同道”同学们少走不必要的弯路.如果有不对之处,欢迎大家指正,欢迎沟通交流. 一.复制 ...

  10. DML、DDL、DCL是什么?

    一.DML DML(data manipulation language)数据操纵语言: 我们经常会用到的 INSERT.DELETE.UPDATE.SELECT语句. 主要用来对数据库的数据进行一些 ...