First you need to distinguish between two different things:
- Performing socket operations, some of which may lead to an unusable state.
- Completion of the socket descriptor.
The shutdown call with parameter 2 (which is equal to SD_BOTH ) is equal to (1). That is, you clear the entire incomplete buffer, and also delete the entire receive buffer. So you can no longer read / write. However, you still keep a valid socket descriptor. You can still request / set your parameters if you want. For example, you could call getpeername to open the address to which you are connected. In addition, depending on the implementation, the shutdown call should not again result in an error. Perhaps it has a cumulative effect.
On the other hand, the closesocket call is (2). Once you have called it, you cannot do anything with this socket descriptor. It is now invalid.
If your socket has a value of 1500, it will still receive it, even after calling closesocket . Because it is just a variable. He would like to ask what value the pointer will have after you delete .
However, this socket value (1500) is no longer valid. You cannot call any socket function with this value.
Also, if you create another socket in the meantime, it will most likely get the same number. Here you may encounter a more serious problem - doing actions on another socket without noticing it.
For some, it's a good practice to assign an INVALID_SOCKET value to a socket variable immediately after calling closesocket on it.
PS Perhaps the shutdown + closesocket does not work specifically because you are closing another socket.
source share