简体   繁体   English

Boost :: asio阻止Tcp服务器混乱

[英]Boost::asio Blocking Tcp Server confusion

I have been stuck with this prolem for the past 5 hours or so.. Sorry if the question is too obvious/noob but I am noob myself when it comes to boost::asio or tcp/ip in general. 在过去的5个小时左右的时间里,我一直陷在这个困境中。.很抱歉,如果这个问题太明显/新手,但对于boost :: asio或tcp / ip来说,我本人也不是。

So here is the problem. 所以这就是问题所在。 I am trying to modify the blocking tcp server example : 我正在尝试修改阻塞的TCP服务器示例

void session(socket_ptr sock)
{
  try
  {
    for (;;)
    {
      char data[max_length];

      boost::system::error_code error;
      size_t length = sock->read_some(boost::asio::buffer(data), error);
      if (error == boost::asio::error::eof)
        break; // Connection closed cleanly by peer.
      else if (error)
        throw boost::system::system_error(error); // Some other error.

      boost::asio::write(*sock, boost::asio::buffer(data, length));
    }
  }
  catch (std::exception& e)
  {
    std::cerr << "Exception in thread: " << e.what() << "\n";
  }
}

What I want to do is save all the chunks or the read_some method into some buffer for this example std::string and then do something with them before sending a reply back: 我想要做的就是将所有块或read_some方法保存到此示例std :: string的某个缓冲区中,然后在发送回信之前对它们进行一些处理:

const int max_length = 10;

typedef boost::shared_ptr<tcp::socket> socket_ptr;

void session(socket_ptr sock)
{
    std::string dataReceived = "";
    try
    {
        for (;;)
        {
            char data[max_length] = {'\0'};
            boost::system::error_code error;
            size_t length = sock->read_some(boost::asio::buffer(data), error);
            dataReceived += std::string(data, length);
            if (error == boost::asio::error::eof)
                break; // Connection closed cleanly by peer.
            else if (error)
                throw boost::system::system_error(error); // Some other error.
            //boost::asio::write(*sock, boost::asio::buffer(&dataReceived[0], dataReceived.size()));
        }
        boost::asio::write(*sock, boost::asio::buffer(&dataReceived[0], dataReceived.size()));
    }
    catch (std::exception& e)
    {
        std::cerr << "Exception in thread: " << e.what() << "\n";
    }
}

When I remove the write from inside the loop the client hangs. 当我从循环内部删除write ,客户端挂起。 The dataReceived has all the data inside. dataReceived具有所有内部数据。 The buffer is deliberately small so that read_some is called more than once. 缓冲区故意较小,因此read_someread_some调用。 In the debugger the control never goes to the write method outside of the loop. 在调试器中,控件永远不会在循环外转到write方法。 I am probably doing something very wrong. 我可能做错了什么。 But I am unable to find out what. 但是我不知道是什么。

Side question: 附带问题:

What would be the simplest solution to have a socket connection between some UI and a backend process? 在某些UI和后端进程之间建立套接字连接的最简单解决方案是什么?

Probably it hangs because client waits for server reply and don't send new data. 它可能挂起,因为客户端等待服务器回复并且不发送新数据。

Also, server will exit form loop only when connection is closed, and there is nowhere to write data. 此外,仅当连接关闭且无处write数据时,服务器才会退出表单循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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