Question

I'm trying to write program which uses sockets to send data over network. Now I need input commands from console. I have select function for sockets. In linux you can use select to look for data input in sockets and in conolse. How I can get same functionality on visual c++?

When I insert console descriptor in select function it returns error 10038 (Socket operation on nonsocket.)

Was it helpful?

Solution

Windows does not support using select for arbitrary handles. In Linux and other forms of Unix, "all handles are equal", and it's not important whether they are handles for console, a socket or anything else.

Note that select in windows does not take a HANDLE, but a special SOCKET type of handle. See the definition of Windows' fd_set.

The easiest solution is probably to use a second thread to read the console input. There is certainly no way to include the console into your select call. There may be ways to convince Windows that a SOCKET is a waitable object, and use it for something like WaitForMultipleObjects, along with a console input handle - but I'm far from convinced (and I don't have a windows setup to try it out on).

OTHER TIPS

You can try ReadFile with console HANDLE. So, you can read console as a file. You can also read from/write to sockets with ReadFile and WrtieFile See MSDN docs for Console Handles, ReadFile and WriteFile

Not sure this is what you want though. If you want some kind of select on windows you can take a look at IO Completion Ports (not sure they work with console - check the docs)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top