c++ - Use socket recv function for reading the stream on stdin -
can use socket recv function reading input stream of stdin. i.e reading ctrl-c.
like.
int dummy; ssize_t bytes = recv (0 /* file descriptor stdin*/ , &dummy, 1, msg_peek); if (bytes > 0 && dummy == 0x03) return true; // ctrl-c receive else return false; // not
actually reading stdin stream using fgetc function notify ctrl-c sometime fgetc not notify ctrl-c. , if use recv function disjunction fgetc function every case of notifying crtl-c being handled.
so can use socket recv function reading stdin stream? here complete algorithm notifying crtl-c event.
ssize_t bytes = recv (data->input, &dummy, 1, msg_peek); dummy1 = fgetc (stdin) if ((dummy1 == 0x03) || (bytes > 0 && dummy == 0x03)) return true; else return false;
no can't use recv
on non-sockets (at least, not portably or commonly). fwiw, on systems, select
or poll
can used monitor non-sockets including stdin i/o events, , read/write can used across sockets , other streams, recv/send part of berkeley sockets api standard for sockets.
control-c generates signal (specifically sigint) can register signal handler, details os specific. see e.g. this answer.
Comments
Post a Comment