1. 程式人生 > >boost::asio 之udp協議的使用

boost::asio 之udp協議的使用

udp sender

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