Boost logo

Boost-Commit :

From: chris_at_[hidden]
Date: 2008-07-08 18:54:39


Author: chris_kohlhoff
Date: 2008-07-08 18:54:38 EDT (Tue, 08 Jul 2008)
New Revision: 47249
URL: http://svn.boost.org/trac/boost/changeset/47249

Log:
Add example showing how to use UNIX domain sockets with connect_pair().

Added:
   trunk/libs/asio/example/local/
   trunk/libs/asio/example/local/Jamfile (contents, props changed)
   trunk/libs/asio/example/local/Jamfile.v2 (contents, props changed)
   trunk/libs/asio/example/local/connect_pair.cpp (contents, props changed)

Added: trunk/libs/asio/example/local/Jamfile
==============================================================================
--- (empty file)
+++ trunk/libs/asio/example/local/Jamfile 2008-07-08 18:54:38 EDT (Tue, 08 Jul 2008)
@@ -0,0 +1,34 @@
+#
+# 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)
+#
+
+subproject libs/asio/example/local ;
+
+project boost : $(BOOST_ROOT) ;
+
+if $(UNIX)
+{
+ switch $(JAMUNAME)
+ {
+ case SunOS* :
+ {
+ SOCKET_LIBS = <find-library>socket <find-library>nsl ;
+ }
+ }
+}
+
+exe connect_pair
+ : <lib>@boost/libs/system/build/boost_system
+ <lib>@boost/libs/thread/build/boost_thread
+ connect_pair.cpp
+ : <include>$(BOOST_ROOT)
+ <include>../../../..
+ <define>BOOST_ALL_NO_LIB=1
+ <threading>multi
+ <mingw><*><find-library>ws2_32
+ <mingw><*><find-library>mswsock
+ $(SOCKET_LIBS)
+ ;

Added: trunk/libs/asio/example/local/Jamfile.v2
==============================================================================
--- (empty file)
+++ trunk/libs/asio/example/local/Jamfile.v2 2008-07-08 18:54:38 EDT (Tue, 08 Jul 2008)
@@ -0,0 +1,39 @@
+#
+# 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)
+#
+
+import os ;
+
+if [ os.name ] = SOLARIS
+{
+ lib socket ;
+ lib nsl ;
+}
+else if [ os.name ] = NT
+{
+ lib ws2_32 ;
+ lib mswsock ;
+}
+else if [ os.name ] = HPUX
+{
+ lib ipv6 ;
+}
+
+exe connect_pair
+ : connect_pair.cpp
+ /boost/system//boost_system
+ /boost/thread//boost_thread
+ : <define>BOOST_ALL_NO_LIB=1
+ <threading>multi
+ <os>SOLARIS:<library>socket
+ <os>SOLARIS:<library>nsl
+ <os>NT:<define>_WIN32_WINNT=0x0501
+ <os>NT,<toolset>gcc:<library>ws2_32
+ <os>NT,<toolset>gcc:<library>mswsock
+ <os>NT,<toolset>gcc-cygwin:<define>__USE_W32_SOCKETS
+ <os>HPUX,<toolset>gcc:<define>_XOPEN_SOURCE_EXTENDED
+ <os>HPUX:<library>ipv6
+ ;

Added: trunk/libs/asio/example/local/connect_pair.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/asio/example/local/connect_pair.cpp 2008-07-08 18:54:38 EDT (Tue, 08 Jul 2008)
@@ -0,0 +1,140 @@
+//
+// connect_pair.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 <string>
+#include <cctype>
+#include <boost/asio.hpp>
+#include <boost/thread.hpp>
+#include <boost/array.hpp>
+#include <boost/bind.hpp>
+
+#if defined(BOOST_ASIO_HAS_LOCAL_SOCKETS)
+
+using boost::asio::local::stream_protocol;
+
+class uppercase_filter
+{
+public:
+ uppercase_filter(boost::asio::io_service& io_service)
+ : socket_(io_service)
+ {
+ }
+
+ stream_protocol::socket& socket()
+ {
+ return socket_;
+ }
+
+ void start()
+ {
+ // Wait for request.
+ socket_.async_read_some(boost::asio::buffer(data_),
+ boost::bind(&uppercase_filter::handle_read,
+ this, boost::asio::placeholders::error,
+ boost::asio::placeholders::bytes_transferred));
+ }
+
+private:
+ void handle_read(const boost::system::error_code& ec, std::size_t size)
+ {
+ if (!ec)
+ {
+ // Compute result.
+ for (std::size_t i = 0; i < size; ++i)
+ data_[i] = std::toupper(data_[i]);
+
+ // Send result.
+ boost::asio::async_write(socket_, boost::asio::buffer(data_, size),
+ boost::bind(&uppercase_filter::handle_write,
+ this, boost::asio::placeholders::error));
+ }
+ else
+ {
+ throw boost::system::system_error(ec);
+ }
+ }
+
+ void handle_write(const boost::system::error_code& ec)
+ {
+ if (!ec)
+ {
+ // Wait for request.
+ socket_.async_read_some(boost::asio::buffer(data_),
+ boost::bind(&uppercase_filter::handle_read,
+ this, boost::asio::placeholders::error,
+ boost::asio::placeholders::bytes_transferred));
+ }
+ else
+ {
+ throw boost::system::system_error(ec);
+ }
+ }
+
+ stream_protocol::socket socket_;
+ boost::array<char, 512> data_;
+};
+
+void run(boost::asio::io_service* io_service)
+{
+ try
+ {
+ io_service->run();
+ }
+ catch (std::exception& e)
+ {
+ std::cerr << "Exception in thread: " << e.what() << "\n";
+ std::exit(1);
+ }
+}
+
+int main(int argc, char* argv[])
+{
+ try
+ {
+ boost::asio::io_service io_service;
+
+ // Create filter and establish a connection to it.
+ uppercase_filter filter(io_service);
+ stream_protocol::socket socket(io_service);
+ boost::asio::local::connect_pair(socket, filter.socket());
+ filter.start();
+
+ // The io_service runs in a background thread to perform filtering.
+ boost::thread thread(boost::bind(run, &io_service));
+
+ for (;;)
+ {
+ // Collect request from user.
+ std::cout << "Enter a string: ";
+ std::string request;
+ std::getline(std::cin, request);
+
+ // Send request to filter.
+ boost::asio::write(socket, boost::asio::buffer(request));
+
+ // Wait for reply from filter.
+ std::vector<char> reply(request.size());
+ boost::asio::read(socket, boost::asio::buffer(reply));
+
+ // Show reply to user.
+ std::cout << "Result: ";
+ std::cout.write(&reply[0], request.size());
+ std::cout << std::endl;
+ }
+ }
+ catch (std::exception& e)
+ {
+ std::cerr << "Exception: " << e.what() << "\n";
+ std::exit(1);
+ }
+}
+
+#endif // defined(BOOST_ASIO_HAS_LOCAL_SOCKETS)


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk