我想使用FCGI.pm获取客户端套接字

时间:2013-06-28 20:44:34

标签: perl sockets fastcgi

我有这段代码:

#!/usr/bin/perl
use strict;
use warnings;
use FCGI;

my $socket = FCGI::OpenSocket(":9000", 5);
my $request = FCGI::Request(\*STDIN, \*STDOUT, \*STDERR, \%ENV, $socket);
my $count = 1;

while($request->Accept() >= 0) {

    print "Content-Type: text/html; charset=UTF-8\r\n\r\n";
    print $count++, "<br />";

}

我需要在接受后创建客户端套接字。我怎么能这样做?

1 个答案:

答案 0 :(得分:0)

我解决了这个问题。

#!/usr/bin/perl  
use strict;
use warnings;
use FCGI;
use Socket;

my $socket = FCGI::OpenSocket(":9000", 5);
my $request = FCGI::Request(\*STDIN, \*STDOUT, \*STDERR, \%ENV, $socket);
my $count = 1;
while($request->Accept() >= 0) {

    print "Content-Type: text/html; charset=UTF-8\r\n\r\n";
    print $count++, "<br />";
    print "$$<br />";
    open(my $client, "+>&4") or die "$!";

    my $mysockaddr = getsockname($client) or die "$!";
    my ($myport, $myaddr) = sockaddr_in($mysockaddr);
    print $myport, ":", inet_ntoa($myaddr), "<br />";
    my $peersockaddr = getpeername($client) or die "$!";
    my ($peerport, $peeraddr) = sockaddr_in($peersockaddr);
    print $peerport, ":", inet_ntoa($peeraddr), "<br />";

    close $client;
}