简体   繁体   中英

C++ no output, boost.asio

I'm about to write an IRCBot using Boost.Asio and I have the function getMsg:

std::string getMsg()
{
buffer.clear();         //make sure buffer is empty
buffer.resize(512);     //make sure it's big enough for 512char
socket.read_some(boost::asio::buffer(&buffer[0],buffer.size()));
std::size_t pos = buffer.find("PING :");
if(pos != std::string::npos)
{
sendMsg("PONG :" + buffer.substr(pos + 6));
}
return buffer;
}

In my main function when using std::cout << Text; I get an output, but when trying std::cout << "Hello", nothing seems to happen:

while(true)
{
std::string Text = Test.getMsg();
std::cout << Text;        //OUTPUT
}


while(true)
{
std::string Text = Test.getMsg();
std::cout << "TEST";      //NO OUTPUT ---- WHY?
}

The error you are asking about most likely occurs because you don't flush the stdout: std::cout << "TEST" << std::flush; This has nothing to do with boost::asio.

However your asio code also has a possible error: You are looking for PING : there in a single read call which might never be received within a single read call, due to the fact of how TCP works (it's a stream, not packets). If it's UDP socket it would work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM