我们还需要另一端用于通信,即一个请求服务器的客户端。基本结构与我们为服务器所做的相同。
#include <iostream>
#include <boost/asio.hpp>
using namespace boost::asio;
using ip::tcp;
using std::string;
using std::cout;
using std::endl;
这些导入与我们为服务器所做的相同。没有什么新的。
int main() {
boost::asio::io_service io_service;
//创建socket
tcp::socket socket(io_service);
//连接
socket.connect( tcp::endpoint( boost::asio::ip::address::from_string("127.0.0.1"), 1234 ));
// 客户端请求/消息
const string msg = "Hello from Client!\n";
boost::system::error_code error;
boost::asio::write( socket, boost::asio::buffer(msg), error );
if( !error ) {
cout << "客户端发送了hello消息!" << endl;
}
else {
cout << "发送失败: " << error.message() << endl;
}
// 从服务器获取响应
boost::asio::streambuf receive_buffer;
boost::asio::read(so