2
0
mirror of https://github.com/boostorg/asio.git synced 2026-01-28 06:42:08 +00:00
Files
asio/example/echo/blocking_tcp_echo_server.cpp
Christopher Kohlhoff 505192a463 Merged revisions 41701-41702,41706,41708-41726,41728-41733,41737,41743-41769,41771-41774,41776-41777,41779-41787,41790,41792-41794,41796-41797,41799-41800,41803-41804,41806-41818,41820-41829,41831-41841,41843-41862,41865,41867-41870 via svnmerge from
https://svn.boost.org/svn/boost/trunk

........
  r41701 | chris_kohlhoff | 2007-12-05 08:28:42 +1100 (Wed, 05 Dec 2007) | 2 lines
  
  Prevent deprecated function warnings for MSVC >= 8.
........
  r41762 | chris_kohlhoff | 2007-12-06 08:46:19 +1100 (Thu, 06 Dec 2007) | 2 lines
  
  Don't use deprecated function workaround when compiling for Windows CE.
........
  r41823 | chris_kohlhoff | 2007-12-07 23:53:39 +1100 (Fri, 07 Dec 2007) | 2 lines
  
  Try to fix stall when sending large amounts of data over SSL.
........
  r41845 | chris_kohlhoff | 2007-12-08 11:18:59 +1100 (Sat, 08 Dec 2007) | 2 lines
  
  Documentation fixes.
........
  r41867 | chris_kohlhoff | 2007-12-09 00:00:45 +1100 (Sun, 09 Dec 2007) | 2 lines
  
  Documentation fixes.
........
  r41868 | chris_kohlhoff | 2007-12-09 00:48:52 +1100 (Sun, 09 Dec 2007) | 2 lines
  
  Suppress signed/unsigned warning.
........
  r41870 | chris_kohlhoff | 2007-12-09 01:03:40 +1100 (Sun, 09 Dec 2007) | 2 lines
  
  Ensure asio header comes before boost.thread header.
........


[SVN r42084]
2007-12-15 22:00:19 +00:00

81 lines
1.8 KiB
C++

//
// blocking_tcp_echo_server.cpp
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2007 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.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;
}