http://www.perlmonks.org?node_id=1197306


in reply to Re: Parallel::ForkManager or something like that?
in thread Parallel::ForkManager or something like that?

Hello Mario. Thank you for your answer. I have modified the script a little bit, but if i want to send a http Header to $bay and open the site on http://127.0.0.1:2222 the browser shows me nothing or only if i have luck the script code. But not HTML ... What i do wrong ?
#!/usr/bin/perl use strict; use warnings; use feature 'state'; no strict 'refs'; use IO::Socket; use IO::Select; use Parallel::ForkManager; my $die = 0; my %lkup; $|=1; sub _uid { state $uid = 0; $uid = 1 if ++$uid > 2e9; $uid; } $SIG{INT} = sub { $die = 1; exit; }; my %clients = (); my $select = IO::Select->new; my $server = IO::Socket::INET->new( LocalHost => '127.0.0.1', LocalPort => 2222, Proto => 'tcp', Listen => 10000, Reuse => 1, ) or die "Sock Error: $!\n"; my $pm = Parallel::ForkManager->new(10); $pm->set_waitpid_blocking_sleep(0); $pm->run_on_finish( sub { my ($pid,$exitcode,$ident,$exitsignal,$coredump,$get) = @_; $clients{$ident}->{ident} = $ident; $clients{$ident}->{ip} = $get->{ip}; $clients{$ident}->{socket} = delete $lkup{$ident}; }); $server->autoflush(1); $select->add($server); while (!$die) { foreach my $key ($select->can_read()) { # foreach if ($key eq $server) { # if $bay eq $server next if $key eq ""; my $bay = $server->accept or next; my $ip = $bay->peerhost(); my $ident = _uid(); $lkup{$ident} = $bay; $select->add($bay); my $buffer = <$bay>; if( $buffer =~ m/^GET\s\/\sHTTP\/1\.1/ ){ print "OK"; sendHeader($bay); my $html=<<"EOT"; <html> <head> </head> <body style='background-color: blue'> </body> </html> EOT $bay->syswrite($html); } $pm->start($ident) and next; print "$buffer\n"; $pm->finish(0, { ip => $ip }); } } $pm->wait_all_children; } sub sendHeader { my $sock = shift; my $header =<<"EOT"; HTTP/1.0 200 OK Content-type: text/html EOT for( 1.. 10 ){ $sock->syswrite("<!-- Server 2017 //-->\n"); } $sock->syswrite($header); }