ASIO learning 3: asynchronous TCP server (datetime)

Source: Internet
Author: User
# Include < Ctime >
# Include < Iostream >
# Include < Boost / Bind. HPP >
# Include < Boost / Shared_ptr.hpp >
# Include < Boost / Enable_shared_from_this.hpp >
# Include < Boost / ASIO. HPP >

UsingBoost: ASIO: IP: TCP;

STD ::StringMake_daytime_string ()
{
Using NamespaceSTD;
Time_t now=Time (0);
ReturnCtime (&Now );
}

/*
class derived from enable_shared_from_this,
shared_from_this returns shared_ptr

the tcp_connection class manages the socket of the new connection and sends the time string to the client after the connection.
*/
class tcp_connection
: Public boost: enable_shared_from_this tcp_connection >
{< br> Public :< br> typedef boost: shared_ptr tcp_connection > pointer;

StaticPointer create (boost: ASIO: io_service&Io_service)
{
ReturnPointer (NewTcp_connection (io_service ));
}

TCP: Socket&Socket ()
{
ReturnSocket _;
}

Void Start ()
{
Message _ = Make_daytime_string ();
/*
* Asynchronously write data to the new connection socket
* Pay attention to the usage of BIND (you can view the relevant information). Here is the binding class member function.
* The first parameter is the class member function address, and the second parameter is the class variable pointer, smart pointer, or class variable.
* Each parameter passed to the class member function is followed by several parameters.
*/
Boost: ASIO: async_write (socket _, boost: ASIO: buffer (Message _),
Boost: BIND ( & Tcp_connection: handle_write, shared_from_this (),
Boost: ASIO: placeholders: error,
Boost: ASIO: placeholders: bytes_transferred ));
}
Private :
Tcp_connection (boost: ASIO: io_service & Io_service)
: Socket _ (io_service)
{
}
Void Handle_write ( Const Boost: ASIO: Error & /* Error */ ,
Size_t /* Bytes_transferfed */ )
{
}
TCP: Socket socket _;
STD :: String Message _;
};

Class Tcp_server
{
Public :
Tcp_server (boost: ASIO: io_service & Io)
: Acceptor _ (Io, TCP: endpoint (TCP: V4 (), 13 ))
{
Start_accept ();
}
Private :
Void Start_accept ()
{
// Create a new tcp_connection object. The member variable socket _ will be used as an acceptor parameter.
Tcp_connection: pointer new_connection =
Tcp_connection: Create (acceptor _. io_service ());

//Waiting to accept new connection
Acceptor _. async_accept (new_connection->Socket (),
Boost: BIND (&Tcp_server: handle_accept,This,
New_connection, boost: ASIO: placeholders: Error ));

}

/*
* Call handle_accept when async_accept accepts the new connection.
* In this function, we respond to the customer request and wait for the next connection.
*/
Void Handle_accept (tcp_connection: pointer new_connection,
Boost: ASIO: Error & Error)
{
If ( ! Error)
{
New_connection -> Start ();
Start_accept ();
}

}
Private:
/*
/// TCP socket type.
Typedef basic_stream_socket <TCP> socket;
/// TCP acceptor type.
Typedef basic_socket_acceptor <TCP> acceptor;
*/
TCP: acceptor _;
};

/*
The io_service class provides core I/O functions for users of asynchronous I/O objects (complete ports are used in Windows or later environments)
For example:
Boost: ASIO: IP: TCP: Socket
Boost: ASIO: IP: TCP: Acceptor
Boost: ASIO: IP: UDP: Socket
Boost: ASIO: deadline_timer
*/
Int _ Tmain ( Int Argc, _ tchar * Argv [])
{
Try {
Boost: ASIO: io_service IO;
Tcp_server server (IO );
Io. Run ();
}
Catch (STD: exception & E)
{
STD: cerr < E. What () < STD: Endl;
}
Return   0 ;
}

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.