I’m using Boost 1.45 ASIO to handle some socket connections in an application that runs on both Windows and Mac. On Windows, the following code does not cause any errors, and my sockets are closed cleanly. On Mac though, both the shutdown and (if I comment it out) the close functions give me “Bad file descriptor” errors. Up until I call this code, the sockets work fine. But as soon as I call shutdown or close, I get the error. Any ideas what could be going on?
if(socket.is_open())
{
socket.shutdown(socket.both);
socket.close();
}
Unless you have a good reason for doing this, I suggest letting the
socket::~socket()destructor close the underlying native file descriptor. If you are concerned about leaking descriptors, use a tool like valgrind to analyze your program.