"Eric Pouech" pouech-eric@wanadoo.fr wrote:
<title>File management</title>
<para>
- With time, Windows API comes closer to the old Unix paradigm "Everything
- is a file". Even if it grew better over the years, it's still not 100%
- there (for example, you cannot use <function>ReadFile()</function> over
- a socket handle).
Is it really true?
From MSDN:
BOOL ReadFile( HANDLE hFile, LPVOID lpBuffer, DWORD nNumberOfBytesToRead, LPDWORD lpNumberOfBytesRead, LPOVERLAPPED lpOverlapped );
Parameters hFile [in] Handle to the file to be read. The file handle must have been created with the GENERIC_READ access right. For more information, see File Security and Access Rights. For asynchronous read operations, hFile can be any handle opened with the FILE_FLAG_OVERLAPPED flag by the CreateFile function, or a socket handle returned by the socket or accept function.
Dmitry Timoshkov a écrit :
"Eric Pouech" pouech-eric@wanadoo.fr wrote:
<title>File management</title>
<para>
- With time, Windows API comes closer to the old Unix paradigm "Everything
- is a file". Even if it grew better over the years, it's still not 100%
- there (for example, you cannot use <function>ReadFile()</function> over
- a socket handle).
Is it really true?
From MSDN:
BOOL ReadFile( HANDLE hFile, LPVOID lpBuffer, DWORD nNumberOfBytesToRead, LPDWORD lpNumberOfBytesRead, LPOVERLAPPED lpOverlapped );
Parameters hFile [in] Handle to the file to be read. The file handle must have been created with the GENERIC_READ access right. For more information, see File Security and Access Rights. For asynchronous read operations, hFile can be any handle opened with the FILE_FLAG_OVERLAPPED flag by the CreateFile function, or a socket handle returned by the socket or accept function.
which means that ReadFile() only works on socket for async reads, not sync reads. I'll precise this. A+
"Eric Pouech" pouech-eric@wanadoo.fr wrote:
For asynchronous read operations, hFile can be any handle opened with the FILE_FLAG_OVERLAPPED flag by the CreateFile function, or a socket handle returned by the socket or accept function.
which means that ReadFile() only works on socket for async reads, not sync reads. I'll precise this.
Did you actually test this? We need a knowledge, not a speculation.
Dmitry Timoshkov a écrit :
"Eric Pouech" pouech-eric@wanadoo.fr wrote:
For asynchronous read operations, hFile can be any handle opened with the FILE_FLAG_OVERLAPPED flag by the CreateFile function, or a socket handle returned by the socket or accept function.
which means that ReadFile() only works on socket for async reads, not sync reads. I'll precise this.
Did you actually test this? We need a knowledge, not a speculation.
I did. See attached test and it fails on XP. Note that ReadFile works on socket (overlapped op), while WriteFile (synchronous operation) doesn't. A+
"Eric Pouech" pouech-eric@wanadoo.fr wrote:
For asynchronous read operations, hFile can be any handle opened with the FILE_FLAG_OVERLAPPED flag by the CreateFile function, or a socket handle returned by the socket or accept function.
which means that ReadFile() only works on socket for async reads, not sync reads. I'll precise this.
Did you actually test this? We need a knowledge, not a speculation.
I did. See attached test and it fails on XP. Note that ReadFile works on socket (overlapped op), while WriteFile (synchronous operation) doesn't.
Your test passes on win2k SP4 for me, but hangs on win98. Uncommented WriteFile call failes and doesn't change a previous error value, but that might be due to different reasons: missing OVERLAPPED pointer, incompatible socket open mode, or something else.
Dmitry Timoshkov a écrit :
"Eric Pouech" pouech-eric@wanadoo.fr wrote:
For asynchronous read operations, hFile can be any handle opened with the FILE_FLAG_OVERLAPPED flag by the CreateFile function, or a socket handle returned by the socket or accept function.
which means that ReadFile() only works on socket for async reads, not sync reads. I'll precise this.
Did you actually test this? We need a knowledge, not a speculation.
I did. See attached test and it fails on XP. Note that ReadFile works on socket (overlapped op), while WriteFile (synchronous operation) doesn't.
Your test passes on win2k SP4 for me, but hangs on win98. Uncommented WriteFile call failes and doesn't change a previous error value, but that might be due to different reasons: missing OVERLAPPED pointer, incompatible socket open mode, or something else.
yeahh, you're right. I found what's going wrong. By default, WS2 opens the socket in overlapped mode, hence the error in Read/Write File (while the send/recv and other WS functions should handle this gracefully). BTW, our WS implementation doesn't use the right open mode by default (which is overlapped) A+
On Sun, 13 Mar 2005 19:21, Dmitry Timoshkov wrote:
"Eric Pouech" pouech-eric@wanadoo.fr wrote:
For asynchronous read operations, hFile can be any handle opened with the FILE_FLAG_OVERLAPPED flag by the CreateFile function, or a socket handle returned by the socket or accept function.
which means that ReadFile() only works on socket for async reads, not sync reads. I'll precise this.
It's not quite correct. The following sequence of code will cause all sockets created subsequently with socket() to be usable with ReadFile() and WriteFile() without overlapped calls. I can confirm that I've done this many times (using sockets of this variety is a good way of porting UNIX apps which use select on a pipe).
int optval = SO_SYNCHRONOUS_NONALERT;
setsockopt( INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE, (char *) &optval, sizeof optval );
According to MSDN you can also use WSASocket in Windows Sockets version 2.0 or later to create sockets that do this by not using the WSA_FLAG_OVERLAPPED flag.