gpt4 book ai didi

c++ - Boost TCP async_receive_some 不会第二次读取

转载 作者:行者123 更新时间:2023-11-28 00:34:59 25 4
gpt4 key购买 nike

我尝试有一个 TCP 服务器,它正在读取数据、处理它们并将它们发回。之后它将等待新数据。我的问题是,服务器在第一次发送数据时工作正常。第二次,程序停留在 run_one() 循环中。

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));
}
}
}

最佳答案

在您的接收处理程序中,如果出现问题,您只会触发另一次读取(?)

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));
}
}

在这个回调上,你需要对下一次读取进行排队(通常如果没有问题,而不是在有问题的时候!)

关于c++ - Boost TCP async_receive_some 不会第二次读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21282192/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com