What causes the ENOTCONN error?

前端 未结 5 1223
伪装坚强ぢ
伪装坚强ぢ 2021-02-01 05:20

I\'m currently maintaining some web server software and I need to perform a lot of I/O operations. The read(), write(), close() and

5条回答
  •  佛祖请我去吃肉
    2021-02-01 06:00

    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.

提交回复
热议问题