boost::asio 之udp协议的使用
来源:互联网 发布:手机处理照片软件 编辑:程序博客网 时间:2024/05/22 07:41
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;
- }
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;
- }
see the gif
0 0
- boost::asio 之udp协议的使用
- boost::asio 之udp协议的使用
- boost::asio使用UDP协议通信源码实现
- UDP boost::asio的实现
- UDP boost::asio的实现
- UDP boost::asio的实现
- Boost.Asio的使用技巧
- Boost.Asio的使用技巧
- Boost.Asio的使用技巧
- Boost.Asio的使用技巧
- Boost.Asio的使用技巧
- Boost.Asio的使用技巧
- Boost-Asio的使用技巧
- Boost.Asio的使用技巧
- Boost.Asio的使用技巧
- boost::asio学习之[四]udp client server 异步server
- boost::asio学习之[五]运行udp tcp服务
- boost::asio编程-同步UDP
- ORACLE利器之OSWatch监控操作系统性能和RAC心跳
- 理解git常用命令原理
- 用VS2012 VS2013编写的程序无法再xp运行的解决方法
- 安装程序无法创建新的系统分区,也无法定位现有系统分区
- Unity3D中Random.seed属性解析
- boost::asio 之udp协议的使用
- Android消息推送机制
- 汇编中常见的一些错误信息
- 黑马程序员--Java 7K面试题之交通灯管理系统
- 赡假残压泊俦崭残胀仁辉素
- LDAP入门
- ACM HEU OJ 1019 Ferry Loading II || POJ 2336
- GPU方法做倒排压缩和交集计算
- BaseServlet思想(Struts2的思想)