What happens if she tries to close an already closed winsock connector?

What happens if I call closesocket (m_socket) twice? After the first call, the socket will be closed. Will something happen after the second call, which is bad for the program or application. or it will just return an error that I can ignore.

+4
source share
1 answer

See http://msdn.microsoft.com/en-us/library/windows/desktop/ms737582(v=vs.85).aspx

Note that the socket descriptor passed in the s parameter can be immediately reused by the system as soon as the closesocket function is started. As a result, it is unreliable to expect that further socket descriptor references passed in parameter s will fail with a WSAENOTSOCK error.

I would interpret this as a value, so you have to be careful not to close the socket twice when any intermediate code could do anything including a descriptor that includes opening files and other sockets, rather than a necessary blanket ban but actually it is fragile and you have to be coding to avoid the possibility. It might make sense to assign INVALID_SOCKET to the variable you are using to store the socket descriptor, allowing you to verify this before calling closesocket again or (even worse) to ensure that the side effect of this second call is not rejected.

+1
source

Source: https://habr.com/ru/post/1481697/


All Articles