关于使用asio发送网络数据的优化。

来源:互联网 发布:餐厅预订软件 编辑:程序博客网 时间:2024/06/09 03:07
关于使用asio发送网络数据的优化。


inline bool Axis::send_data()
{
progress_timer t;
//while(true)
try
{
io_service ios;


ip::tcp::endpoint ep(ip::tcp::v4(),6688);
ip::tcp::acceptor acceptor(ios,ep);




// cout<<"1"<<endl;


ip::tcp::iostream tcp_stream;
//cout<<"2"<<endl;
acceptor.accept(*tcp_stream.rdbuf());
//cout<<"3"<<endl;

//mutex::scoped_lock lock(io_str_data);
tcp_stream<<str_data;
//str_data=" ";
cout<<"--------------------------------time elapsed :"<<t.elapsed()<<endl;
// cout<<endl;
// cout<<"thanks to use the net client!"<<endl;
return true;
}
catch(...)
{
return false;
}
}


关于上述代码的优化问题,


上面的这个函数,执行时要花0.5秒左右的时间,当我把它改成下面的代码时,花时间只有几幑秒了。当我看到接受端像法拉利马达一样快的显示下面函数发过来的数据时,我的心都陶醉了!
忙了一天,这个优化是最让我兴奋的了。


inline bool Axis::send_data()
{
progress_timer t;
//while(true)
try
{
static io_service ios;


static ip::tcp::endpoint ep(ip::tcp::v4(),6688);
static ip::tcp::acceptor acceptor(ios,ep);




// cout<<"1"<<endl;


ip::tcp::iostream tcp_stream;
//cout<<"2"<<endl;
acceptor.accept(*tcp_stream.rdbuf());
//cout<<"3"<<endl;

//mutex::scoped_lock lock(io_str_data);
tcp_stream<<str_data;
//str_data=" ";
cout<<"--------------------------------time elapsed :"<<t.elapsed()<<endl;
// cout<<endl;
// cout<<"thanks to use the net client!"<<endl;
return true;
}
catch(...)
{
return false;
}
}
原创粉丝点击