Ho letto che con IO::Select potevo riuscire a gestire più connessioni simultaneamente e ho trovato questi due esempi in rete:
1)
codice:use IO::Select; $read_set = new IO::Select(); # create handle set for reading $read_set->add($s); # add the main socket to the set while (1) { # forever # get a set of readable handles (blocks until at least one handle is ready) my $rh_set = IO::Select->select($read_set, undef, undef, 0); # take all readable handles in turn foreach $rh (@$rh_set) { # if it is the main socket then we have an incoming connection and # we should accept() it and then add the new socket to the $read_set if ($rh == $s) { $ns = $rh->accept(); $read_set->add($ns); } # otherwise it is an ordinary socket and we should read and process the request else { $buf = <$rh>; if($buf) { # we get normal input # ... process $buf ... } else { # the client has closed the socket # remove the socket from the $read_set and close it $read_set->remove($rh); close($rh); } } } }
2)
a intuito provo a fare il mio codice per gestire connessione multiple con IO::Selectcodice:use IO::Select; use IO::Socket; $lsn = new IO::Socket::INET(Listen => 1, LocalPort => 8080); $sel = new IO::Select( $lsn ); while(@ready = $sel->can_read) { foreach $fh (@ready) { if($fh == $lsn) { # Create a new socket $new = $lsn->accept; $sel->add($new); } else { # Process socket # Maybe we have finished with the socket $sel->remove($fh); $fh->close; } } }
o forse e meglio cosi'?codice:use IO::Select; use IO::Socket; $socket = IO::Socket::INET; $sel = new IO::Select( $socket ); while (1) { @ready = $sel->can_read; foreach $fh (@ready) { if ($fh == $socket) { $client = $socket->accept; $sel->add($client); } else { # Process socket recv($client,$content,40000,0); # $sel->remove($fh); $fh->close; } } }
il fatto è che non so da dove leggere la richiesta con "recv"codice:use IO::Select; use IO::Socket; $socket = IO::Socket::INET; $sel = new IO::Select(); $sel->add($socket); while (1) { $rh_set = IO::Select->select($sel, undef, undef, 0); foreach $fh (@$rh_set) { if ($fh == $socket) { $client = $socket->accept; $sel->add($client); } else { # Process socket recv($client,$content,40000,0); # $sel->remove($fh); $fh->close; } } }
se da $client o se da $fh



Rispondi quotando