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. 使用JDBC连接数据库的一些BUG

    题记:前几天用JDBC连接MYSQL数据库的时候,出现了一些BUG,有代码层次的,也有设置层次的, 下面的解决方法时我目前所遇到的,后期如果还有遇到的会进行补充. 一.出现:远程mysql_java. ...

  2. 【Python】单例模式Singleton

    前两天一个面试被问到python中单例模式有几种实现方式,只答出了可以用元类实现...然后就想不起来了. 之后翻书,原来这些之前都见过的啊.... 1.手动实现真正创建实例的方法__new__()来实 ...

  3. 51nod1624 取余最长路 前缀和 + set

    由于只有3行,因此只会会换行2次,假设$x, y$分别为这两次的换行点 那么答案为$S[1][x] +S[2][y] - S[2][x - 1] + S[3][n] - S[3][y - 1]$ 其中 ...

  4. HDU 6141 I am your Father!(最小树形图)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=6141 [题目大意] 给出一个有向图,求1点为根的最小树形图使得第n个点的直接父亲编号最小 [题解] ...

  5. [BZOJ1005](HNOI 2008)明明的烦恼

    Description 自从明明学了树的结构,就对奇怪的树产生了兴趣...... 给出标号为1到N的点,以及某些点最终的度数,允许在任意两点间连线,可产生多少棵度数满足要求的树? Input 第一行为 ...

  6. JS 循环遍历json

    第一  看看json的格式 { "employees": [ { "firstName":"Bill" , "lastName&q ...

  7. drbd + pacemaker

    Centos6.5 X86 64 2014-04-27 # yum -y install corosync pacemaker # -.el6.x86_64.rpm pssh--.el6.x86_64 ...

  8. IntelliJ IDEA导出设置

    导出: [File]->[Export Settings] 导入: [File]->[Import Settings]

  9. java类中属性的加载顺序,以及内存分配情况介绍

    看下面例子及说明: /** 假如有外部类调用了该类,代码为:new StaticTest(); 那么下面是类属性的加载顺序 */ public class StaticTest{ public int ...

  10. 移动端 关于 键盘将input 框 顶上去的解决思路---个人见解

    在移动端,经常会遇到input获得焦点时候弹出的虚拟键盘将整体页面布局打乱的情况. 比如说是这种 输入框未获得焦点键盘未抬起的时候: 输入框获得焦点键盘抬起的时候 这种情况下,不管是上面的textar ...