我努力的TCP-Server,該服務器讀取數據,處理它們並把他們送回。之後,它將等待新的數據。我的問題是,服務器第一次發送數據時工作正常。第二次,程序停留在run_one()
-loop。加速TCP async_receive_some不讀第二次
size_t m_lengthReceive;
io_service m_ioService;
std::vector<unsigned char> m_vectorBuffer;
unsigned char m_bufferReceive[128];
void SyncServer::initialize(){
m_acceptor = shared_ptr<tcp::acceptor>(
new tcp::acceptor(m_ioService,
tcp::endpoint(tcp::v4(), m_port)));
m_acceptor->set_option(tcp::acceptor::reuse_address(true));
m_sock = shared_ptr<tcp::socket>(new tcp::socket(m_ioService));
m_acceptor->accept(*m_sock, m_ec);
}
void SyncServer::tcpReceiveVector(){
boost::system::error_code ec;
m_sock->async_read_some(buffer(m_bufferReceive),
boost::bind(&SyncServer::tcpReceiveHandler, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
do{
m_ioService.run_one();
}while(m_lengthReceive == 0);
}
void SyncServer::tcpReceiveHandler(const boost::system::error_code& ec,
size_t size){
if(size > 0 && !ec){
m_sock->cancel();
m_lengthReceive = size;
m_vectorBuffer.resize(m_lengthReceive);
int i = 0;
for(std::vector<unsigned char>::iterator it = m_vectorBuffer.begin();
it != m_vectorBuffer.end(); ++it){
*it = m_bufferReceive[i];
++i;
}
}else{
m_lengthReceive = 0;
m_sock->async_read_some(buffer(m_bufferReceive),
boost::bind(&SyncServer::tcpReceiveHandler, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
}
std::vector<unsigned char> SyncServer::getVectorBuffer(){
return m_vectorBuffer;
}
void SyncServer::openConnection(){
if(!m_sock->is_open())
m_sock->open(tcp::v4());
m_lengthReceive = 0;
}
void SyncServer::closeConnection(){
m_sock->close();
}
main(){
m_tcpServer = shared_ptr<SyncServer>(new SyncServer(m_tcpPort));
m_tcpServer->initialize();
while(1){
m_tcpServer->openConnection();
m_tcpServer->tcpReceiveVector();
vector = m_tcpServer->getVectorBuffer();
//do something with vector
m_tcpServer->tcpSend(vector); //this works fine
m_tcpServer->closeConnection();
}
}
更新的代碼:(?)
void SyncServer::tcpReceiveHandler(const boost::system::error_code& ec,
size_t size){
if(ec){
std::cout<< ec << std::endl;
}else{
if(size > 0){
m_sock->cancel();
m_lengthReceive = size;
m_vectorBuffer.resize(m_lengthReceive);
int i = 0;
for(std::vector<unsigned char>::iterator it = m_vectorBuffer.begin();
it != m_vectorBuffer.end(); ++it){
*it = m_bufferReceive[i];
++i;
}
}else{
m_lengthReceive = 0;
m_sock->async_read_some(buffer(m_bufferReceive),
boost::bind(&SyncServer::tcpReceiveHandler, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
}
}
解決了第一個問題。現在我得到一個錯誤107'not_connected'。現在我會試着找出問題所在。謝謝。 – cxd
在第一個成功完成後,您仍然沒有開始新的'async_receive()',據我所知。所以一旦你收到了一些東西,你就可以處理它,把'm_lengthReceive'設置爲0而不是'run_one()'循環。然後你發送一些東西並關閉連接,而不需要客戶給你發送任何東西。基本上,如果你想多次接收,你需要在處理程序的'if(size> 0)'路徑中有'async_receive()',而不僅僅是'else'。 – DeVadder
但我在我的主體中有一段時間(1)' - 圈,我每次都開始新的接收。這不會有幾乎相同的效果? – cxd