我在 TCP 服务器上使用 select。我想将 STDIN 添加到选择的文件句柄集中。
#!/usr/bin/perl
use IO::Select;
use IO::Socket::INET;
my $sock = IO::Socket::INET->new(LocalPort => $serv_listen_port, Proto => 'tcp', List en=> 1);
my $s = IO::Select->new();
$s->add(\*STDIN); #want to be responsive to user input (allow me to type commands for example)
$s->add($sock);
@readytoread=$s->can_read(1); #timeout = 1sec
foreach $readable (@readytoread) {
if ($readable==$sock) {
#This was a listen request, I accept and add new client here
}
if ($readable == STDIN){ #what to do on this line?
#This is user typing input into server on terminal
}
}
需要有关此处代码中倒数第四行的帮助。
最佳答案
$readable->fileno == fileno STDIN
或者,如果您对此感到满意,fileno STDIN
为零,您可以直接检查。
关于perl -- 检查文件句柄是否为标准输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5179922/