intclient_end_point(){ // Step 1. Assume that the client application has already // obtained the IP-address and the protocol port number. std::string raw_ip_address = "127.0.0.1"; unsignedshort port_num = 3333; // Used to store information about error that happens // while parsing the raw IP-address. boost::system::error_code ec; // Step 2. Using IP protocol version independent address // representation. asio::ip::address ip_address = asio::ip::address::from_string(raw_ip_address, ec); if (ec.value() != 0) { // Provided IP address is invalid. Breaking execution. std::cout << "Failed to parse the IP address. Error code = " << ec.value() << ". Message: " << ec.message(); return ec.value(); } // Step 3. asio::ip::tcp::endpoint ep(ip_address, port_num); // Step 4. The endpoint is ready and can be used to specify a // particular server in the network the client wants to // communicate with. return0; }
服务端
创建一个用于接受任意IP的endpoint,需要指定监听的端口。
intserver_end_point(){ // Step 1. Here we assume that the server application has //already obtained the protocol port number. unsignedshort port_num = 3333; // Step 2. Create special object of asio::ip::address class // that specifies all IP-addresses available on the host. Note // that here we assume that server works over IPv4 protocol. asio::ip::address ip_address = asio::ip::address_v4::any(); //asio::ip::address_v6::any()也可 // Step 3. asio::ip::tcp::endpoint ep(ip_address, port_num); // Step 4. The endpoint is created and can be used to // specify the IP addresses and a port number on which // the server application wants to listen for incoming // connections. return0; }
intconnect_to_end(){ // Step 1. Assume that the client application has already // obtained the IP address and protocol port number of the // target server. std::string raw_ip_address = "127.0.0.1"; unsignedshort port_num = 3333; try { // Step 2. Creating an endpoint designating // a target server application. asio::ip::tcp::endpoint ep(asio::ip::address::from_string(raw_ip_address), port_num); asio::io_context ios; // Step 3. Creating and opening a socket. asio::ip::tcp::socket sock(ios, ep.protocol()); // Step 4. Connecting a socket. sock.connect(ep); } // Overloads of asio::ip::address::from_string() and // asio::ip::tcp::socket::connect() used here throw // exceptions in case of error condition. catch (system::system_error& e) { std::cout << "Error occured! Error code = " << e.code() << ". Message: " << e.what(); return e.code().value(); } }
intaccept_new_connection(){ // The size of the queue containing the pending connection // requests. constint BACKLOG_SIZE = 30; // tcp缓存的连接数 // Step 1. Here we assume that the server application has // already obtained the protocol port number. unsignedshort port_num = 3333; // Step 2. Creating a server endpoint. asio::ip::tcp::endpoint ep(asio::ip::address_v4::any(), port_num); asio::io_context ios; try { // Step 3. Instantiating and opening an acceptor socket. asio::ip::tcp::acceptor acceptor(ios, ep.protocol()); // 创建的同时open了,相当于 // asio::ip::tcp::acceptor acceptor(ios); // acceptor.open(ep.protocol(), ec); // Step 4. Binding the acceptor socket to the // server endpint. acceptor.bind(ep); // Step 5. Starting to listen for incoming connection // requests. acceptor.listen(BACKLOG_SIZE); // Step 6. Creating an active socket. asio::ip::tcp::socket sock(ios); // Step 7. Processing the next connection request and // connecting the active socket to the client. acceptor.accept(sock); } catch (system::system_error& e) { std::cout << "Error occured! Error code = " << e.code() << ". Message: " << e.what(); return e.code().value(); } }
voiduse_stream_buffer(){ asio::streambuf buf; std::ostream output(&buf); // Writing the message to the stream-based buffer. output << "Message1\nMessage2"; // Now we want to read all data from a streambuf // until '\n' delimiter. // Instantiate an input stream which uses our // stream buffer. std::istream input(&buf); // We'll read data into this string. std::string message1; std::getline(input, message1); // Now message1 string contains 'Message1'. }