tutorial.qbk
来自「Boost provides free peer-reviewed portab」· QBK 代码 · 共 2,067 行 · 第 1/5 页
QBK
2,067 行
[endsect][section:tutdaytime1 Daytime.1 - A synchronous TCP daytime client]This tutorial program shows how to use asio to implement a client application with TCP.We start by including the necessary header files. ``''''''``#include <iostream> ``''''''``#include <boost/array.hpp> ``''''''``#include <boost/asio.hpp>The purpose of this application is to access a daytime service, so we need the user to specify the server. ``''''''``using boost::asio::ip::tcp; ``''''''``int main(int argc, char* argv[]) ``''''''``{ ``''''''`` try ``''''''`` { ``''''''`` if (argc != 2) ``''''''`` { ``''''''`` std::cerr << "Usage: client <host>" << std::endl; ``''''''`` return 1; ``''''''`` }All programs that use asio need to have at least one [link boost_asio.reference.io_service io_service] object. ``''''''`` boost::asio::io_service io_service;We need to turn the server name that was specified as a parameter to the application, into a TCP endpoint. To do this we use an [link boost_asio.reference.ip__tcp.resolver ip::tcp::resolver] object. ``''''''`` tcp::resolver resolver(io_service);A resolver takes a query object and turns it into a list of endpoints. We construct a query using the name of the server, specified in `argv[1]`, and the name of the service, in this case `"daytime"`. ``''''''`` tcp::resolver::query query(argv[1], "daytime");The list of endpoints is returned using an iterator of type [link boost_asio.reference.ip__basic_resolver.iterator ip::tcp::resolver::iterator]. A default constructed [link boost_asio.reference.ip__basic_resolver.iterator ip::tcp::resolver::iterator] object is used as the end iterator. ``''''''`` tcp::resolver::iterator endpoint_iterator = resolver.resolve(query); ``''''''`` tcp::resolver::iterator end;Now we create and connect the socket. The list of endpoints obtained above may contain both IPv4 and IPv6 endpoints, so we need to try each of them until we find one that works. This keeps the client program independent of a specific IP version. ``''''''`` tcp::socket socket(io_service); ``''''''`` boost::system::error_code error = boost::asio::error::host_not_found; ``''''''`` while (error && endpoint_iterator != end) ``''''''`` { ``''''''`` socket.close(); ``''''''`` socket.connect(*endpoint_iterator++, error); ``''''''`` } ``''''''`` if (error) ``''''''`` throw boost::system::system_error(error);The connection is open. All we need to do now is read the response from the daytime service.We use a `boost::array` to hold the received data. The boost::asio::buffer() function automatically determines the size of the array to help prevent buffer overruns. Instead of a `boost::array`, we could have used a `char []` or `std::vector`. ``''''''`` for (;;) ``''''''`` { ``''''''`` boost::array<char, 128> buf; ``''''''`` boost::system::error_code error; ``''''''`` size_t len = socket.read_some(boost::asio::buffer(buf), error);When the server closes the connection, the [link boost_asio.reference.basic_stream_socket.read_some ip::tcp::socket::read_some()] function will exit with the boost::asio::error::eof error, which is how we know to exit the loop. ``''''''`` if (error == boost::asio::error::eof) ``''''''`` break; // Connection closed cleanly by peer. ``''''''`` else if (error) ``''''''`` throw boost::system::system_error(error); // Some other error. ``''''''`` std::cout.write(buf.data(), len); ``''''''`` }Finally, handle any exceptions that may have been thrown. ``''''''`` } ``''''''`` catch (std::exception& e) ``''''''`` { ``''''''`` std::cerr << e.what() << std::endl; ``''''''`` }See the [link boost_asio.tutorial.tutdaytime1.src full source listing]Return to the [link boost_asio.tutorial tutorial index]Next: [link boost_asio.tutorial.tutdaytime2 Daytime.2 - A synchronous TCP daytime server][section:src Source listing for Daytime.1] ``''''''``// ``''''''``// client.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 <iostream> ``''''''``#include <boost/array.hpp> ``''''''``#include <boost/asio.hpp> ``''''''``using boost::asio::ip::tcp; ``''''''``int main(int argc, char* argv[]) ``''''''``{ ``''''''`` try ``''''''`` { ``''''''`` if (argc != 2) ``''''''`` { ``''''''`` std::cerr << "Usage: client <host>" << std::endl; ``''''''`` return 1; ``''''''`` } ``''''''`` boost::asio::io_service io_service; ``''''''`` tcp::resolver resolver(io_service); ``''''''`` tcp::resolver::query query(argv[1], "daytime"); ``''''''`` tcp::resolver::iterator endpoint_iterator = resolver.resolve(query); ``''''''`` tcp::resolver::iterator end; ``''''''`` tcp::socket socket(io_service); ``''''''`` boost::system::error_code error = boost::asio::error::host_not_found; ``''''''`` while (error && endpoint_iterator != end) ``''''''`` { ``''''''`` socket.close(); ``''''''`` socket.connect(*endpoint_iterator++, error); ``''''''`` } ``''''''`` if (error) ``''''''`` throw boost::system::system_error(error); ``''''''`` for (;;) ``''''''`` { ``''''''`` boost::array<char, 128> buf; ``''''''`` boost::system::error_code error; ``''''''`` size_t len = socket.read_some(boost::asio::buffer(buf), error); ``''''''`` if (error == boost::asio::error::eof) ``''''''`` break; // Connection closed cleanly by peer. ``''''''`` else if (error) ``''''''`` throw boost::system::system_error(error); // Some other error. ``''''''`` std::cout.write(buf.data(), len); ``''''''`` } ``''''''`` } ``''''''`` catch (std::exception& e) ``''''''`` { ``''''''`` std::cerr << e.what() << std::endl; ``''''''`` } ``''''''`` return 0; ``''''''``}Return to [link boost_asio.tutorial.tutdaytime1 Daytime.1 - A synchronous TCP daytime client][endsect][endsect][section:tutdaytime2 Daytime.2 - A synchronous TCP daytime server]This tutorial program shows how to use asio to implement a server application with TCP. ``''''''``#include <ctime> ``''''''``#include <iostream> ``''''''``#include <string> ``''''''``#include <boost/asio.hpp> ``''''''``using boost::asio::ip::tcp;We define the function `make_daytime_string()` to create the string to be sent back to the client. This function will be reused in all of our daytime server applications. ``''''''``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;A [link boost_asio.reference.ip__tcp.acceptor ip::tcp::acceptor] object needs to be created to listen for new connections. It is initialised to listen on TCP port 13, for IP version 4. ``''''''`` tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), 13));This is an iterative server, which means that it will handle one connection at a time. Create a socket that will represent the connection to the client, and then wait for a connection. ``''''''`` for (;;) ``''''''`` { ``''''''`` tcp::socket socket(io_service); ``''''''`` acceptor.accept(socket);A client is accessing our service. Determine the current time and transfer this information to the client. ``''''''`` std::string message = make_daytime_string(); ``''''''`` boost::system::error_code ignored_error; ``''''''`` boost::asio::write(socket, boost::asio::buffer(message), ``''''''`` boost::asio::transfer_all(), ignored_error); ``''''''`` } ``''''''`` }Finally, handle any exceptions. ``''''''`` catch (std::exception& e) ``''''''`` { ``''''''`` std::cerr << e.what() << std::endl; ``''''''`` } ``''''''`` return 0; ``''''''``}See the [link boost_asio.tutorial.tutdaytime2.src full source listing]Return to the [link boost_asio.tutorial tutorial index]Previous: [link boost_asio.tutorial.tutdaytime1 Daytime.1 - A synchronous TCP daytime client]Next: [link boost_asio.tutorial.tutdaytime3 Daytime.3 - An asynchronous TCP daytime server][section:src Source listing for Daytime.2] ``''''''``// ``''''''``// 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/asio.hpp> ``''''''``using boost::asio::ip::tcp; ``''''''``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; ``''''''`` tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), 13)); ``''''''`` for (;;) ``''''''`` { ``''''''`` tcp::socket socket(io_service); ``''''''`` acceptor.accept(socket); ``''''''`` std::string message = make_daytime_string(); ``''''''`` boost::system::error_code ignored_error; ``''''''`` boost::asio::write(socket, boost::asio::buffer(message), ``''''''`` boost::asio::transfer_all(), ignored_error); ``''''''`` } ``''''''`` } ``''''''`` catch (std::exception& e) ``''''''`` { ``''''''`` std::cerr << e.what() << std::endl; ``''''''`` } ``''''''`` return 0; ``''''''``}Return to [link boost_asio.tutorial.tutdaytime2 Daytime.2 - A synchronous TCP daytime server][endsect][endsect][section:tutdaytime3 Daytime.3 - An asynchronous TCP daytime server][heading The main() function] ``''''''``int main() ``''''''``{ ``''''''`` try ``''''''`` {We need to create a server object to accept incoming client connections. The [link boost_asio.reference.io_service io_service] object provides I/O services, such as sockets, that the server object will use. ``''''''`` boost::asio::io_service io_service; ``''''''`` tcp_server server(io_service);Run the [link boost_asio.reference.io_service io_service] object so that it will perform asynchronous operations on your behalf. ``''''''`` io_service.run(); ``''''''`` } ``''''''`` catch (std::exception& e) ``''''''`` { ``''''''`` std::cerr << e.what() << std::endl; ``''''''`` } ``''''''`` return 0; ``''''''``}[heading The tcp_server class] ``''''''``class tcp_server ``''''''``{ ``''''''``public:The constructor initialises an acceptor to listen on TCP port 13. ``''''''`` tcp_server(boost::asio::io_service& io_service) ``''''''`` : acceptor_(io_service, tcp::endpoint(tcp::v4(), 13))
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?