C + + asynchronous socket processing

Source: Internet
Author: User

Server-side:

#include <boost/thread.hpp> #include <boost/asio.hpp> #include <boost/date_time/posix_time/posix_ Time.hpp>class Server{private:boost::asio::io_service &ios;boost::asio::ip::tcp::acceptor Acceptor;typedef Boost::shared_ptr<boost::asio::ip::tcp::socket> sock_pt;public:server (Boost::asio::io_service &io): iOS (IO), acceptor (iOS, Boost::asio::ip::tcp::endpoint (Boost::asio::ip::tcp::v4 (), 6688)) {Start ();} void Start () {Sock_pt sock (new Boost::asio::ip::tcp::socket (iOS));//Smart Pointer acceptor.async_accept (*sock,boost::bind ( &server::accept_handler, this, _1, sock)); Asynchronous Listener Service}void Accept_handler (const boost::system::error_code &e, sock_pt sock) {if (e)//Detect error code {return;} Std::cout << "client:"; std::cout << Sock->remote_endpoint (). Address () << std::endl;sock-> Async_write_some (Boost::asio::buffer ("Hello client ..."), Boost::bind (&server::write_hander, this, _1)); start () ; Start the asynchronous listener again}void Write_hander (const Boost::system::error_code &e) {sTd::cout << "Send message complete." << Std::endl;}; int main () {try{std::cout << "server start ..." << std::endl;boost::asio::io_service iOS; Server server (iOS); Ios.run (); Initiates an asynchronous invoke event processing Loop}catch (std::exception &e) {std::cout << e.what () << Std::endl;} return 0;}

  

Client:

#include <boost/thread.hpp> #include <boost/asio.hpp> #include <boost/date_time/posix_time/posix_ time.hpp> #include <vector>class client{private:boost::asio::io_service &ios;boost::asio::ip::tcp:: Endpoint Ep;typedef boost::shared_ptr<boost::asio::ip::tcp::socket> sock_pt;public:client (boost::asio::io_ Service &io): iOS (IO), EP (Boost::asio::ip::address::from_string ("127.0.0.1"), 6688) {start ();//Start Asynchronous Connection}void start () {Sock_pt sock (new Boost::asio::ip::tcp::socket (iOS)); Sock->async_connect (EP, Boost::bind (&client::conn_ Hander, this, _1, sock)); Asynchronous Connection}void Conn_hander (const boost::system::error_code &e, sock_pt sock) {if (e) {return;} Std::cout << "recive from" << sock->remote_endpoint (). Address () << ":"; BOOST::SHARED_PTR&LT;STD: :vector<char> > str (new std::vector<char> (0)); Sock->async_read_some (Boost::asio::buffer (* STR), Boost::bind (&client::read_hander, this, _1, str)); Read data asynchronously start (); Start the asynchronous connection againConnect}void read_hander (const boost::system::error_code &e, boost::shared_ptr<std::vector<char> > str) {if (e) {return;} Std::cout << & (*STR) [0] << Std::endl;}}; int main () {try{std::cout << "client start ..." << std::endl;boost::asio::io_service iOS; Client client (iOS); Ios.run ();} catch (Std::exception &e) {std::cout << e.what () << Std::endl;} return 0;}

  

C + + asynchronous socket processing

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.