C ++ 03: Use boost-implement simple echo server with ASIO

Source: Internet
Author: User

C ++ 03: Use boost-implement simple echo server with ASIO

C ++ 03: Use boost-implement simple echo with ASIO Server  

I. WritingCode

Note: The following code is taken from the example code of boost 1.40:

/// Async_tcp_echo_server.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_00000.txt or copy at http://www.boost.org/LICENSE_1_0.txt) // # include <cstdlib> # include <iostream> # include <boost/bind. HPP> # include <boost/ASIO. HPP> using boost: ASIO: IP: TCP; class session {public: Session (boost: ASIO: io_servi CE & io_service): Socket _ (io_service) {} TCP: Socket & socket () {return socket _;} void start () {socket _. async_read_some (boost: ASIO: buffer (Data _, max_length), boost: BIND (& session: handle_read, this, boost: ASIO: placeholders: error, boost: ASIO: placeholders: bytes_transferred);} void handle_read (const boost: System: error_code & error, size_t bytes_transferred) {If (! Error) {boost: ASIO: async_write (socket _, boost: ASIO: buffer (Data _, bytes_transferred), boost: BIND (& session: handle_write, this, boost: ASIO: placeholders: Error) ;}else {Delete this ;}} void handle_write (const boost: System: error_code & error) {If (! Error) {socket _. async_read_some (boost: ASIO: buffer (Data _, max_length), boost: BIND (& session: handle_read, this, boost: ASIO: placeholders: error, boost: ASIO: placeholders: bytes_transferred);} else {Delete this ;}} PRIVATE: TCP :: Socket socket _; Enum {max_length = 1024 }; char data _ [max_length] ;}; class server {public: Server (boost: ASIO: io_service & io_service, short port): io_service _ (io_se Rvice), acceptor _ (io_service, TCP: endpoint (TCP: V4 (), Port) {session * 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 * new_session, const boost: System: error_code & error) {If (! Error) {new_session-> Start (); new_session = new session (io_service _); acceptor _. async_accept (new_session-> socket (), boost: BIND (& server: handle_accept, this, new_session, boost: ASIO: placeholders: Error ));} else {Delete new_session;} PRIVATE: boost: ASIO: io_service & io_service _; TCP: acceptor _ ;}; int main (INT argc, char * argv []) {try {If (argc! = 2) {STD: cerr <"Usage: async_tcp_echo_server <port> \ n"; return 1;} boost: ASIO: 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 ;}
Ii. Compile the code
 
$ G ++-G3-wall-o "async_tcp_echo_server" async_tcp_echo_server.cpp-lboost_system

Note: The boost. ASIO library depends on boost_system.

Iii. Running Async_tcp_echo_server
 
$./Async_tcp_echo_server 8868#8868 is the port number of async_tcp_echo_server listen
Iv. Use Telnet command as client Test

Start a new terminal,Note: log on to the Linux lab.Run Telnet localhost 8868.

$ Telnet localhost 8868 trying: 1... trying 127.0.0.1... connected to localhost. Escape Character is '^]'. The first line# This is our input.The first line# This is a serverAsync_tcp_echo_server response, And so onThe second linethe second linewelcome to xuanyuan-soft.org.cn! Welcome to xuanyuan-soft.org.cn!
5. More information about boost

Please visit boost home page: http://www.boost.org/

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.