【问题标题】:C++ no output, boost.asioC++ 没有输出,boost.asio
【发布时间】:2017-03-04 04:56:14
【问题描述】:

我即将使用 Boost.Asio 编写一个 IRCBot,并且我有 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;
}

在我的 main 函数中使用 std::cout

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


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

【问题讨论】:

  • 检查这是否有效:std::cout

标签: c++ string boost network-programming boost-asio


【解决方案1】:

您询问的错误很可能是因为您没有刷新标准输出:std::cout &lt;&lt; "TEST" &lt;&lt; std::flush; 这与 boost::asio 无关。

但是您的 asio 代码也有一个可能的错误:由于 TCP 的工作原理(它是一个流,不是数据包)。如果是 UDP 套接字,它会工作。

【讨论】:

    猜你喜欢
    • 2018-08-31
    • 2015-12-22
    • 2012-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多