mirror of
https://github.com/boostorg/asio.git
synced 2026-01-23 05:22:08 +00:00
io_service.notify_fork() at the appropriate times. Two new examples have been added showing how to use this feature. Refs #3238, #4162. * Clean up the handling of errors reported by the close() system call. In particular, assume that most operating systems won't have close() fail with EWOULDBLOCK, but if it does then set blocking mode and restart the call. If any other error occurs we assume the descriptor is closed. Refs #3307. * EV_ONESHOT seems to cause problems on some versions of Mac OS X, with the io_service destructor getting stuck inside the close() system call. Use EV_CLEAR instead. Refs #5021. * Include function name in exception what() messages. * Fix insufficient initialisers warning with MinGW. * Make the shutdown_service() member functions private. * Add archetypes for testing socket option functions. * Add missing lock in signal_set_service::cancel(). * Fix copy/paste error in SignalHandler example. * The signal header needs to be included in signal_set_service.hpp so that we can use constants like NSIG and SIGRTMAX. * Don't use Boost.Thread's convenience header. Use the header file that is specifically for the boost::thread class instead. [SVN r69467]
81 lines
1.8 KiB
C++
81 lines
1.8 KiB
C++
//
|
|
// blocking_tcp_echo_server.cpp
|
|
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
//
|
|
// Copyright (c) 2003-2011 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 <cstdlib>
|
|
#include <iostream>
|
|
#include <boost/bind.hpp>
|
|
#include <boost/smart_ptr.hpp>
|
|
#include <boost/asio.hpp>
|
|
#include <boost/thread/thread.hpp>
|
|
|
|
using boost::asio::ip::tcp;
|
|
|
|
const int max_length = 1024;
|
|
|
|
typedef boost::shared_ptr<tcp::socket> socket_ptr;
|
|
|
|
void session(socket_ptr sock)
|
|
{
|
|
try
|
|
{
|
|
for (;;)
|
|
{
|
|
char data[max_length];
|
|
|
|
boost::system::error_code error;
|
|
size_t length = sock->read_some(boost::asio::buffer(data), 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.
|
|
|
|
boost::asio::write(*sock, boost::asio::buffer(data, length));
|
|
}
|
|
}
|
|
catch (std::exception& e)
|
|
{
|
|
std::cerr << "Exception in thread: " << e.what() << "\n";
|
|
}
|
|
}
|
|
|
|
void server(boost::asio::io_service& io_service, short port)
|
|
{
|
|
tcp::acceptor a(io_service, tcp::endpoint(tcp::v4(), port));
|
|
for (;;)
|
|
{
|
|
socket_ptr sock(new tcp::socket(io_service));
|
|
a.accept(*sock);
|
|
boost::thread t(boost::bind(session, sock));
|
|
}
|
|
}
|
|
|
|
int main(int argc, char* argv[])
|
|
{
|
|
try
|
|
{
|
|
if (argc != 2)
|
|
{
|
|
std::cerr << "Usage: blocking_tcp_echo_server <port>\n";
|
|
return 1;
|
|
}
|
|
|
|
boost::asio::io_service io_service;
|
|
|
|
using namespace std; // For atoi.
|
|
server(io_service, atoi(argv[1]));
|
|
}
|
|
catch (std::exception& e)
|
|
{
|
|
std::cerr << "Exception: " << e.what() << "\n";
|
|
}
|
|
|
|
return 0;
|
|
}
|