C + + Boost::asio Programming-asynchronous TCP detailed and instance code _c language

Source: Internet
Author: User
Tags socket

C + + Boost::asio programming-Asynchronous TCP

Hello, everyone, I'm an asynchronous way.

Unlike sync, I never take the time to wait for those turtle-speed IO operations, I just tell the system what to do, and then I can do something else. If the system completes the operation, the system will notify me by the callback object I gave it before.
In the ASIO library, a function or method name in the asynchronous way has a "async_" prefix in front of it, and a callback function (or an imitation function) is required in the function argument. The asynchronous operation is returned immediately after execution, regardless of whether it is completed, and something else can be done until the callback function (or functor) is invoked, indicating that the asynchronous operation is complete.

Many of the callback functions in ASIO accept only one boost::system::error_code parameter, which is certainly not enough when used in practice, so it is generally used to carry a bunch of related data as a callback, or to bind a bunch of data using Boost::bind.

Also note that the callback object is invoked only after the run () method of the Io_service class is running, otherwise there will be no task action even if the system has completed an asynchronous operation.

Okay, so here's the asynchronous way I brought the TCP HelloWorld server side:

BoostTcpServer.cpp: Defines the entry point for a console application. #include "stdafx.h" #include "boost/asio.hpp" #include "boost/shared_ptr.hpp" #include "boost/thread.hpp" usin 
G namespace Std; 
 
using namespace Boost::asio; #ifdef _msc_ver #define _WIN32_WINNT 0x0501//Avoid VC compiler warning #endif #define PORT 1000 #define IPV6//#define IPV4 CL  
  Ass asyncserver {public://constructor Asyncserver (Io_service &io,ip::tcp::endpoint &ep): iOS (IO), acceptor (IO,EP) 
    {//acceptor (IOS,EP); 
  Start (); 
    //Start asynchronous Accept Client connection void Start () {Sock_ptr sock (new Ip::tcp::socket (iOS)); Callback Accept_handler function acceptor.async_accept (*sock, Boost::bind (&ASYNCSERVER::ACCEPT_HANDLER,THIS,PLA) when there is a connection entry 
  Ceholders::error,sock)); 
  } Private:io_service &ios; 
  Ip::tcp::acceptor acceptor; 
 
  typedef boost::shared_ptr<ip::tcp::socket> SOCK_PTR; 
    void Accept_handler (const boost::system::error_code &AMP;EC, Sock_ptr sock) {if (EC) return; Output customerEnd-Connection Information std::cout << "Remote IP:" <<sock->remote_endpoint (). Address () <<endl; 
    Std::cout << "Remote port:" <<sock->remote_endpoint () port () << Std::endl; Sends the data asynchronously to the client and calls Write_handler Sock->async_write_some (buffer ("I heard you!") when the send completes, bind (&asyncserver::wri 
    Te_handler,this,placeholders::error)); 
  Start the asynchronous accept connection start again (); } void Write_handler (const boost::system::error_code&) {cout<< "Send msg complete!" 
  <<endl; 
 
} 
}; 
    int _tmain (int argc, _tchar* argv[]) {try {//define Io_service object Io_service iOS; 
Define service-side endpoint objects (protocols and listening ports) #ifdef IPV4 ip::tcp::endpoint serverep (IP::TCP::V4 (), port); 
#endif #ifdef IPV6 ip::tcp::endpoint serverep (Ip::tcp::v6 (), PORT); 
    #endif//Start Asynchronous Service Asyncserver Server (iOS, serverep); 
  Wait for asynchronous completion of Ios.run (); 
  catch (std::exception& e) {cout<<e.what () <<endl; 
return 0; 

 }

The client generally does not need to adopt the asynchronous method, the same synchronization method can.

Thank you for reading, I hope to help you, thank you for your support for this site!

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.