Boost::asio 阻止 TCP 服务器混乱
在过去的 5 个小时左右的时间里,我一直被这个问题困扰。很抱歉,如果这个问题太明显/菜鸟,但当涉及到 boost::asio 或 tcp/ip 时,我自己就是菜鸟。
所以问题就在这里。我正在尝试修改 阻止 tcp 服务器example :
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";
}
}
我想要做的是将所有块或 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";
}
}
当我从循环内部删除 write
时,客户端挂起。 dataReceived
包含所有数据。缓冲区故意较小,以便多次调用 read_some
。在调试器中,控件永远不会转到循环之外的 write
方法。我可能做错了什么。但我无法找出什么。
附带问题:
在某些 UI 和后端进程之间建立套接字连接的最简单解决方案是什么?
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.
So here is the problem. I am trying to modify the blocking tcp server example :
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:
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. The dataReceived
has all the data inside. The buffer is deliberately small so that read_some
is called more than once. In the debugger the control never goes to the write
method outside of the loop. 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
可能它挂起是因为客户端等待服务器回复并且不发送新数据。
另外,只有当连接关闭时,服务器才会退出表单循环,并且没有地方可以
写入
数据。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.