开发者

Windows listen for socket and command line input simultaneously

开发者 https://www.devze.com 2023-03-26 09:31 出处:网络
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 l

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

精彩评论

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