I want to write a simple, small C++ RAII wrapper over a C socket.
The question is in what state is a socket considered initialized (from the perspective of RAII) and so eligible for release.
For instance, for a TCP client socket: if the socket
call succeed, but the connect
call failed, should close
be called?
This is just an example, I am interested in a general answer, something like:
- Each socket successfully created by
socket
must be closed.
or - There must be a close for each
connect
,listen
oraccept
.
The man pages for socket
& friends and close
are not very clear (or at least to me).
socket
oraccept
return a value other thanINVALID_SOCKET
and until youclose
the handle. – StaysINVALID_SOCKET
is a WinSock constant, @CaptainObvlious. In Unix tradition, Linux uses filedescriptors that are just indices into a zero-based array, so anything that is less than zero is a "faulty" return value from e.g.socket()
. Oh, and checking for negative values fails for WinSock on 64 bits, because there the socket handle type is an unsigned integer, if I remember correctly (it's been a while, I must admit). – Dope