Article 1900 of microsoft.public.win32.programmer.kernel: Patrik Blomberg wrote in article <01bcbf8b$7fd18c50$e50279c0@pbgntw>... > > > I am seeing some weird behavior in NT trying to use ReadFile and WriteFile > > > with a socket handle. I've used socket handles from socket() and from > > > accept(). They work fine when used with send() and recv(). > > > > > > When I use WriteFile or ReadFile synchronously, I get error 87 (The > > > parameter is invalid.) I know that the other parameters are fine, so it > > > must be something with the socket handle. > Socket handles can indeed be treated as file handles in calls to > ReadFile and WriteFile, for both NT and 95 Absolutely. I wrote a large semi-real time data gathering application that used ReadFile/WriteFile almost exclusively on sockets (the only exception involved broadcast UDP datagrams). > The described problem seems to have something to do with how > the socket was initializied when set up for connect or listen. That is correct but insufficiently detailed. By default when you open a socket it is opened for overlapped I/O. Given that Mr. Blomberg claims to be trying to use WriteFile synchronously, we may assume that he is passing in NULL for the overlapped structure. The "Parameter Invalid" error is complaining about that, which is correct. Handles opened for overlapped must always have an overlapped structure in the WriteFile call. The solution is somewhat obscure but easy to implement. To quote from the MSVC files, Use the following to open a nonoverlapped socket handle: SOCKET s; int optionValue = SO_SYNCHRONOUS_NONALERT; int err; err = setsockopt( INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE, (char *)&optionValue, sizeof(optionValue)); if (err != NO_ERROR) { /* failed for some reason... */ } s = socket( ... ); Note that the socket handle passed to setsockopt is ignored for this option.