C + + synchronous socket processing

Source: Internet
Author: User

The 1.socket class is the basic class for TCP communication, and the call member function connect () can connect to a specified communication endpoint, with Local_endpoint () and Remote_endpoint () to obtain the endpoint of the connection at the end of the connection, using Read_ Some () and Write_some () block read and write data and use the close () function to close the socket when the operation is complete. If you do not close the socket, close () is automatically called Close () when the socket is destructor.
The 2.acceptor class corresponds to the Socketapi's accept () function, which is used on the server side to accept connections at the specified port number and must be mated with the socket class to complete the communication.
3.resolver class object Socketapi getaddrinfo () series functions for client-side resolution URLs to obtain an available IP address, the resolved IP address can be connected using the socket object.

Server-side:

#include <boost/thread.hpp> #include <boost/asio.hpp> #include <boost/date_time/posix_time/posix_ Time.hpp>int Main () {try{std::cout << "server start ..." << Std::endl;boost::asio::io_service ios;boost: : Asio::ip::tcp::acceptor acceptor (iOS, Boost::asio::ip::tcp::endpoint (Boost::asio::ip::tcp::v4 (), 6688)); Accept 6688 Ports Std::cout << Acceptor.local_endpoint (). Address () << Std::endl;while (TRUE)//loop execution Service {boost:: Asio::ip::tcp::socket sock (iOS); Socket Object Acceptor.accept (sock); Blocking wait for socket connection Std::cout << "client:"; std::cout << Sock.remote_endpoint (). Address () << Std::endl; Sock.write_some (Boost::asio::buffer ("Hello client ..."); Send Data}}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 a_timer{private:int count, count_max;boost::function<void () > F;// No parameter no return can be called with Boost::asio::d Eadline_timer t; Timer public:template<typename F>a_timer (boost::asio::io_service &ios, int x, F func): F (func), Count_max (x), Count (0), T (iOS, boost::p osix_time::millisec) {t.async_wait (Boost::bind (&a_timer::call_func, this, _1)); Register callback function}void call_func (const Boost::system::error_code &e) {if (Count > Count_max) {return;} ++count;f (); T.expires_at (T.expires_at () + boost::p osix_time::millisec) t.async_wait (Boost::bind (&a_ Timer::call_func, this, _1)); Start Timer again}};void client (Boost::asio::io_service &ios) {try{std::cout << "client start ..." << Std::endl ; Boost::asio::ip::tcp::socket sock (iOS); Boost::asio::ip::tcp::endpoint EP (Boost::asio::ip::address::from_string ( "127.0.0.1"), 6688); Sock.connect (ep); Socket connected to Endpoint std::vector<char> str (0); Sock.read_some (Boost::asio::buffer (str)); Receive data std::cout << recive from << sock.remote_endpoint (). Address () << ":"; std::cout << &s Tr[0] << Std::endl;} catch (std::exception& e) {std::cout << e.what () << Std::endl;}} int main () {Boost::asio::io_service Ios;a_timer t (iOS, 5, boost::bind (client, Boost::ref (iOS))); Ios.run (); return 0;}

  

C + + synchronous socket processing

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.