Can you _close(), _read(), and _write() a socket on windows?

Viewed 1690

MSDN says closesocket() is the function to use. However, I couldn't help wondering if _close() will work also?

MSDN appears to say no in their description of SOCKET type: (http://msdn.microsoft.com/en-us/windows/ms740516(v=vs.80)):

In Winsock applications, a socket descriptor is not a file descriptor and must be used with the Winsock functions.

and more specifically from its note on renamed socket functions:

Sockets are represented by standard file descriptors in Berkeley Sockets, so the close function can be used to close sockets as well as regular files. While nothing in Windows Sockets prevents an implementation from using regular file handles to identify sockets, nothing requires it either. On Windows, sockets must be closed by using the closesocket routine. On Windows, using the close function to close a socket is incorrect and the effects of doing so are undefined by this specification.

However, even despite the above, some of the Windows file functions might work with sockets in practice:

Given that ReadFile and WriteFile work on sockets, I suspect _read and _write, for instance, might also work with sockets as well as file handles.

MSDN's overview of socket handles states:

A socket handle can optionally be a file handle in Windows Sockets 2. A socket handle from a Winsock provider can be used with other non-Winsock functions such as ReadFile, WriteFile, ReadFileEx, and WriteFileEx.

2 Answers
Related