1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4<title>Socket Iostreams</title> 5<link rel="stylesheet" href="../../../../../doc/src/boostbook.css" type="text/css"> 6<meta name="generator" content="DocBook XSL Stylesheets V1.79.1"> 7<link rel="home" href="../../../boost_asio.html" title="Boost.Asio"> 8<link rel="up" href="../networking.html" title="Networking"> 9<link rel="prev" href="other_protocols.html" title="Support for Other Protocols"> 10<link rel="next" href="bsd_sockets.html" title="The BSD Socket API and Boost.Asio"> 11</head> 12<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> 13<table cellpadding="2" width="100%"><tr> 14<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../boost.png"></td> 15<td align="center"><a href="../../../../../index.html">Home</a></td> 16<td align="center"><a href="../../../../../libs/libraries.htm">Libraries</a></td> 17<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> 18<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> 19<td align="center"><a href="../../../../../more/index.htm">More</a></td> 20</tr></table> 21<hr> 22<div class="spirit-nav"> 23<a accesskey="p" href="other_protocols.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../networking.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../boost_asio.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="bsd_sockets.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a> 24</div> 25<div class="section"> 26<div class="titlepage"><div><div><h4 class="title"> 27<a name="boost_asio.overview.networking.iostreams"></a><a class="link" href="iostreams.html" title="Socket Iostreams">Socket Iostreams</a> 28</h4></div></div></div> 29<p> 30 Boost.Asio includes classes that implement iostreams on top of sockets. 31 These hide away the complexities associated with endpoint resolution, protocol 32 independence, etc. To create a connection one might simply write: 33 </p> 34<pre class="programlisting">ip::tcp::iostream stream("www.boost.org", "http"); 35if (!stream) 36{ 37 // Can't connect. 38} 39</pre> 40<p> 41 The iostream class can also be used in conjunction with an acceptor to 42 create simple servers. For example: 43 </p> 44<pre class="programlisting">io_context ioc; 45 46ip::tcp::endpoint endpoint(tcp::v4(), 80); 47ip::tcp::acceptor acceptor(ios, endpoint); 48 49for (;;) 50{ 51 ip::tcp::iostream stream; 52 acceptor.accept(stream.socket()); 53 ... 54} 55</pre> 56<p> 57 Timeouts may be set by calling <code class="computeroutput">expires_at()</code> or <code class="computeroutput">expires_from_now()</code> 58 to establish a deadline. Any socket operations that occur past the deadline 59 will put the iostream into a "bad" state. 60 </p> 61<p> 62 For example, a simple client program like this: 63 </p> 64<pre class="programlisting">ip::tcp::iostream stream; 65stream.expires_from_now(boost::posix_time::seconds(60)); 66stream.connect("www.boost.org", "http"); 67stream << "GET /LICENSE_1_0.txt HTTP/1.0\r\n"; 68stream << "Host: www.boost.org\r\n"; 69stream << "Accept: */*\r\n"; 70stream << "Connection: close\r\n\r\n"; 71stream.flush(); 72std::cout << stream.rdbuf(); 73</pre> 74<p> 75 will fail if all the socket operations combined take longer than 60 seconds. 76 </p> 77<p> 78 If an error does occur, the iostream's <code class="computeroutput">error()</code> member function 79 may be used to retrieve the error code from the most recent system call: 80 </p> 81<pre class="programlisting">if (!stream) 82{ 83 std::cout << "Error: " << stream.error().message() << "\n"; 84} 85</pre> 86<h6> 87<a name="boost_asio.overview.networking.iostreams.h0"></a> 88 <span class="phrase"><a name="boost_asio.overview.networking.iostreams.see_also"></a></span><a class="link" href="iostreams.html#boost_asio.overview.networking.iostreams.see_also">See 89 Also</a> 90 </h6> 91<p> 92 <a class="link" href="../../reference/ip__tcp/iostream.html" title="ip::tcp::iostream">ip::tcp::iostream</a>, 93 <a class="link" href="../../reference/basic_socket_iostream.html" title="basic_socket_iostream">basic_socket_iostream</a>, 94 <a class="link" href="../../examples/cpp03_examples.html#boost_asio.examples.cpp03_examples.iostreams">iostreams 95 examples</a>. 96 </p> 97<h6> 98<a name="boost_asio.overview.networking.iostreams.h1"></a> 99 <span class="phrase"><a name="boost_asio.overview.networking.iostreams.notes"></a></span><a class="link" href="iostreams.html#boost_asio.overview.networking.iostreams.notes">Notes</a> 100 </h6> 101<p> 102 These iostream templates only support <code class="computeroutput">char</code>, not <code class="computeroutput">wchar_t</code>, 103 and do not perform any code conversion. 104 </p> 105</div> 106<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 107<td align="left"></td> 108<td align="right"><div class="copyright-footer">Copyright © 2003-2020 Christopher M. 109 Kohlhoff<p> 110 Distributed under the Boost Software License, Version 1.0. (See accompanying 111 file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) 112 </p> 113</div></td> 114</tr></table> 115<hr> 116<div class="spirit-nav"> 117<a accesskey="p" href="other_protocols.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../networking.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../boost_asio.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="bsd_sockets.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a> 118</div> 119</body> 120</html> 121