开发者

Windows listen for socket and command line input simultaneously

My application needs to liste开发者_运维技巧n for keyboard input from user from command line as well as listen for data on sockets simultaneously in a single loop. This application currently runs on linux and there the above can be achieved easily by using select and adding stdin and network sockets to the fd_set array list given to select call. Depending on the activity, the app can either process the user input OR the data on the socket.

Now I am porting this application to Windows and I am not very familiar with Windows. How can the above be achieved in Windows? That is listening for activity on stdin as well as sockets. Any pointers appreciated.


I was able to solve this in the following way.

Used the WaitForMultipleObjects() api. For this API, use the handle for the STD_INPUT console handle. And for each socket that you are interested in, create a WSACreateEvent and add it to the array of handles passed to the WaitForMultipleObjects() API.

When WaitForMultipleObjects() API returns you can find out the handle for which the activity is detected and act accordingly.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜