Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在我的代码中设置PERL recv超时?_Perl_Sockets_Tcp_Tcpserver_Tcpsocket - Fatal编程技术网

如何在我的代码中设置PERL recv超时?

如何在我的代码中设置PERL recv超时?,perl,sockets,tcp,tcpserver,tcpsocket,Perl,Sockets,Tcp,Tcpserver,Tcpsocket,我想在下面的特定代码中设置recv函数的超时,因为有时我的脚本会永远卡住。我是套接字编程新手,因此我非常感谢您的帮助。提前谢谢 use IO::Socket::INET; use IO::Select; use LWP::UserAgent; use JSON::XS 'decode_json'; use Data::Dumper; use DBI(); sub dbconn { my $db_conf = shift; my $dbh = DBI->connect("D

我想在下面的特定代码中设置recv函数的超时,因为有时我的脚本会永远卡住。我是套接字编程新手,因此我非常感谢您的帮助。提前谢谢

use IO::Socket::INET;
use IO::Select;
use LWP::UserAgent;
use JSON::XS 'decode_json';
use Data::Dumper;
use DBI();
sub dbconn {
    my $db_conf = shift;

    my $dbh = DBI->connect("DBI:Pg:dbname=somedatabase;host=somehost", "postgres", "",
                                        {pg_server_prepare => 
                                        0,AutoCommit => 1,RaiseError=>1});
    $dbh->do("SET CLIENT_ENCODING TO 'UTF-8';");
    return $dbh;
}



# auto-flush on socket
$| = 1;

# creating a listening socket
my $socket = new IO::Socket::INET (
    LocalHost => '0.0.0.0',
    LocalPort => '5000',
    Proto => 'tcp',
    Listen => 5,
    Reuse => 1
);
die "cannot create socket $!\n" unless $socket;

$sel = IO::Select->new( $socket );

print "Server waiting for client connection on port 5000...\n";

my $command = 1;
my $watchTracker = "*HQ,";
my $tl206 = ",LAT:";
my $watchConnectedCheck = ",A,";
my $gpsType;
my $circleString = ",LINK,";
my $dataToSend;
my $new;
my $dbh = dbconn();
while(@ready = $sel->can_read) {

        foreach $fh (@ready) {
            if($fh == $socket) {
                # Create a new socket
                $new = $socket->accept;



                $new->recv($dataReceived, 1024);
                $new->recv($dataReceived, 1024);




                # get information about a newly connected client
                my $client_address = $new->peerhost();
                my $client_port = $new->peerport();
                print "===============================================\n";
                print "===============================================\n\n";
                print "Connection from $client_address:$client_port\n";

                print "General data received: $dataReceived\n\n";

#MORE LINES...

}
            else {
                # Process socket
                # Maybe we have finished with the socket
                $sel->remove($fh);
                $fh->close;
            }
        }
    }
    $dbh->disconnect();
也许我误解了这个问题,但是您是否尝试过用“timeout”在套接字中设置超时

编辑:我没听清楚“recv”部分。您必须使用setsockopt,它不是完全可移植的,因此最终答案在某种程度上取决于您的平台。以下是一些可能有帮助的帖子:

例如:


是的,什么也没发生。我认为我必须在recv函数中设置一个超时,以避免永远等待接收当前周期的数据。欢迎提出任何建议。
$socket->setsockopt(SOL_SOCKET, SO_RCVTIMEO, pack('l!l!', 30, 0))
    or die "setsockopt: $!";