【发布时间】:2020-07-18 21:13:26
【问题描述】:
我有一个客户端处于接收模式的程序,它正在等待接收一些数据;虽然如果用户在终端上写入程序被“阻塞”,当程序从接收返回时,我有一个必须从标准输入读取的std::getline(),但它在程序被阻塞时读取用户给出的输入而是让我有机会做一个std::cin。
当程序在接收数据时被阻止时,如何刷新用户提供的所有输入?
这是伪代码:
{
...
int ret = receive(socket,buffer,0);
//During the receive the program will be blocked on the receive till it will be completed
//If the user type something in the stdin before the receive above it's completede when the getline it's executed it catch the bad input given by the user when the receive is processing
getline(cin, buffer);
...
}
【问题讨论】:
-
您将需要使用非阻塞 I/O 来读取标准输入上的任何缓冲数据。这超出了 C++ 标准的范围,您需要使用特定于操作系统的工具来执行此操作。
-
我编辑了这个问题,我不知道现在是否更清楚了
-
没有办法刷新
stdin。试图这样做是未定义的行为。无论如何,您都不应该尝试实现此功能,以免您希望您的用户对您非常生气。如果我在程序中输入一些东西,我最不希望它会因为它“忙”而丢弃我的输入。 -
我完全同意你的看法,但是当程序繁忙时写的这些东西可能是来自恶意用户的攻击,所以我需要以某种方式忽略它。这是出于安全原因。
-
"这是伪代码" - 现在试试真正的代码。