c++ - 为什么 fd_set(file descriptor) 会阻止终端输出直到超时?
问题描述
我使用了这个存储库,它是一个类似于 Boost.Asio 的结构,它在 C++ 中实现消息传递和双端队列以及客户端和服务器通信。
问题是,它是为 Windows 编写的,在文件 SimpleClient.cpp 中,从命令行读取按键,现在我想在 Ubuntu 终端上读取命令行按键,因此我在使用 fd_set 的堆栈上找到了这个问题,我把它放在了函数,然后在SimpleClient文件中:
void task1_getkeyboard(CustomClient *c)
{
struct termios oldSettings, newSettings;
tcgetattr( fileno( stdin ), &oldSettings );
newSettings = oldSettings;
newSettings.c_lflag &= (~ICANON & ~ECHO);
tcsetattr( fileno( stdin ), TCSANOW, &newSettings );
fd_set set;
struct timeval tv;
tv.tv_sec = 5;
tv.tv_usec = 0;
FD_ZERO( &set );
FD_SET( fileno( stdin ), &set );
int res = select( fileno( stdin )+1, &set, NULL, NULL, &tv );
char cinif;
if( res > 0 )
{
read( fileno( stdin ), &cinif, 1 );
//printf( "Input available %c\n" , cinif);
if ( cinif == 'p' ) {
c->PingServer();
cinif = '\0';
}
}
else if( res < 0 )
{
perror( "select error" );
//break;
}
else
{
printf( "Select timeout\n" );
}
}
我已经把函数放在了while循环中:
...
while (!bQuit)
{
task1_getkeyboard(&c);
if (c.IsConnected())
{
...
问题是,它会阻止终端直到超时,例如在成功连接时,客户端应该打印服务器接受的连接但它等待 5 秒(fd_set 超时)。我该如何解决这个问题?
解决方案
推荐阅读
- java - Spring MongoDB:服务器在启动后立即关闭连接
- python-3.x - 在......中找不到“adb.exe”?
- mysql - MYSQL请求显示两个表的行的差异并删除考虑到行ID的重复字段
- hibernate - Spring JPA - 两种方式映射急切地获取孩子
- c# - 如何将此 PowerShell 代码转换为 C#
- c++ - 在点云项目中找不到 std::sample
- java - 从 Json 到 Java Object 的转换,int 变量默认赋值为 0,如果没有传入 JSON
- visual-studio-code - Visual Studio代码,如何复制文件名
- javascript - JavaScript SyntaxError:输入意外结束
- python - 从 DataLoader 图像中基于 Pytorch 参数裁剪自定义子图像