The boost library is on the server of the working network (31 ).

Source: Internet
Author: User
Tags network function pconnect

The client has been introduced above, and then we need to develop the server-side program, because only the client has no server-side can't do anything and can't provide network services. More and more applications, such as HTTP servers, email servers, instant messaging servers, and game servers, are in the network server field. Because the hardware resources on the server are relatively expensive, it is more appropriate to use C ++ to increase the data volume for processing a large number of services. It can be said that it is the best cost-effective, the performance of languages such as Java is lower than that of C ++, and compilation and development are successful. Some people may say that today's hardware is already very cheap and there is no need to use a language like C ++? But if you think about it, the hardware improves the speed, but the speed is relative to the data volume and functional requirements, it is still a drop in the neck. For example, the software we developed 10 years ago is processing 1g databases, which is already very large. But today we have to deal with 100g or even g databases, however, hardware cannot meet the requirements. Especially in a mobile network environment like today, mobile phones are a relatively simple and confusing small software. Therefore, it is impossible to implement many complex functions and must be powerful in the server field, in this way, powerful functions can be provided. For example, voice recognition is implemented on the server. It can be seen that the development of servers has become more and more important in the software industry. How can we develop stable and reliable servers? Using the network function of the boost library is a good solution.

Let's take a look at the first part of the example and receive the connection to the server, as shown below:

// Server, which receives new connections and starts new connections to receive data. // Software developer: Cai junsheng 2013-06-30class cserver {public: // constructor, mainly providing Io services and ports. Cserver (boost: ASIO: io_service & ioservice, short sport): m_ioservice (ioservice), m_acceptor (ioservice, boost: ASIO: IP: TCP :: endpoint (boost: ASIO: IP: TCP: V4 (), Sport) {// create a new connection to receive the representation of the connected client. Boost: shared_ptr <cconnect> pconnect (New cconnect (m_ioservice); // prepare for the connection. M_acceptor.async_accept (pconnect-> getsocket (), boost: BIND (& cserver: handleaccept, this, pconnect, boost: ASIO: placeholders: Error ));} // receives the client connection event response. Void handleaccept (boost: shared_ptr <cconnect> pnewconnect, const boost: System: error_code & error) {If (! Error) {// if no error occurs, send and receive data from the connected connection. Pnewconnect-> Start (); // create a new connection for the next client to connect. Pnewconnect. Reset (New cconnect (m_ioservice); // prepare the connection. M_acceptor.async_accept (pnewconnect-> getsocket (), boost: BIND (& cserver: handleaccept, this, pnewconnect, boost: ASIO: placeholders: Error ));}} PRIVATE: // Io service boost: ASIO: io_service & m_ioservice; // receiver, used to receive new connections. Boost: ASIO: IP: TCP: acceptor m_acceptor;}; // int _ tmain (INT argc, _ tchar * argv []) {// create an IO service boost: ASIO: io_service ioservice; // create a server with port 9001. Cserver server (ioservice, 9001); // response Io service ioservice. Run (); 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.