diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000000..3e84d7c704 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,96 @@ +* text=auto !eol svneol=native#text/plain +*.gitattributes text svneol=native#text/plain + +# Scriptish formats +*.bat text svneol=native#text/plain +*.bsh text svneol=native#text/x-beanshell +*.cgi text svneol=native#text/plain +*.cmd text svneol=native#text/plain +*.js text svneol=native#text/javascript +*.php text svneol=native#text/x-php +*.pl text svneol=native#text/x-perl +*.pm text svneol=native#text/x-perl +*.py text svneol=native#text/x-python +*.sh eol=lf svneol=LF#text/x-sh +configure eol=lf svneol=LF#text/x-sh + +# Image formats +*.bmp binary svneol=unset#image/bmp +*.gif binary svneol=unset#image/gif +*.ico binary svneol=unset#image/ico +*.jpeg binary svneol=unset#image/jpeg +*.jpg binary svneol=unset#image/jpeg +*.png binary svneol=unset#image/png +*.tif binary svneol=unset#image/tiff +*.tiff binary svneol=unset#image/tiff +*.svg text svneol=native#image/svg%2Bxml + +# Data formats +*.pdf binary svneol=unset#application/pdf +*.avi binary svneol=unset#video/avi +*.doc binary svneol=unset#application/msword +*.dsp text svneol=crlf#text/plain +*.dsw text svneol=crlf#text/plain +*.eps binary svneol=unset#application/postscript +*.gz binary svneol=unset#application/gzip +*.mov binary svneol=unset#video/quicktime +*.mp3 binary svneol=unset#audio/mpeg +*.ppt binary svneol=unset#application/vnd.ms-powerpoint +*.ps binary svneol=unset#application/postscript +*.psd binary svneol=unset#application/photoshop +*.rdf binary svneol=unset#text/rdf +*.rss text svneol=unset#text/xml +*.rtf binary svneol=unset#text/rtf +*.sln text svneol=native#text/plain +*.swf binary svneol=unset#application/x-shockwave-flash +*.tgz binary svneol=unset#application/gzip +*.vcproj text svneol=native#text/xml +*.vcxproj text svneol=native#text/xml +*.vsprops text svneol=native#text/xml +*.wav binary svneol=unset#audio/wav +*.xls binary svneol=unset#application/vnd.ms-excel +*.zip binary svneol=unset#application/zip + +# Text formats +.htaccess text svneol=native#text/plain +*.bbk text svneol=native#text/xml +*.cmake text svneol=native#text/plain +*.css text svneol=native#text/css +*.dtd text svneol=native#text/xml +*.htm text svneol=native#text/html +*.html text svneol=native#text/html +*.ini text svneol=native#text/plain +*.log text svneol=native#text/plain +*.mak text svneol=native#text/plain +*.qbk text svneol=native#text/plain +*.rst text svneol=native#text/plain +*.sql text svneol=native#text/x-sql +*.txt text svneol=native#text/plain +*.xhtml text svneol=native#text/xhtml%2Bxml +*.xml text svneol=native#text/xml +*.xsd text svneol=native#text/xml +*.xsl text svneol=native#text/xml +*.xslt text svneol=native#text/xml +*.xul text svneol=native#text/xul +*.yml text svneol=native#text/plain +boost-no-inspect text svneol=native#text/plain +CHANGES text svneol=native#text/plain +COPYING text svneol=native#text/plain +INSTALL text svneol=native#text/plain +Jamfile text svneol=native#text/plain +Jamroot text svneol=native#text/plain +Jamfile.v2 text svneol=native#text/plain +Jamrules text svneol=native#text/plain +Makefile* text svneol=native#text/plain +README text svneol=native#text/plain +TODO text svneol=native#text/plain + +# Code formats +*.c text svneol=native#text/plain +*.cpp text svneol=native#text/plain +*.h text svneol=native#text/plain +*.hpp text svneol=native#text/plain +*.ipp text svneol=native#text/plain +*.tpp text svneol=native#text/plain +*.jam text svneol=native#text/plain +*.java text svneol=native#text/plain diff --git a/example/allocation/Jamfile b/example/allocation/Jamfile new file mode 100644 index 0000000000..5c7dbd3e8c --- /dev/null +++ b/example/allocation/Jamfile @@ -0,0 +1,25 @@ +subproject libs/asio/example/allocation ; + +project boost : $(BOOST_ROOT) ; + +if $(UNIX) +{ + switch $(JAMUNAME) + { + case SunOS* : + { + SOCKET_LIBS = socket nsl ; + } + } +} + +exe server + : server.cpp + : $(BOOST_ROOT) + ../../../.. + BOOST_ALL_NO_LIB=1 + multi + <*>ws2_32 + <*>mswsock + $(SOCKET_LIBS) + ; diff --git a/example/allocation/server.cpp b/example/allocation/server.cpp new file mode 100644 index 0000000000..ce73cca99f --- /dev/null +++ b/example/allocation/server.cpp @@ -0,0 +1,229 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using boost::asio::ip::tcp; + +// Class to manage the memory to be used for handler-based custom allocation. +// It contains a single block of memory which may be returned for allocation +// requests. If the memory is in use when an allocation request is made, the +// allocator delegates allocation to the global heap. +class handler_allocator + : private boost::noncopyable +{ +public: + handler_allocator() + : in_use_(false) + { + } + + void* allocate(std::size_t size) + { + if (!in_use_ && size < storage_.size) + { + in_use_ = true; + return storage_.address(); + } + else + { + return ::operator new(size); + } + } + + void deallocate(void* pointer) + { + if (pointer == storage_.address()) + { + in_use_ = false; + } + else + { + ::operator delete(pointer); + } + } + +private: + // Storage space used for handler-based custom memory allocation. + boost::aligned_storage<1024> storage_; + + // Whether the handler-based custom allocation storage has been used. + bool in_use_; +}; + +// Wrapper class template for handler objects to allow handler memory +// allocation to be customised. Calls to operator() are forwarded to the +// encapsulated handler. +template +class custom_alloc_handler +{ +public: + custom_alloc_handler(handler_allocator& a, Handler h) + : allocator_(a), + handler_(h) + { + } + + template + void operator()(Arg1 arg1) + { + handler_(arg1); + } + + template + void operator()(Arg1 arg1, Arg2 arg2) + { + handler_(arg1, arg2); + } + + friend void* asio_handler_allocate(std::size_t size, + custom_alloc_handler* this_handler) + { + return this_handler->allocator_.allocate(size); + } + + friend void asio_handler_deallocate(void* pointer, std::size_t size, + custom_alloc_handler* this_handler) + { + this_handler->allocator_.deallocate(pointer); + } + +private: + handler_allocator& allocator_; + Handler handler_; +}; + +// Helper function to wrap a handler object to add custom allocation. +template +inline custom_alloc_handler make_custom_alloc_handler( + handler_allocator& a, Handler h) +{ + return custom_alloc_handler(a, h); +} + +class session + : public boost::enable_shared_from_this +{ +public: + session(boost::asio::io_service& io_service) + : socket_(io_service) + { + } + + tcp::socket& socket() + { + return socket_; + } + + void start() + { + socket_.async_read_some(boost::asio::buffer(data_), + make_custom_alloc_handler(allocator_, + boost::bind(&session::handle_read, + shared_from_this(), + boost::asio::placeholders::error, + boost::asio::placeholders::bytes_transferred))); + } + + void handle_read(const boost::asio::error& error, size_t bytes_transferred) + { + if (!error) + { + boost::asio::async_write(socket_, + boost::asio::buffer(data_, bytes_transferred), + make_custom_alloc_handler(allocator_, + boost::bind(&session::handle_write, + shared_from_this(), + boost::asio::placeholders::error))); + } + } + + void handle_write(const boost::asio::error& error) + { + if (!error) + { + socket_.async_read_some(boost::asio::buffer(data_), + make_custom_alloc_handler(allocator_, + boost::bind(&session::handle_read, + shared_from_this(), + boost::asio::placeholders::error, + boost::asio::placeholders::bytes_transferred))); + } + } + +private: + // The socket used to communicate with the client. + tcp::socket socket_; + + // Buffer used to store data received from the client. + boost::array data_; + + // The allocator to use for handler-based custom memory allocation. + handler_allocator allocator_; +}; + +typedef boost::shared_ptr session_ptr; + +class server +{ +public: + server(boost::asio::io_service& io_service, short port) + : io_service_(io_service), + acceptor_(io_service, tcp::endpoint(tcp::v4(), port)) + { + session_ptr new_session(new session(io_service_)); + acceptor_.async_accept(new_session->socket(), + boost::bind(&server::handle_accept, this, new_session, + boost::asio::placeholders::error)); + } + + void handle_accept(session_ptr new_session, const boost::asio::error& error) + { + if (!error) + { + new_session->start(); + new_session.reset(new session(io_service_)); + acceptor_.async_accept(new_session->socket(), + boost::bind(&server::handle_accept, this, new_session, + boost::asio::placeholders::error)); + } + } + +private: + boost::asio::io_service& io_service_; + tcp::acceptor acceptor_; +}; + +int main(int argc, char* argv[]) +{ + try + { + if (argc != 2) + { + std::cerr << "Usage: server \n"; + return 1; + } + + boost::asio::io_service io_service; + + using namespace std; // For atoi. + server s(io_service, atoi(argv[1])); + + io_service.run(); + } + catch (boost::asio::error& e) + { + std::cerr << e << "\n"; + } + catch (std::exception& e) + { + std::cerr << "Exception: " << e.what() << "\n"; + } + + return 0; +} diff --git a/example/buffers/Jamfile b/example/buffers/Jamfile new file mode 100644 index 0000000000..16a045c2f1 --- /dev/null +++ b/example/buffers/Jamfile @@ -0,0 +1,25 @@ +subproject libs/asio/example/buffers ; + +project boost : $(BOOST_ROOT) ; + +if $(UNIX) +{ + switch $(JAMUNAME) + { + case SunOS* : + { + SOCKET_LIBS = socket nsl ; + } + } +} + +exe server + : reference_counted.cpp + : $(BOOST_ROOT) + ../../../.. + BOOST_ALL_NO_LIB=1 + multi + <*>ws2_32 + <*>mswsock + $(SOCKET_LIBS) + ; diff --git a/example/buffers/reference_counted.cpp b/example/buffers/reference_counted.cpp new file mode 100644 index 0000000000..f35c0fe42c --- /dev/null +++ b/example/buffers/reference_counted.cpp @@ -0,0 +1,118 @@ +#include +#include +#include +#include +#include + +using boost::asio::ip::tcp; + +// A reference-counted non-modifiable buffer class. +class shared_const_buffer +{ +public: + // Construct from a std::string. + explicit shared_const_buffer(const std::string& data) + : data_(new std::vector(data.begin(), data.end())), + buffer_(boost::asio::buffer(*data_)) + { + } + + // Implement the Const_Buffers concept. + typedef boost::asio::const_buffer value_type; + typedef const boost::asio::const_buffer* const_iterator; + const boost::asio::const_buffer* begin() const { return &buffer_; } + const boost::asio::const_buffer* end() const { return &buffer_ + 1; } + +private: + boost::shared_ptr > data_; + boost::asio::const_buffer buffer_; +}; + +class session + : public boost::enable_shared_from_this +{ +public: + session(boost::asio::io_service& io_service) + : socket_(io_service) + { + } + + tcp::socket& socket() + { + return socket_; + } + + void start() + { + using namespace std; // For time_t, time and ctime. + time_t now = time(0); + shared_const_buffer buffer(ctime(&now)); + boost::asio::async_write(socket_, buffer, + boost::bind(&session::handle_write, shared_from_this())); + } + + void handle_write() + { + } + +private: + // The socket used to communicate with the client. + tcp::socket socket_; +}; + +typedef boost::shared_ptr session_ptr; + +class server +{ +public: + server(boost::asio::io_service& io_service, short port) + : io_service_(io_service), + acceptor_(io_service, tcp::endpoint(tcp::v4(), port)) + { + session_ptr new_session(new session(io_service_)); + acceptor_.async_accept(new_session->socket(), + boost::bind(&server::handle_accept, this, new_session, + boost::asio::placeholders::error)); + } + + void handle_accept(session_ptr new_session, const boost::asio::error& error) + { + if (!error) + { + new_session->start(); + new_session.reset(new session(io_service_)); + acceptor_.async_accept(new_session->socket(), + boost::bind(&server::handle_accept, this, new_session, + boost::asio::placeholders::error)); + } + } + +private: + boost::asio::io_service& io_service_; + tcp::acceptor acceptor_; +}; + +int main(int argc, char* argv[]) +{ + try + { + if (argc != 2) + { + std::cerr << "Usage: reference_counted \n"; + return 1; + } + + boost::asio::io_service io_service; + + using namespace std; // For atoi. + server s(io_service, atoi(argv[1])); + + io_service.run(); + } + catch (std::exception& e) + { + std::cerr << "Exception: " << e.what() << "\n"; + } + + return 0; +} diff --git a/example/chat/Jamfile b/example/chat/Jamfile new file mode 100644 index 0000000000..0cc5bf7802 --- /dev/null +++ b/example/chat/Jamfile @@ -0,0 +1,38 @@ +subproject libs/asio/example/chat ; + +project boost : $(BOOST_ROOT) ; + +if $(UNIX) +{ + switch $(JAMUNAME) + { + case SunOS* : + { + SOCKET_LIBS = socket nsl ; + } + } +} + +exe chat_client + : @boost/libs/thread/build/boost_thread + chat_client.cpp + : $(BOOST_ROOT) + ../../../.. + BOOST_ALL_NO_LIB=1 + multi + <*>ws2_32 + <*>mswsock + $(SOCKET_LIBS) + ; + +exe chat_server + : @boost/libs/thread/build/boost_thread + chat_server.cpp + : $(BOOST_ROOT) + ../../../.. + BOOST_ALL_NO_LIB=1 + multi + <*>ws2_32 + <*>mswsock + $(SOCKET_LIBS) + ; diff --git a/example/chat/chat_client.cpp b/example/chat/chat_client.cpp new file mode 100644 index 0000000000..57876fba70 --- /dev/null +++ b/example/chat/chat_client.cpp @@ -0,0 +1,181 @@ +#include +#include +#include +#include +#include +#include +#include "chat_message.hpp" + +using boost::asio::ip::tcp; + +typedef std::deque chat_message_queue; + +class chat_client +{ +public: + chat_client(boost::asio::io_service& io_service, + tcp::resolver::iterator endpoint_iterator) + : io_service_(io_service), + socket_(io_service) + { + tcp::endpoint endpoint = *endpoint_iterator; + socket_.async_connect(endpoint, + boost::bind(&chat_client::handle_connect, this, + boost::asio::placeholders::error, ++endpoint_iterator)); + } + + void write(const chat_message& msg) + { + io_service_.post(boost::bind(&chat_client::do_write, this, msg)); + } + + void close() + { + io_service_.post(boost::bind(&chat_client::do_close, this)); + } + +private: + + void handle_connect(const boost::asio::error& error, + tcp::resolver::iterator endpoint_iterator) + { + if (!error) + { + boost::asio::async_read(socket_, + boost::asio::buffer(read_msg_.data(), chat_message::header_length), + boost::bind(&chat_client::handle_read_header, this, + boost::asio::placeholders::error)); + } + else if (endpoint_iterator != tcp::resolver::iterator()) + { + socket_.close(); + tcp::endpoint endpoint = *endpoint_iterator; + socket_.async_connect(endpoint, + boost::bind(&chat_client::handle_connect, this, + boost::asio::placeholders::error, ++endpoint_iterator)); + } + } + + void handle_read_header(const boost::asio::error& error) + { + if (!error && read_msg_.decode_header()) + { + boost::asio::async_read(socket_, + boost::asio::buffer(read_msg_.body(), read_msg_.body_length()), + boost::bind(&chat_client::handle_read_body, this, + boost::asio::placeholders::error)); + } + else + { + do_close(); + } + } + + void handle_read_body(const boost::asio::error& error) + { + if (!error) + { + std::cout.write(read_msg_.body(), read_msg_.body_length()); + std::cout << "\n"; + boost::asio::async_read(socket_, + boost::asio::buffer(read_msg_.data(), chat_message::header_length), + boost::bind(&chat_client::handle_read_header, this, + boost::asio::placeholders::error)); + } + else + { + do_close(); + } + } + + void do_write(chat_message msg) + { + bool write_in_progress = !write_msgs_.empty(); + write_msgs_.push_back(msg); + if (!write_in_progress) + { + boost::asio::async_write(socket_, + boost::asio::buffer(write_msgs_.front().data(), + write_msgs_.front().length()), + boost::bind(&chat_client::handle_write, this, + boost::asio::placeholders::error)); + } + } + + void handle_write(const boost::asio::error& error) + { + if (!error) + { + write_msgs_.pop_front(); + if (!write_msgs_.empty()) + { + boost::asio::async_write(socket_, + boost::asio::buffer(write_msgs_.front().data(), + write_msgs_.front().length()), + boost::bind(&chat_client::handle_write, this, + boost::asio::placeholders::error)); + } + } + else + { + do_close(); + } + } + + void do_close() + { + socket_.close(); + } + +private: + boost::asio::io_service& io_service_; + tcp::socket socket_; + chat_message read_msg_; + chat_message_queue write_msgs_; +}; + +int main(int argc, char* argv[]) +{ + try + { + if (argc != 3) + { + std::cerr << "Usage: chat_client \n"; + return 1; + } + + boost::asio::io_service io_service; + + tcp::resolver resolver(io_service); + tcp::resolver::query query(argv[1], argv[2]); + tcp::resolver::iterator iterator = resolver.resolve(query); + + chat_client c(io_service, iterator); + + boost::thread t(boost::bind(&boost::asio::io_service::run, &io_service)); + + char line[chat_message::max_body_length + 1]; + while (std::cin.getline(line, chat_message::max_body_length + 1)) + { + using namespace std; // For strlen and memcpy. + chat_message msg; + msg.body_length(strlen(line)); + memcpy(msg.body(), line, msg.body_length()); + msg.encode_header(); + c.write(msg); + } + + c.close(); + t.join(); + } + catch (boost::asio::error& e) + { + std::cerr << e << "\n"; + } + catch (std::exception& e) + { + std::cerr << "Exception: " << e.what() << "\n"; + } + + return 0; +} diff --git a/example/chat/chat_message.hpp b/example/chat/chat_message.hpp new file mode 100644 index 0000000000..156765275b --- /dev/null +++ b/example/chat/chat_message.hpp @@ -0,0 +1,83 @@ +#ifndef CHAT_MESSAGE_HPP +#define CHAT_MESSAGE_HPP + +#include +#include +#include + +class chat_message +{ +public: + enum { header_length = 4 }; + enum { max_body_length = 512 }; + + chat_message() + : body_length_(0) + { + } + + const char* data() const + { + return data_; + } + + char* data() + { + return data_; + } + + size_t length() const + { + return header_length + body_length_; + } + + const char* body() const + { + return data_ + header_length; + } + + char* body() + { + return data_ + header_length; + } + + size_t body_length() const + { + return body_length_; + } + + void body_length(size_t length) + { + body_length_ = length; + if (body_length_ > max_body_length) + body_length_ = max_body_length; + } + + bool decode_header() + { + using namespace std; // For strncat and atoi. + char header[header_length + 1] = ""; + strncat(header, data_, header_length); + body_length_ = atoi(header); + if (body_length_ > max_body_length) + { + body_length_ = 0; + return false; + } + return true; + } + + void encode_header() + { + using namespace std; // For sprintf and memcpy. + char header[header_length + 1] = ""; + sprintf(header, "%4d", body_length_); + memcpy(data_, header, header_length); + } + +private: + char data_[header_length + max_body_length]; + size_t body_length_; +}; + +#endif // CHAT_MESSAGE_HPP diff --git a/example/chat/chat_server.cpp b/example/chat/chat_server.cpp new file mode 100644 index 0000000000..dafb733afc --- /dev/null +++ b/example/chat/chat_server.cpp @@ -0,0 +1,237 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "chat_message.hpp" + +using boost::asio::ip::tcp; + +//---------------------------------------------------------------------- + +typedef std::deque chat_message_queue; + +//---------------------------------------------------------------------- + +class chat_participant +{ +public: + virtual ~chat_participant() {} + virtual void deliver(const chat_message& msg) = 0; +}; + +typedef boost::shared_ptr chat_participant_ptr; + +//---------------------------------------------------------------------- + +class chat_room +{ +public: + void join(chat_participant_ptr participant) + { + participants_.insert(participant); + std::for_each(recent_msgs_.begin(), recent_msgs_.end(), + boost::bind(&chat_participant::deliver, participant, _1)); + } + + void leave(chat_participant_ptr participant) + { + participants_.erase(participant); + } + + void deliver(const chat_message& msg) + { + recent_msgs_.push_back(msg); + while (recent_msgs_.size() > max_recent_msgs) + recent_msgs_.pop_front(); + + std::for_each(participants_.begin(), participants_.end(), + boost::bind(&chat_participant::deliver, _1, boost::ref(msg))); + } + +private: + std::set participants_; + enum { max_recent_msgs = 100 }; + chat_message_queue recent_msgs_; +}; + +//---------------------------------------------------------------------- + +class chat_session + : public chat_participant, + public boost::enable_shared_from_this +{ +public: + chat_session(boost::asio::io_service& io_service, chat_room& room) + : socket_(io_service), + room_(room) + { + } + + tcp::socket& socket() + { + return socket_; + } + + void start() + { + room_.join(shared_from_this()); + boost::asio::async_read(socket_, + boost::asio::buffer(read_msg_.data(), chat_message::header_length), + boost::bind( + &chat_session::handle_read_header, shared_from_this(), + boost::asio::placeholders::error)); + } + + void deliver(const chat_message& msg) + { + bool write_in_progress = !write_msgs_.empty(); + write_msgs_.push_back(msg); + if (!write_in_progress) + { + boost::asio::async_write(socket_, + boost::asio::buffer(write_msgs_.front().data(), + write_msgs_.front().length()), + boost::bind(&chat_session::handle_write, shared_from_this(), + boost::asio::placeholders::error)); + } + } + + void handle_read_header(const boost::asio::error& error) + { + if (!error && read_msg_.decode_header()) + { + boost::asio::async_read(socket_, + boost::asio::buffer(read_msg_.body(), read_msg_.body_length()), + boost::bind(&chat_session::handle_read_body, shared_from_this(), + boost::asio::placeholders::error)); + } + else + { + room_.leave(shared_from_this()); + } + } + + void handle_read_body(const boost::asio::error& error) + { + if (!error) + { + room_.deliver(read_msg_); + boost::asio::async_read(socket_, + boost::asio::buffer(read_msg_.data(), chat_message::header_length), + boost::bind(&chat_session::handle_read_header, shared_from_this(), + boost::asio::placeholders::error)); + } + else + { + room_.leave(shared_from_this()); + } + } + + void handle_write(const boost::asio::error& error) + { + if (!error) + { + write_msgs_.pop_front(); + if (!write_msgs_.empty()) + { + boost::asio::async_write(socket_, + boost::asio::buffer(write_msgs_.front().data(), + write_msgs_.front().length()), + boost::bind(&chat_session::handle_write, shared_from_this(), + boost::asio::placeholders::error)); + } + } + else + { + room_.leave(shared_from_this()); + } + } + +private: + tcp::socket socket_; + chat_room& room_; + chat_message read_msg_; + chat_message_queue write_msgs_; +}; + +typedef boost::shared_ptr chat_session_ptr; + +//---------------------------------------------------------------------- + +class chat_server +{ +public: + chat_server(boost::asio::io_service& io_service, + const tcp::endpoint& endpoint) + : io_service_(io_service), + acceptor_(io_service, endpoint) + { + chat_session_ptr new_session(new chat_session(io_service_, room_)); + acceptor_.async_accept(new_session->socket(), + boost::bind(&chat_server::handle_accept, this, new_session, + boost::asio::placeholders::error)); + } + + void handle_accept(chat_session_ptr session, const boost::asio::error& error) + { + if (!error) + { + session->start(); + chat_session_ptr new_session(new chat_session(io_service_, room_)); + acceptor_.async_accept(new_session->socket(), + boost::bind(&chat_server::handle_accept, this, new_session, + boost::asio::placeholders::error)); + } + } + +private: + boost::asio::io_service& io_service_; + tcp::acceptor acceptor_; + chat_room room_; +}; + +typedef boost::shared_ptr chat_server_ptr; +typedef std::list chat_server_list; + +//---------------------------------------------------------------------- + +int main(int argc, char* argv[]) +{ + try + { + if (argc < 2) + { + std::cerr << "Usage: chat_server [ ...]\n"; + return 1; + } + + boost::asio::io_service io_service; + + chat_server_list servers; + for (int i = 1; i < argc; ++i) + { + using namespace std; // For atoi. + tcp::endpoint endpoint(tcp::v4(), atoi(argv[i])); + chat_server_ptr server(new chat_server(io_service, endpoint)); + servers.push_back(server); + } + + io_service.run(); + } + catch (boost::asio::error& e) + { + std::cerr << e << "\n"; + } + catch (std::exception& e) + { + std::cerr << "Exception: " << e.what() << "\n"; + } + + return 0; +} diff --git a/example/echo/Jamfile b/example/echo/Jamfile new file mode 100644 index 0000000000..54a561f1eb --- /dev/null +++ b/example/echo/Jamfile @@ -0,0 +1,55 @@ +subproject libs/asio/example/echo ; + +project boost : $(BOOST_ROOT) ; + +if $(UNIX) +{ + switch $(JAMUNAME) + { + case SunOS* : + { + SOCKET_LIBS = socket nsl ; + } + } +} + +template asio_echo_example + : @boost/libs/thread/build/boost_thread + : $(BOOST_ROOT) + ../../../.. + BOOST_ALL_NO_LIB=1 + multi + <*>ws2_32 + <*>mswsock + $(SOCKET_LIBS) + ; + +exe async_tcp_echo_server + :