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 ;}