• boost::asio 之udp协议的使用


    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.  
    12.     ip::udp::endpoint local_endpoint(ip::udp::v4(), 7777);//create endpoint,this a local endpoint 
    13.  
    14.     ip::udp::endpoint remote_endpoint(ip::address_v4::from_string("127.0.0.1"), 2300);//create a remote endpoint 
    15.     //don't  fill (ip::udp::v4()) in the first parameter,it will cause that the contents are seny out the failure! 
    16.     ip::udp::socket socket(my_io_service, local_endpoint);//create socket and bind the endpoint 
    17.  
    18.     char *send_data = "hello! my name is Bojie. Can you see me?";/*the contents to be sent*/ 
    19.  
    20.     try 
    21.     { 
    22.         while (1) 
    23.         { 
    24.             Sleep(500); 
    25.             socket.send_to(buffer(send_data, strlen(send_data) + 1/*the size of contents*/), remote_endpoint); 
    26.         } 
    27.     } 
    28.     catch (std::exception& e)//to get the error when sending 
    29.     { 
    30.         std::cerr << e.what() << std::endl; 
    31.     } 
    32.  
    33.     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.  
    10.     ip::udp::endpoint local_endpoint(ip::address_v4::from_string("127.0.0.1"), 2300);//create  a local endpoint 
    11.  
    12.     ip::udp::endpoint romote_endpoint; //this enpoint is used to store the endponit from remote-computer 
    13.  
    14.     ip::udp::socket socket(my_io_service, local_endpoint);//create socket and bind the endpoint 
    15.  
    16.     char buffer[40000]; 
    17.      
    18.     int nAdd = 0; 
    19.  
    20.     while (1) 
    21.     {    
    22.         memset(buffer, 0, 40000);//to initialize variables 
    23.         nAdd++; 
    24.         socket.receive_from(boost::asio::buffer(buffer, 40000), romote_endpoint);//receive data from  remote-computer 
    25.         printf("recv %d datapacket:%s ",nAdd, buffer); 
    26.     } 
    27.     return 0; 

    see the  gif


  • 相关阅读:
    Systemd 指令
    2018年书单
    2017年书单
    Centos7 Devstack [Rocky] 重启后无法联网
    kvm虚拟机操作相关命令及虚拟机和镜像密码修改
    负载均衡原理-转
    用配置文件里面的参数值替换yaml模板中的变量值【python】
    linux工具之sar
    利用系统缓存优化程序的运行效率
    Elasticsearch单机部署
  • 原文地址:https://www.cnblogs.com/yjbjingcha/p/7356267.html
Copyright © 2020-2023  润新知