tutorial.qbk

来自「Boost provides free peer-reviewed portab」· QBK 代码 · 共 2,067 行 · 第 1/5 页

QBK
2,067
字号
  ``''''''``}Return to [link boost_asio.tutorial.tutdaytime4 Daytime.4 - A synchronous UDP daytime client][endsect][endsect][section:tutdaytime5 Daytime.5 - A synchronous UDP daytime server]This tutorial program shows how to use asio to implement a server application with UDP.  ``''''''``int main()  ``''''''``{  ``''''''``  try  ``''''''``  {  ``''''''``    boost::asio::io_service io_service;Create an [link boost_asio.reference.ip__udp.socket ip::udp::socket] object to receive requests on UDP port 13.  ``''''''``    udp::socket socket(io_service, udp::endpoint(udp::v4(), 13));Wait for a client to initiate contact with us. The remote\_endpoint object will be populated by [link boost_asio.reference.basic_datagram_socket.receive_from ip::udp::socket::receive_from()].  ``''''''``    for (;;)  ``''''''``    {  ``''''''``      boost::array<char, 1> recv_buf;  ``''''''``      udp::endpoint remote_endpoint;  ``''''''``      boost::system::error_code error;  ``''''''``      socket.receive_from(boost::asio::buffer(recv_buf),  ``''''''``          remote_endpoint, 0, error);  ``''''''``      if (error && error != boost::asio::error::message_size)  ``''''''``        throw boost::system::system_error(error);Determine what we are going to send back to the client.  ``''''''``      std::string message = make_daytime_string();Send the response to the remote\_endpoint.  ``''''''``      boost::system::error_code ignored_error;  ``''''''``      socket.send_to(boost::asio::buffer(message),  ``''''''``          remote_endpoint, 0, ignored_error);  ``''''''``    }  ``''''''``  }Finally, handle any exceptions.  ``''''''``  catch (std::exception& e)  ``''''''``  {  ``''''''``    std::cerr << e.what() << std::endl;  ``''''''``  }  ``''''''``  return 0;  ``''''''``}See the [link boost_asio.tutorial.tutdaytime5.src full source listing]Return to the [link boost_asio.tutorial tutorial index]Previous: [link boost_asio.tutorial.tutdaytime4 Daytime.4 - A synchronous UDP daytime client]Next: [link boost_asio.tutorial.tutdaytime6 Daytime.6 - An asynchronous UDP daytime server][section:src Source listing for Daytime.5]  ``''''''``//  ``''''''``// server.cpp  ``''''''``// ~~~~~~~~~~  ``''''''``//  ``''''''``// Copyright (c) 2003-2008 Christopher M. Kohlhoff (chris at kohlhoff dot com)  ``''''''``//  ``''''''``// Distributed under the Boost Software License, Version 1.0. (See accompanying  ``''''''``// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)  ``''''''``//  ``''''''``#include <ctime>  ``''''''``#include <iostream>  ``''''''``#include <string>  ``''''''``#include <boost/array.hpp>  ``''''''``#include <boost/asio.hpp>  ``''''''``using boost::asio::ip::udp;  ``''''''``std::string make_daytime_string()  ``''''''``{  ``''''''``  using namespace std; // For time_t, time and ctime;  ``''''''``  time_t now = time(0);  ``''''''``  return ctime(&now);  ``''''''``}  ``''''''``int main()  ``''''''``{  ``''''''``  try  ``''''''``  {  ``''''''``    boost::asio::io_service io_service;  ``''''''``    udp::socket socket(io_service, udp::endpoint(udp::v4(), 13));  ``''''''``    for (;;)  ``''''''``    {  ``''''''``      boost::array<char, 1> recv_buf;  ``''''''``      udp::endpoint remote_endpoint;  ``''''''``      boost::system::error_code error;  ``''''''``      socket.receive_from(boost::asio::buffer(recv_buf),  ``''''''``          remote_endpoint, 0, error);  ``''''''``      if (error && error != boost::asio::error::message_size)  ``''''''``        throw boost::system::system_error(error);  ``''''''``      std::string message = make_daytime_string();  ``''''''``      boost::system::error_code ignored_error;  ``''''''``      socket.send_to(boost::asio::buffer(message),  ``''''''``          remote_endpoint, 0, ignored_error);  ``''''''``    }  ``''''''``  }  ``''''''``  catch (std::exception& e)  ``''''''``  {  ``''''''``    std::cerr << e.what() << std::endl;  ``''''''``  }  ``''''''``  return 0;  ``''''''``}Return to [link boost_asio.tutorial.tutdaytime5 Daytime.5 - A synchronous UDP daytime server][endsect][endsect][section:tutdaytime6 Daytime.6 - An asynchronous UDP daytime server][heading The main() function]  ``''''''``int main()  ``''''''``{  ``''''''``  try  ``''''''``  {Create a server object to accept incoming client requests, and run the [link boost_asio.reference.io_service io_service] object.  ``''''''``    boost::asio::io_service io_service;  ``''''''``    udp_server server(io_service);  ``''''''``    io_service.run();  ``''''''``  }  ``''''''``  catch (std::exception& e)  ``''''''``  {  ``''''''``    std::cerr << e.what() << std::endl;  ``''''''``  }  ``''''''``  return 0;  ``''''''``}[heading The udp_server class]  ``''''''``class udp_server  ``''''''``{  ``''''''``public:The constructor initialises a socket to listen on UDP port 13.  ``''''''``  udp_server(boost::asio::io_service& io_service)  ``''''''``    : socket_(io_service, udp::endpoint(udp::v4(), 13))  ``''''''``  {  ``''''''``    start_receive();  ``''''''``  }  ``''''''``private:  ``''''''``  void start_receive()  ``''''''``  {The function [link boost_asio.reference.basic_datagram_socket.async_receive_from ip::udp::socket::async_receive_from()] will cause the application to listen in the background for a new request. When such a request is received, the [link boost_asio.reference.io_service io_service] object will invoke the `handle_receive()` function with two arguments: a value of type boost::system::error\_code indicating whether the operation succeeded or failed, and a `size_t` value `bytes_transferred` specifying the number of bytes received.  ``''''''``    socket_.async_receive_from(  ``''''''``        boost::asio::buffer(recv_buffer_), remote_endpoint_,  ``''''''``        boost::bind(&udp_server::handle_receive, this,  ``''''''``          boost::asio::placeholders::error,  ``''''''``          boost::asio::placeholders::bytes_transferred));  ``''''''``  }The function `handle_receive()` will service the client request.  ``''''''``  void handle_receive(const boost::system::error_code& error,  ``''''''``      std::size_t /*bytes_transferred*/)  ``''''''``  {The `error` parameter contains the result of the asynchronous operation. Since we only provide the 1-byte `recv_buffer_` to contain the client's request, the [link boost_asio.reference.io_service io_service] object would return an error if the client sent anything larger. We can ignore such an error if it comes up.  ``''''''``    if (!error || error == boost::asio::error::message_size)  ``''''''``    {Determine what we are going to send.  ``''''''``      boost::shared_ptr<std::string> message(  ``''''''``          new std::string(make_daytime_string()));We now call [link boost_asio.reference.basic_datagram_socket.async_send_to ip::udp::socket::async_send_to()] to serve the data to the client.  ``''''''``      socket_.async_send_to(boost::asio::buffer(*message), remote_endpoint_,  ``''''''``          boost::bind(&udp_server::handle_send, this, message,  ``''''''``            boost::asio::placeholders::error,  ``''''''``            boost::asio::placeholders::bytes_transferred));When initiating the asynchronous operation, and if using boost::bind(), you must specify only the arguments that match the handler's parameter list. In this program, both of the argument placeholders (boost::asio::placeholders::error and boost::asio::placeholders::bytes\_transferred) could potentially have been removed.Start listening for the next client request.  ``''''''``      start_receive();Any further actions for this client request are now the responsibility of `handle_send()`.  ``''''''``    }  ``''''''``  }The function `handle_send()` is invoked after the service request has been completed.  ``''''''``  void handle_send(boost::shared_ptr<std::string> /*message*/,  ``''''''``      const boost::system::error_code& /*error*/,  ``''''''``      std::size_t /*bytes_transferred*/)  ``''''''``  {  ``''''''``  }  ``''''''``  udp::socket socket_;  ``''''''``  udp::endpoint remote_endpoint_;  ``''''''``  boost::array<char, 1> recv_buffer_;  ``''''''``};See the [link boost_asio.tutorial.tutdaytime6.src full source listing]Return to the [link boost_asio.tutorial tutorial index]Previous: [link boost_asio.tutorial.tutdaytime5 Daytime.5 - A synchronous UDP daytime server]Next: [link boost_asio.tutorial.tutdaytime7 Daytime.7 - A combined TCP/UDP asynchronous server][section:src Source listing for Daytime.6]  ``''''''``//  ``''''''``// server.cpp  ``''''''``// ~~~~~~~~~~  ``''''''``//  ``''''''``// Copyright (c) 2003-2008 Christopher M. Kohlhoff (chris at kohlhoff dot com)  ``''''''``//  ``''''''``// Distributed under the Boost Software License, Version 1.0. (See accompanying  ``''''''``// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)  ``''''''``//  ``''''''``#include <ctime>  ``''''''``#include <iostream>  ``''''''``#include <string>  ``''''''``#include <boost/array.hpp>  ``''''''``#include <boost/bind.hpp>  ``''''''``#include <boost/shared_ptr.hpp>  ``''''''``#include <boost/asio.hpp>  ``''''''``using boost::asio::ip::udp;  ``''''''``std::string make_daytime_string()  ``''''''``{  ``''''''``  using namespace std; // For time_t, time and ctime;  ``''''''``  time_t now = time(0);  ``''''''``  return ctime(&now);  ``''''''``}  ``''''''``class udp_server  ``''''''``{  ``''''''``public:  ``''''''``  udp_server(boost::asio::io_service& io_service)  ``''''''``    : socket_(io_service, udp::endpoint(udp::v4(), 13))  ``''''''``  {  ``''''''``    start_receive();  ``''''''``  }  ``''''''``private:  ``''''''``  void start_receive()  ``''''''``  {  ``''''''``    socket_.async_receive_from(  ``''''''``        boost::asio::buffer(recv_buffer_), remote_endpoint_,  ``''''''``        boost::bind(&udp_server::handle_receive, this,  ``''''''``          boost::asio::placeholders::error,  ``''''''``          boost::asio::placeholders::bytes_transferred));  ``''''''``  }  ``''''''``  void handle_receive(const boost::system::error_code& error,  ``''''''``      std::size_t /*bytes_transferred*/)  ``''''''``  {  ``''''''``    if (!error || error == boost::asio::error::message_size)  ``''''''``    {  ``''''''``      boost::shared_ptr<std::string> message(  ``''''''``          new std::string(make_daytime_string()));  ``''''''``      socket_.async_send_to(boost::asio::buffer(*message), remote_endpoint_,  ``''''''``          boost::bind(&udp_server::handle_send, this, message,  ``''''''``            boost::asio::placeholders::error,  ``''''''``            boost::asio::placeholders::bytes_transferred));  ``''''''``      start_receive();  ``''''''``    }  ``''''''``  }  ``''''''``  void handle_send(boost::shared_ptr<std::string> /*message*/,  ``''''''``      const boost::system::error_code& /*error*/,  ``''''''``      std::size_t /*bytes_transferred*/)  ``''''''``  {  ``''''''``  }  ``''''''``  udp::socket socket_;  ``''''''``  udp::endpoint remote_endpoint_;  ``''''''``  boost::array<char, 1> recv_buffer_;  ``''''''``};  ``''''''``int main()  ``''''''``{  ``''''''``  try  ``''''''``  {  ``''''''``    boost::asio::io_service io_service;  ``''''''``    udp_server server(io_service);  ``''''''``    io_service.run();  ``''''''``  }  ``''''''``  catch (std::exception& e)  ``''''''``  {  ``''''''``    std::cerr << e.what() << std::endl;  ``''''''``  }  ``''''''``  return 0;  ``''''''``}Return to [link boost_asio.tutorial.tutdaytime6 Daytime.6 - An asynchronous UDP daytime server][endsect][endsect][section:tutdaytime7 Daytime.7 - A combined TCP/UDP asynchronous server]This tutorial program shows how to combine the two asynchronous servers that we have just written, into a single server application.[head

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?