简体   繁体   English

使用socket recv函数在stdin上读取流

[英]Use socket recv function for reading the stream on stdin

Can I use socket recv function for reading the input stream of stdin. 我可以使用socket recv函数读取stdin的输入流吗? ie for reading the Ctrl-C. 即用于读取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. 实际上,我正在通过使用fgetc函数来通知CTRL-C来读取stdin流,但有时fgetc不会通知任何CTRL-C。 and if I use recv function as disjunction with fgetc function then every case of notifying the CRTL-C is being handled. 如果我将recv函数与fgetc函数分开使用,那么将处理每种通知CRTL-C的情况。

So Can I use socket recv function for reading the stdin stream? 那么我可以使用socket recv函数来读取stdin流吗? Here is my complete algorithm for notifying the CRTL-C event. 这是通知CRTL-C事件的完整算法。

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 you can't use recv on non-sockets (at least, not portably or commonly). 不,您不能在非套接字上使用recv (至少,不是可移植或常见的)。 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 . 在某些系统上,FWIW可以使用selectpoll来监视非套接字,包括用于输入/输出事件的stdin,并且可以在套接字和其他流中使用读/写,但是recv / send是Berkeley套接字API的一部分插座标准。

Control-C often generates a signal (specifically SIGINT) for which you can register a signal handler, but details are OS specific. Control-C通常会生成一个信号(特别是SIGINT),您可以为其注册信号处理程序,但是细节是特定于OS的。 See eg this answer . 参见例如这个答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM