同步服務端:
boost::timer tm;
io_service is;
ip::tcp::acceptor acceptor(is, ip::tcp::endpoint(ip::tcp::v4(), 8000));
for(;;) {
ip::tcp::socket so(is);
acceptor.accept(so);
char buf[501];
error_code ec;
so.read_some(buffer(buf), ec);
buf[500] = '\0';
cout << buf << endl;
cout << "all time:"<<tm.elapsed() << endl;
}
同步客戶端:
io_service is;
ip::tcp::socket socket(is);
ip::tcp::endpoint ep(ip::address_v4::from_string("192.168.3.33"), 8000);
socket.connect(ep);
error_code ec;
char buff[500];
memset(buff, '9', 500);
socket.write_some(buffer(buff, 500), ec);
return NULL;
異步服務端:
io_service is;
ip::tcp::acceptor ac(is, ip::tcp::endpoint(ip::tcp::v4(), 8000));
boost::shared_ptr<ip::tcp::socket> psocket(new ip::tcp::socket(is));
ac.async_accept(*psocket, boost::bind(accept_f, psocket, _1));
is.run();
void accept_f(boost::shared_ptr<ip::tcp::socket> pso, error_code ec)
{
std::cout << pso->remote_endpoint().address() << std::endl;
boost::shared_ptr<string> pstr(new string("999999999999999999999999"));
psocket->async_write_some(buffer(*pstr), boost::bind(write_f, pstr, _1, _2));
}
void write_f(boost::shared_ptr<std::string> pstr, error_code ec,
size_t bytes_transferred)
{
}
摘自 緣起