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
		=================================版权声明================================= 版权声明:原创文章 谢绝转载 啥说的,鄙视那些无视版权随 ... 
随机推荐
- Django+Nginx+uwsgi搭建自己的博客(三)
			(本来打算在这篇博文中介绍Users App的前端部分的,但写着写着就发现还需要铺垫很多东西才能把整个项目串的比较流畅些,因此这篇就继续介绍了后端的一些东西,前端的部分只好跳票到下一篇了-) 在上一篇 ... 
- 【SpringBoot】关闭HttpClient无用日志
			环境: SpringBoot pom依赖了apache.commons.HttpClient: <!--httpclient--> <dependency> <group ... 
- SpringBoot学习(五)
			package org.springboot.sample.config; import org.slf4j.Logger; import org.slf4j.LoggerFactory; impor ... 
- POJ 3162 Walking Race 树形dp 优先队列
			http://poj.org/problem?id=3162 题意 : 一棵n个节点的树.wc爱跑步,跑n天,第i天从第i个节点开始跑步,每次跑到距第i个节点最远的那个节点(产生了n个距离),现在要 ... 
- SEL和IMP
			http://www.jianshu.com/p/4a09d5ebdc2c SEL : 类成员方法的指针,但不同于C语言中的函数指针,函数指针直接保存了方法的地址,但SEL只是方法编号. IMP:一个 ... 
- [转载]C++内存管理
			[导语] 内存管理是C++最令人切齿痛恨的问题,也是C++最有争议的问题,C++高手从中获得了更好的性能,更大的自由,C++菜鸟的收获则是一遍一遍的检查代码和对C++的痛恨,但内存管理在C++中无处不 ... 
- Problem B: 深入浅出学算法003-计算复杂度
			Description 算法复杂度一般分为:时间复杂度.空间复杂度.编程复杂度. 这三个复杂度本身是矛盾体,不能一味地追求降低某一复杂度,否则会带来其他复杂度的增加.在权衡各方面的情况下,降低时间复杂 ... 
- 【洛谷】1972:[SDOI2009]HH的项链【莫队+树状数组】
			P1972 [SDOI2009]HH的项链 题目背景 无 题目描述 HH 有一串由各种漂亮的贝壳组成的项链.HH 相信不同的贝壳会带来好运,所以每次散步完后,他都会随意取出一段贝壳,思考它们所表达的含 ... 
- SQL Server 事务复制爬坑记
			SQL Server 复制功能折腾了好几天了,现特将其配置过程以及其间遇到的问题记录下来,以备日后查阅.同时,也让“同道”同学们少走不必要的弯路.如果有不对之处,欢迎大家指正,欢迎沟通交流. 一.复制 ... 
- DML、DDL、DCL是什么?
			一.DML DML(data manipulation language)数据操纵语言: 我们经常会用到的 INSERT.DELETE.UPDATE.SELECT语句. 主要用来对数据库的数据进行一些 ... 
