hmb
hmb

Reputation: 103

Use socket recv function for reading the stream on stdin

Can I use socket recv function for reading the input stream of stdin. i.e for reading the Ctrl-C.

Like.

int dummy;

ssize_t bytes = recv (0 /* file descriptor for stdin*/ , &dummy, 1, MSG_PEEK);

if (bytes > 0 && dummy == 0x03)
return true; // Ctrl-C receive
else
return false;        // Not

Actually I am reading the stdin stream by using the fgetc function to notify the CTRL-C but sometime fgetc does not notify any CTRL-C. and if I use recv function as disjunction with fgetc function then every case of notifying the CRTL-C is being handled.

So Can I use socket recv function for reading the stdin stream? Here is my complete algorithm for notifying the 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; 

Upvotes: 0

Views: 1996

Answers (1)

Tony Delroy
Tony Delroy

Reputation: 106076

No you can't use recv on non-sockets (at least, not portably or commonly). FWIW, on some systems, select or poll can be used to monitor non-sockets including stdin for I/O events, and read/write can be used across sockets and other streams, but recv/send are specifically part of the Berkeley Sockets API standard for sockets.

Control-C often generates a signal (specifically SIGINT) for which you can register a signal handler, but details are OS specific. See e.g. this answer.

Upvotes: 2

Related Questions