My I suggest the following Tread example, this is for each time I get a connection I spin off a child process.

The server:

use strict; use warnings; use Socket; use IO::Select; #use Time::HiRes; use threads; use threads::shared; $| = 1; # The following variables should be set within init_webserver_extensio +n use vars qw/ $port_listen /; require "http_handler.pl"; init_webserver_extension(); local *S; socket (S, PF_INET , SOCK_STREAM , getprotobyname('tcp')) or die + "couldn't open socket: $!"; setsockopt (S, SOL_SOCKET, SO_REUSEADDR, 1); bind (S, sockaddr_in($port_listen, INADDR_ANY)); listen (S, 5) or die + "don't hear anything: $!"; my $ss = IO::Select->new(); $ss -> add (*S); while(1) { my @connections_pending = $ss->can_read(); foreach (@connections_pending) { my $fh; my $remote = accept($fh, $_); my($port,$iaddr) = sockaddr_in($remote); my $peeraddress = inet_ntoa($iaddr); my $t = threads->create(\&new_connection, $fh); $t->detach; } } sub extract_vars { my $line = shift; my %vars; foreach my $part (split '&', $line) { $part =~ /^(.*)=(.*)$/; my $n = $1; my $v = $2; $n =~ s/%(..)/chr(hex($1))/eg; $v =~ s/%(..)/chr(hex($1))/eg; $vars{$n}=$v; } return \%vars; } sub new_connection { my $fh = shift; binmode $fh; my %req; $req{HEADER}={}; my $request_line = <$fh>; my $first_line = ""; while ($request_line ne "\r\n") { unless ($request_line) { close $fh; } chomp $request_line; unless ($first_line) { $first_line = $request_line; my @parts = split(" ", $first_line); if (@parts != 3) { close $fh; } $req{METHOD} = $parts[0]; $req{OBJECT} = $parts[1]; } else { my ($name, $value) = split(": ", $request_line); $name = lc $name; $req{HEADER}{$name} = $value; } $request_line = <$fh>; } http_request_handler($fh, \%req); close $fh; }

the request_handler

sub http_request_handler { my $fh = shift; my $req_ = shift; my %req = %$req_; my %header = %{$req{HEADER}}; print $fh "HTTP/1.0 200 OK\r\n"; print $fh "Server: adp perl webserver\r\n"; #print $fh "content-length: ... \r\n"; print $fh "\r\n"; print $fh "<html><h1>hello</h1></html>"; print $fh "Method: $req{METHOD}<br>"; print $fh "Object: $req{OBJECT}<br>"; foreach my $r (keys %header) { print $fh $r, " = ", $header{$r} , "<br>"; } } sub init_webserver_extension { $port_listen = 8888; } 1;

In reply to Re^2: Real-time socket data processing by Generoso
in thread Real-time socket data processing by psyk0tic

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.