The first goal is to take your code from last week and make the receives non-blocking (see the first discussion session and examples below).
The second goal is to make the user input non-blocking (see the second discussion section and examples below).
Note that judicious use of the usleep(nMicroseconds) function will take a lot of burden off the cpu in your 'waiting for something to do' loops!
// the receives are blocking or not based on the blocking flag void processMessages(int sockfd, bool blocking) { // set the socket to be blocking or not int flags = fcntl(sockfd, F_GETFL); if (!blocking) flags |= O_NONBLOCK; fcntl(sockfd, F_SETFL, flags); ... then inside the do while loop ... if (numbytes <= 0) { cout << "No messages currently waiting\n"; |
In files keyPress.h and keyPress.C I provide two routines:
Note that keys like the function keys, arrow keys, backspace, etc, actually get stored as specific character sequences, so it would take multiple calls to keyQuick to grab all the characters in the sequence and figure out what they represent.
If a 'special' key has been pressed it will attempt to grab the sequence of characters that represent it and attempt to identify which special key was actually pressed.
The names that keyType might be assigned include
Keys it still can't handle include:
An example of using these in non-blocking receives is given in keyTest.C.
Just for completeness, a Makefile and printFiles script are included.