views:

636

answers:

2

I'm porting some code from BSD sockets to Winsock, and I'm not sure how to handle the case below.

My original application runs a select on both stdin and the network socket:

FD_SET(sock, &fd);
FD_SET(0, &fd);
...
if (select(..., &fd, ... )...)

Trying to run this in Winsock gives an error 10038 (WSAENOTSOCK), which makes sense, since what was file handle 0 in Linux (stdin) is not a socket (more precisely: a SOCKET type) in Windows.

Is there an easy way to port this test to Windows sockets?

+1  A: 

I'd love to be corrected, but as far as I know, Winsock does not extend beyond the realm of sockets. That is, Unix's "everything is a file" philosophy for the select(), read(), write(), etc system calls is not there in Winsock.

I'm sure you can do something similar with just the Win32 API working on socket and console handles, but it's not going to look much like Winsock (or BSD) anymore.

Dave Ray
I think you're right, but I'm hoping not; that's why I've asked. The problem is that I need to scan two different input sources, and attend to the first one that has data. select() in Unix makes it easy; I don't know how to do this in Windows, though (and I don't want cygwin).
Mikeage
Damn. I was going to suggest cygwin next. :)
Dave Ray
I'm porting an application that already compiles with cygwin, but I'd like a MinGW solution, as well. Being tied to a particular cygwin1.dll on a system with many is never fun.
Mikeage
+1  A: 

Winsock's select() only works with sockets. A more 'Windows-y' alternative would be to use Asynchronous I/O on both handles and then WaitForMultipleObjects.

Max Caceres
thanks Max. I'll try this today. It's a bit of a shame, though; I was hoping to have a winsock porting layer; it seems like this is going to require a bit more of a rewrite that I'd envisioned. Oh well.
Mikeage
You can not use Overlapped IO on a handle unless it has been created in a special way. In particular this means that you will not be able to use overlapped IO on inherited handles, such as the three standard streams.