Boost Library at Work (39) network UDP asynchronous Server nine

Source: Internet
Author: User

The UDP server and client that you created earlier are synchronous, which means that when you receive data, you cannot participate in other things. If in a program with only the interface thread, and do not want to create multi-threading, resulting in increased complexity, in this case, we have another option, is to create an asynchronous UDP server or client, so that both single-threaded simplicity, but also allows customers to manipulate the interface of the fast response characteristics. It is easy to use the Io_service object in the Boost library to achieve asynchrony because the encapsulated interface is simple and straightforward. The specific code is as follows:

[CPP]View Plaincopy
  1. Boost_028.cpp:Defines the entry point for the console application.
  2. //
  3. #include "stdafx.h"
  4. #include <ctime>
  5. #include <boost/asio/ip/tcp.hpp>
  6. #include <boost/asio.hpp>
  7. #include <boost/bind.hpp>
  8. #include <boost/enable_shared_from_this.hpp>
  9. #include <boost/shared_ptr.hpp>
  10. #include <boost/array.hpp>
  11. #include <iostream>
  12. #include <string>
  13. Using the UDP namespace
  14. Using BOOST::ASIO::IP::UDP;
  15. Converts the current time to a string.
  16. std::string make_daytime_string ()
  17. {
  18. using namespace std;  //In order to use time_t, time and CTime;
  19. time_t now = time (0);
  20. return CTime (&now);
  21. }
  22. //
  23. Creates a time server for asynchronous UDP.
  24. Software developer: Cai Junsheng 2013-08-25
  25. qq:9073204
  26. //
  27. Class Udptimeserver
  28. {
  29. Public
  30. ///Incoming IO service, then create a socket,ipv4 version of UDP with a port number of
  31. Udptimeserver (boost::asio::io_service& ioservice)
  32. : M_sockudp (Ioservice, Udp::endpoint (Udp::v4 (), 13))
  33. {
  34. //Enter the receiving service.
  35. Recvtime ();
  36. }
  37. Private
  38. //Receive client requests.
  39. void Recvtime (void)
  40. {
  41. //Receive data asynchronously
  42. M_sockudp.async_receive_from (
  43. Boost::asio::buffer (M_RECVBUF), M_endpointremote,
  44. Boost::bind (&udptimeserver::handlerecvtime, this ,
  45. Boost::asio::p Laceholders::error,
  46. Boost::asio::p laceholders::bytes_transferred));
  47. }
  48. //When receiving client data, enter the function response processing
  49. void Handlerecvtime (const boost::system::error_code& error,
  50. std::size_t /*bytes_transferred*/)
  51. {
  52. //If there is no error, the time string is sent to the client.
  53. if (!error | | error = = boost::asio::error::message_size)
  54. {
  55. Boost::shared_ptr<std::string> strmessage (
  56. New Std::string (Make_daytime_string ()));
  57. M_sockudp.async_send_to (Boost::asio::buffer (*strmessage), M_endpointremote,
  58. Boost::bind (&udptimeserver::handlesendtime, this , strmessage,
  59. Boost::asio::p Laceholders::error,
  60. Boost::asio::p laceholders::bytes_transferred));
  61. //Receive the next message.
  62. Recvtime ();
  63. }
  64. }
  65. //When sending a time string to the client after a successful response.
  66. void Handlesendtime (boost::shared_ptr<std::string> /*strmessage*/,
  67. Const boost::system::error_code& /*error*/,
  68. std::size_t /*bytes_transferred*/)
  69. {
  70. }
  71. Private
  72. Udp::socket m_sockudp;  //server socket.
  73. Udp::endpoint M_endpointremote;  //endpoint information when data is received.
  74. boost::array<Char, 1> m_recvbuf;  //Receive data buffers.
  75. };
  76. void testudp (void)
  77. {
  78. Boost::asio::io_service Ioservice;
  79. Udptimeserver Udptimeserver (Ioservice);
  80. Ioservice.run ();
  81. }
  82. int _tmain (int argc, _tchar* argv[])
  83. {
  84. //  
  85. TESTUDP ();
  86. return 0;
  87. }

In this example, the main package is a server udptimeserver, it is the use of the Io_service object and the socket object of the asynchronous feature to construct, there is an event response before the corresponding operation, but this is more than the previous synchronization method, or more complex, But it brings the problem of avoiding synchronization between multiple threads.

Boost Library at Work (39) network UDP asynchronous server nine

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.