Boost Library at work (29) network client Four _ learning boost

Source: Internet
Author: User

What do you want to do when the client connects to the server? In fact, like a person to see a doctor, first to hang a number, and then according to the line number to see a doctor, and then according to the doctor's prescription for treatment. Therefore, in the client and server design, there is generally an important principle, that is, as a client to actively launch data to the server, let the server know what the client is to do. Like the port on the same server, can provide many kinds of client connections, such as HTTP server, also faced with a lot of different browser connections, and the server to distinguish between different browsers to do different things, then need to know what the browser to do, so the browser after the successful connection, First send data to the server, stating the version of the Protocol, the name of the browser, what data can be received, and so on. In this way, the server will be differentiated according to the client's situation, and placed in different task processing queues, so that the server is compatible with different versions of the client situation, to achieve backward compatibility of all previous versions, to achieve smooth forward upgrade function. In the network by server and client design, the server is often designed to consider the upgrade time is relatively long, and the client is ready to be replaced or upgraded. This causes the server to be compatible with different versions in order to accommodate different user requirements. For example, the current mobile system updates very quickly, the mobile system developed in the app is often different OS version, the function is also a difference, but the user will be distributed in different OS versions, resulting in the client also has a new and old points, In Android version 1.6 is the 1.0 version of the client, while in Android version 2.0 is the 2.0 version of the client, in the Android version 4.2 is the 3.0 version of the client. In this case, for different versions of the user, also continue to use the services provided, then you have to use a different compatibility policy on the server to maintain the satisfaction of old and new customers, so that the old customers continue to be your customers, because the loss of an old customer, cost three times times to develop a new customer. In software design and hardware design, the biggest difference is that the software design needs to be compatible with the old version, need to maintain and upgrade, maybe this is the software can not appear on the hardware IC industry reasons. Here's a look at the example of sending the data, the following code:

Boost_022.cpp:Defines the entry point for the console application. #include "stdafx.h" #include <boost/asio/ip/tcp.hpp > #include <boost/asio.hpp> #include <iostream
 
> #include <string> bool Sendrecvdata (boost::asio::ip::tcp::socket& socket);
Test Network Service queries, connections.
Software developer: Cai Junsheng 2013-06-10//qq:9073204 void Test (void) {//To identify a query endpoint object.
Boost::asio::ip::tcp::resolver::queryquery ("www.boost.org", "http"); Std::cout << "host_name:" << query.host_name () << "service_name:" << query.service_name ()
 
;< Std::endl;
Defines the IO service object.
 
Boost::asio::io_service Ioservice;
Defines the interpretation object.
Boost::asio::ip::tcp::resolverresolver (Ioservice);
Perform a domain name or service explanation to generate the data format used inside the socket.
Boost::asio::ip::tcp::resolver::iteratoriterator = resolver.resolve (query);
Boost::asio::ip::tcp::resolver::iteratoritend;
 
if (iterator = = Itend) {return;}
After the explanation is successful, the interpreted IP address and port are displayed.
Boost::asio::ip::tcp::endpointendpoint = Iterator->endpoint (); std:: cout << "hOST_IP: "<< endpoint.address () <<" Port: <<endpoint.port () << Std::endl;
Try to connect to the server.
Boost::asio::ip::tcp::socketsocket (Ioservice);
 
Boost::system::error_codeerrorcode = Boost::asio::error::host_not_found;
Socket.close (); Socket.connect (*iterator,errorcode);
Create a socket sync connection, that is, a blocking type.
     if (errorcode) {//connection error.
Return
 
} std::cout << "Connectsuccess" <<std::endl; if (!
 
Sendrecvdata (socket)) {return;}
Send and receive data with the server.
Software developer: Cai Junsheng 2013-06-11//qq:9073204 bool Sendrecvdata (boost::asio::ip::tcp::socket& socket) {//Send data to server.
Constructs an HTTP request.
    BOOST::ASIO::STREAMBUF request;
    Std::ostreamrequestpacket (&request);
    Requestpacket << "Get" << "http://www.boost.org/" << "http/1.0\r\n";
    Requestpacket << "Host:" << "www.boost.org" << "\ r \ n";
    Requestpacket << "Accept: */*\r\n";
 
    Requestpacket << "connection:close\r\n\r\n";
    Sends data through the socket. Boost::asio::write (SOcket,request);
Receive server response data.
BOOST::ASIO::STREAMBUF response;
 
Boost::asio::read_until (Socket,response, "\ r \ n");
Analyze the response data.
Std::istreamresponsepacket (&response);
Std::string strhttpversion;
Responsepacket >>strhttpVersion;
unsigned int istatuscode;
 
Responsepacket >>iStatusCode;  
Std::string strstatusmessage;
 
Std::getline (Responsepacket,strstatusmessage);
The output explains the data. Std::cout << "Response: << strhttpversion <<" "<< istatuscode <<" "<< St
 
rstatusmessage<< Std::endl;
return true;
 
int _tmain (int argc, _tchar* argv[]) {Test ();
 
System ("pause");
return 0; }


In this example, a Boost::asio::streambuf object is used to construct the sending packet, and then the data is sent through the network socket object via the Boost::asio::write function, and finally, in order to receive the server response data, construct boost :: Asio::streambuf object to save the data, through the Boost::asio::read_until function to wait for the server to arrive until the condition is over. After receiving the data, the Std::istream object can be used to interpret the response data, and then output the corresponding data to the control side window.

Although this example is short, it is more easily understood and maintained by dividing the received and sent code into a function Sendrecvdata. In the software development, each function does not exceed screen one screen, is the most ideal function, otherwise maintenance cost increases many, whenever the developer looks at the code, then rolls the screen, the thought rolls away. And this can reduce the bug to the lowest degree, greatly improve the code quality, improve the software development efficiency.

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.