• 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


  • 相关阅读:
    linux下shell显示-bash-4.1#不显示路径解决方法
    update chnroute
    An error "Host key verification failed" when you connect to other computer by OSX SSH
    使用dig查询dns解析
    DNS被污染后
    TunnelBroker for EdgeRouter 后记
    mdadm详细使用手册
    关于尼康黄的原因
    Panda3d code in github
    Python实例浅谈之三Python与C/C++相互调用
  • 原文地址:https://www.cnblogs.com/gccbuaa/p/6721735.html
Copyright © 2020-2023  润新知