Visualizzazione dei risultati da 1 a 2 su 2

Discussione: IO::Select

  1. #1

    IO::Select

    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)
    codice:
     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;
                }
            }
        }
    a intuito provo a fare il mio codice per gestire connessione multiple con IO::Select

    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;
    
    		}
    
    	}
    
    }
    o forse e meglio cosi'?

    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;
    
    		}
    
    	}
    
    }
    il fatto è che non so da dove leggere la richiesta con "recv"

    se da $client o se da $fh

  2. #2
    codice:
    while (1)
    {
    
    	@ready = $sel->can_read;
    	
    	foreach $fh ( @ready ) {
    	
    		if ($fh == $socket)
    		{
    		
    			$ns = $socket->accept;
    			$sel->add($ns);
    			
    		} else {
    		
    			recv($fh,$content,10000,0);
    			print $content;
    			
    			syswrite $fh, "HTTP/1.1 200 OK\n";
    			
    			$sel->remove($fh);
    			$fh->close;
    		}
    		
    	}
    	
    }
    io avevo optato per questa scelta...soltanto che non so se magari c'è un modo per scriverlo ancora più pulito.

    ad esempio mettendo...

    while (1)
    {
    while(@ready = $sel->can_read) {

    ...

    oppure mettendo direttamente:

    while(@ready = $sel->can_read) {

    e levando del tutto il while (1)

    chiedo un aiuto a voi...

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.