如何检查 SSL 套接字是否关闭(异步)
我使用 boost asio 进行网络有一段时间了,但从未用于 SSL 套接字。现在我需要使用 SSL 套接字,它们工作得很好。但我无法找出套接字何时关闭(我通常这样做,就像处理常规套接字一样 - 在回调中使用 boost::asio::async_read_until()
时检查错误值 即使
这是一些相关的代码片段:
boost::asio::streambuf streambuf;
boost::asio::ssl::context sslctx(io_service, boost::asio::ssl::context::tlsv1);
boost::asio::ssl::stream<boost::asio::ip::tcp::socket> sock(io_service, sslctx);
void DoAsyncRead()
{
boost::asio::async_read_until(sock, streambuf, "\n", MyReadHandler);
}
void MyReadHandler(const boost::system::error_code& error, size_t bytes_transferred)
{
if (error) {
std::cout << "Read error: " << error.message() << std::endl;
} else {
// ...
}
}
我终止服务器或断开客户端连接,错误条件也永远不会成立。
I've been using boost asio for networking for some time, but never for SSL sockets. Now i'm required to use SSL sockets and they work pretty fine. But i am not able to find out when a sockets get closed (I usually did this as I did with regular sockets - checking the error value when using boost::asio::async_read_until()
in the callback function.
Here's some relevant code snippets:
boost::asio::streambuf streambuf;
boost::asio::ssl::context sslctx(io_service, boost::asio::ssl::context::tlsv1);
boost::asio::ssl::stream<boost::asio::ip::tcp::socket> sock(io_service, sslctx);
void DoAsyncRead()
{
boost::asio::async_read_until(sock, streambuf, "\n", MyReadHandler);
}
void MyReadHandler(const boost::system::error_code& error, size_t bytes_transferred)
{
if (error) {
std::cout << "Read error: " << error.message() << std::endl;
} else {
// ...
}
}
The error condition is never true, even if I kill the server, or drop the client connection. How can I track if the connection is closed?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
EOS 不是大多数 API 中的错误条件。它是返回的标记值而不是字节计数,通常为零 (Unix) 或 -1 (Java)。
EOS is not an error condition in most APIs. It is a sentinel value returned instead of a byte count, typically zero (Unix) or -1 (Java).