The client has been described above, and then it is necessary to develop the server-side program, because only the client is not a service-side can not be done, the provision of network services. Like the C + + language, more and more applications are in the domain of Web servers, such as HTTP servers, email servers, instant messaging servers and game servers, and so on. Because the server's hardware resources are more expensive, to improve the volume of data processing a large number of services, using C + + is more appropriate, you can say that the best cost-effective, use such as Java language performance than C + +, the use of compilation and development success is relatively high. Some people may say: "Today's hardware is very cheap, no need to use the language of C + +." But you think carefully, the hardware is improved speed, but the speed of increase relative to the amount of data and functional requirements, or a drop in the bucket. For example, 10 ago we developed software is processing 1G database, it is already very large, but today has to deal with 100G, or even 1000G of the database, and the hardware simply can not keep up with demand. Especially like today's mobile network environment, in the mobile phone there is a relatively simple, comparison of the small software, so it is impossible to do a lot of complex functions, must be in the server domain to do very strong, so as to provide strong features, such as speech recognition on the server to do. This shows that the development of the server in the software industry, become more and more important. How can we develop a stable and reliable server? Using the Boost library's network features is a good solution. Let's take a look at the first part of the example, receive the connection server part, as follows:
Server, primarily receives new connections and initiates new connections to receive data.
Software developer: Cai Junsheng 2013-06-30 class Cserver {public://constructor, which mainly provides IO services and ports. Cserver (boost::asio::io_service& ioservice, Short Sport): M_ioservice (Ioservice), M_acceptor (IoService, boost::a
Sio::ip::tcp::endpoint (BOOST::ASIO::IP::TCP::V4 (), sport)) {//Create a new connection to receive the incoming client representation.
boost::shared_ptr< cconnect > Pconnect (New CConnect (M_ioservice));
Do the connection preparation. M_acceptor.async_accept (Pconnect->getsocket (), Boost::bind (&cserver::handleaccept, this, PConnect, boost::
ASIO::p laceholders::error));
//Receive Client connection incoming event response. void Handleaccept (boost::shared_ptr< cconnect > pnewconnect, const boost::system::error_code& error) {if (
!error) {////If there are no errors, send and receive data to incoming connections.
Pnewconnect->start ();
Create a new connection in case the next client connects.
Pnewconnect.reset (New CConnect (M_ioservice));
Do the connection preparation. M_acceptor.async_accept (Pnewconnect->getsocket (), Boost::bind (&cserver::handleaccept, this, PNewConnect, Bo Ost::asio::p laceholders::error));
} Private://io service boost::asio::io_service& M_ioservice;
Receiver that is used to receive new connections in.
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 a port of 9001.
Cserver Server (Ioservice, 9001);
Response IO service ioservice.run ();
return 0;
}