简体   繁体   English

Boost ASIO套接字消耗文件描述符

[英]Boost ASIO socket consumes file descriptors

I'm using Boost ASIO sockets for communicating with some remote devices under linux, but i have a problem when the endpoint is not reachable. 我正在使用Boost ASIO套接字与linux下的某些远程设备进行通信,但是当端点不可访问时,我遇到了问题。 First of all, here's the portion of code that shows this issue: 首先,这是显示此问题的代码部分:

try {
   if(mysocket == NULL)
   {
      mysocket = new boost::asio::ip::tcp::socket(io_service);
   }
   mysocket->connect(boost::asio::ip::tcp::endpoint(boost::asio::ip::address::from_string("192.168.0.12"), 1));
   printf("connected\n");
   return 0;
}
catch (std::exception &e)
{
   boost::system::error_code ec;
   mysocket->close(ec);
   delete mysocket;
   mysocket = NULL;
   printf("not connected %s\n", e.what());
}

By using this piece of code inside my class I get an increasing number of file descriptors of type eventfd, until all the available fds are used and the application crashes. 通过在我的类中使用这段代码,我得到了越来越多的eventfd类型的文件描述符,直到所有可用的fds被使用并且应用程序崩溃为止。 Is there any problem with the code above? 上面的代码有什么问题吗? Why boost is not closing the file descriptors? 为什么boost不关闭文件描述符? I even delete the socket! 我什至删除套接字! Thanks in advance! 提前致谢!

The problem was due to not releasing resources, such as boost sockets. 问题是由于未释放资源(例如Boost套接字)。 Those sockets left open files, which gradually reached the open file limit in my system, thus trying to open additional files resulted in an error. 这些套接字留下了打开的文件,这些文件逐渐达到了我系统中的打开文件限制,因此尝试打开其他文件会导致错误。 The actual exception was thrown by the boost UUID generator, which could not open files! 实际的异常由boost UUID生成器抛出,无法打开文件!

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

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