Before I begin -- YES, this is via a homework assignment. However, the functionality I'm asking about is beyond the scope of the homework itself.
This might be a strange question -- and if my thinking is going down the wrong alley, let me know! -- but I'm looking for a way to watch the input for a quit command WITHOUT interfering with the program.
The program is a simple CLI socket-based server-client chat program using multithreading. I got all of it working great in the scope of the assignment.
However, it bothers me that the program cannot quit gracefully -- I have to hit Ctrl-C to quit it. So I went to work on getting it to quit gracefully. I've managed to make it detect if the server or client is no longer running, and quit.
However, that still requires me to Ctrl-C the *other* program.
The obvious step would be a simple "if input equals "/quit" then return 0" and fork it off into it's own infinite loop.
Obvious problem: this is a chat program, so input is being monitored for read/write via socket. Before you ask: I've tried putting the if-statement just before the write() call, but this doesn't work because even though it terminates the writing thread, it doesn't terminate the reading thread.
So is there a way to fork main and have that "passively" watch input, and execute as soon as it sees a matching input?
This needs to be without blocking, obviously.
I'm probably over-thinking this, but I figured this would be a good learning experience, even though it's beyond the scope of the assignment itself.
Whew, sorry for the lengthy post!
TL;DR: How to passively monitor input in CLI without blocking, for a matching input that would exit the program?
This might be a strange question -- and if my thinking is going down the wrong alley, let me know! -- but I'm looking for a way to watch the input for a quit command WITHOUT interfering with the program.
The program is a simple CLI socket-based server-client chat program using multithreading. I got all of it working great in the scope of the assignment.
However, it bothers me that the program cannot quit gracefully -- I have to hit Ctrl-C to quit it. So I went to work on getting it to quit gracefully. I've managed to make it detect if the server or client is no longer running, and quit.
However, that still requires me to Ctrl-C the *other* program.
The obvious step would be a simple "if input equals "/quit" then return 0" and fork it off into it's own infinite loop.
Obvious problem: this is a chat program, so input is being monitored for read/write via socket. Before you ask: I've tried putting the if-statement just before the write() call, but this doesn't work because even though it terminates the writing thread, it doesn't terminate the reading thread.
So is there a way to fork main and have that "passively" watch input, and execute as soon as it sees a matching input?
This needs to be without blocking, obviously.
I'm probably over-thinking this, but I figured this would be a good learning experience, even though it's beyond the scope of the assignment itself.
Whew, sorry for the lengthy post!
TL;DR: How to passively monitor input in CLI without blocking, for a matching input that would exit the program?