Calling shutdown and closesocket twice on same socket

后端 未结 3 1285
暗喜
暗喜 2021-01-03 12:18

In my application I call shutdown and closesocket functions twice on the same socket. I know this is not right thing to do and I have to ensure tha

3条回答
  •  礼貌的吻别
    2021-01-03 12:43

    First one must distinguish two different things:

    1. Performing operations on a socket, some of which may bring it in an unusable state.
    2. Terminating the socket handle.

    Calling shutdown with parameter 2 (which is SD_BOTH) is (1). That is, you flush all the pending out buffer, plus discard all the receive buffer. So that you can't read/write anymore. However you still hold a valid socket handle. You still may query/set its options if you want. For instance, one could call getpeername on it to discover the address you were connected to. Also, depending on the implementation, calling shutdown again doesn't have to result in an error. Maybe it has an accumulative effect.

    On the other hand calling closesocket is (2). Once you called it - you can't do anything with that socket handle. It's now invalid.

    If you socket has a value of 1500 - it will still have it, even after a call to closesocket. Because it's just a variable. It's like asking what value will have the pointer after you delete it.

    However this socket value (1500) is no more valid. You can't call any socket function with this value.

    Moreover, if you create another socket meanwhile - it's very likely to receive the same number. Here you may find yourself in a more severe problem - doing actions on another socket without noticing it.

    For some it's a good practice assigning INVALID_SOCKET value to a socket variable right after you call closesocket on it.

    P.S. Perhaps calling shutdown + closesocket don't fail specifically because you're closing another socket.

提交回复
热议问题