I\'m currently maintaining some web server software and I need to perform a lot of I/O operations. The read()
, write()
, close()
and
It's because, at the moment of shutting() the socket, you have data in the socket's buffer waiting to be delivered to the remote party which has closed() or shutted down() its receiving socket. I don't finish understanding how sockets work, I am rather a noob, and I've failed to even find the files where this "shutdown" function is implemented, but seeing that there's practically no user manual for the whole sockets thing I started trying all possibilities until I got the error in a "controlled" environment. It could be something else, but after much trying these are the explanations I settled for:
I believe ENOTCONN is returned, because shutdown() is not supposed to return ECONNRESET or other more accurate errors.
It is wrong to assume that the other side “just” closed the connection. On the TCP-level, the other side can only half-close a connection (or abort it). The connection is ordinary fully closed if both sides do a shutdown() (or close()). If both side do that, shutdown() actually succeeds for both of them!
The problem is that shutdown() did not succeed in ordinary (half-)closing the connection, neither as the first one to close it, nor as the second one. – From the errors listed in the POSIX docs for shutdown(), ENOTCONN is the least inappropriate, because the others indicate problems with arguments passed to shutdown() (or local resource problems to handle the request).
So what happened? These days, a NAT device somewhere between the two parties involved might have dropped the association and sends out RESET packets as a reaction. Reset connections are so common for IPv4, that you will get them anywhere in your code, even masked as ENOTCONN in shutdown().
A coding bug might also be the reason. On a non-blocking socket, for example, a connect() can return 0 without indicating a successful connection yet.
Transport endpoint is not connected
The socket is associated with a connection-oriented protocol and has not been connected. This is usually a programming flaw.
From: http://www.wlug.org.nz/ENOTCONN
If you are sure that nothing on your side of the TCP connection is closing the connection, then it sounds to me like the remote side is closing the connection.
ENOTCONN
, as others have pointed out, simply means that the socket is not connected. This doesn't necessarily mean that connect
failed. The socket may well have been connected previously, it just wasn't at the time of the call that resulted in ENOTCONN
.
This differs from:
ECONNRESET
: the other end of the connection sent a TCP reset packet. This can happen if the other end is refusing a connection, or doesn't acknowledge that it is already connected, among other things.ETIMEDOUT
: this generally applies only to connect
. This can happen if the connection attempt is not successful within a system-dependent amount of time.EPIPE
can sometimes be returned by some socket-related system calls under conditions that are more or less the same as ENOTCONN
. For example, on some systems, EPIPE
and ENOTCONN
are synonymous when returned by send
.
While it's not unusual for shutdown
to return ENOTCONN
, since this function is supposed to tear down the TCP connection, I would be surprised to see close
return ENOTCONN
. It really should never do that.
Finally, as dwc mentioned, EBADF
shouldn't apply in your scenario unless you are attempting some operation on a file descriptor that has already been close
d. Having a socket get disconnected (i.e. the TCP connection has broken) is not the same as closing the file descriptor associated with that socket.
If you're sure you've connected properly in the first place, ENOTCONN
is most likely to be caused by either the fd
being closed on your end (perhaps in another thread?) while you're in the middle of a request, or by the connection dropping while you're in the middle of the request.
In any case, it means that the socket is not connected. Go ahead and clean up that socket. It's dead. No problem calling close()
or shutdown()
on it.