boost::asio 之udp协议的使用

时间:2023-03-10 02:06:55
boost::asio 之udp协议的使用

write by http://blog.****.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. 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;
  29. }
#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

  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. ip::udp::endpoint local_endpoint(ip::address_v4::from_string("127.0.0.1"), 2300);//create  a local endpoint
  10. ip::udp::endpoint romote_endpoint;
    //this enpoint is used to store the endponit from remote-computer
  11. ip::udp::socket socket(my_io_service, local_endpoint);//create socket and bind the endpoint
  12. char buffer[40000];
  13. int nAdd = 0;
  14. while (1)
  15. {
  16. memset(buffer, 0, 40000);//to initialize variables
  17. nAdd++;
  18. socket.receive_from(boost::asio::buffer(buffer, 40000), romote_endpoint);//receive data from  remote-computer
  19. printf("recv %d datapacket:%s\n",nAdd, buffer);
  20. }
  21. return 0;
  22. }
#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

boost::asio 之udp协议的使用

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvYm9qaWU1NzQ0/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center">