码迷,mamicode.com
首页 > 其他好文 > 详细

boost::asio 之udp协议的使用

时间:2014-09-18 00:53:13      阅读:274      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   os   使用   ar   strong   2014   

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\n",nAdd, buffer); 
  26.     } 
  27.     return 0; 

see the  gif

bubuko.com,布布扣

boost::asio 之udp协议的使用

标签:style   blog   http   io   os   使用   ar   strong   2014   

原文地址:http://blog.csdn.net/laoyi_grace/article/details/39352203

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!