0

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; 
hmb
  • 83
  • 3
  • 10

1 Answers1

2

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.

Community
  • 1
  • 1
Tony Delroy
  • 102,968
  • 15
  • 177
  • 252
  • But how can I use ungetc function or MSG_PEEK flag like things with read/write function on stdio – hmb Jun 04 '15 at 14:45
  • I suggest you use `std::cin`'s [`peek`](http://en.cppreference.com/w/cpp/io/basic_istream/peek) and [`putback`](http://en.cppreference.com/w/cpp/io/basic_istream/putback), and `std::cout` for output, and not use `read`/`write` for stdin/stdout I/O. – Tony Delroy Jun 05 '15 at 04:52