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
- Boost_028.cpp:Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include <ctime>
- #include <boost/asio/ip/tcp.hpp>
- #include <boost/asio.hpp>
- #include <boost/bind.hpp>
- #include <boost/enable_shared_from_this.hpp>
- #include <boost/shared_ptr.hpp>
- #include <boost/array.hpp>
- #include <iostream>
- #include <string>
- Using the UDP namespace
- Using BOOST::ASIO::IP::UDP;
- Converts the current time to a string.
- std::string make_daytime_string ()
- {
- using namespace std; //In order to use time_t, time and CTime;
- time_t now = time (0);
- return CTime (&now);
- }
- //
- Creates a time server for asynchronous UDP.
- Software developer: Cai Junsheng 2013-08-25
- qq:9073204
- //
- Class Udptimeserver
- {
- Public
- ///Incoming IO service, then create a socket,ipv4 version of UDP with a port number of
- Udptimeserver (boost::asio::io_service& ioservice)
- : M_sockudp (Ioservice, Udp::endpoint (Udp::v4 (), 13))
- {
- //Enter the receiving service.
- Recvtime ();
- }
- Private
- //Receive client requests.
- void Recvtime (void)
- {
- //Receive data asynchronously
- M_sockudp.async_receive_from (
- Boost::asio::buffer (M_RECVBUF), M_endpointremote,
- Boost::bind (&udptimeserver::handlerecvtime, this ,
- Boost::asio::p Laceholders::error,
- Boost::asio::p laceholders::bytes_transferred));
- }
- //When receiving client data, enter the function response processing
- void Handlerecvtime (const boost::system::error_code& error,
- std::size_t /*bytes_transferred*/)
- {
- //If there is no error, the time string is sent to the client.
- if (!error | | error = = boost::asio::error::message_size)
- {
- Boost::shared_ptr<std::string> strmessage (
- New Std::string (Make_daytime_string ()));
- M_sockudp.async_send_to (Boost::asio::buffer (*strmessage), M_endpointremote,
- Boost::bind (&udptimeserver::handlesendtime, this , strmessage,
- Boost::asio::p Laceholders::error,
- Boost::asio::p laceholders::bytes_transferred));
- //Receive the next message.
- Recvtime ();
- }
- }
- //When sending a time string to the client after a successful response.
- void Handlesendtime (boost::shared_ptr<std::string> /*strmessage*/,
- Const boost::system::error_code& /*error*/,
- std::size_t /*bytes_transferred*/)
- {
- }
- Private
- Udp::socket m_sockudp; //server socket.
- Udp::endpoint M_endpointremote; //endpoint information when data is received.
- boost::array<Char, 1> m_recvbuf; //Receive data buffers.
- };
- void testudp (void)
- {
- Boost::asio::io_service Ioservice;
- Udptimeserver Udptimeserver (Ioservice);
- Ioservice.run ();
- }
- int _tmain (int argc, _tchar* argv[])
- {
- //
- TESTUDP ();
- return 0;
- }
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