Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Re^5: Adding simple HTTP controls to existing code

by huck (Prior)
on Jul 05, 2021 at 18:33 UTC ( #11134677=note: print w/replies, xml ) Need Help??

in reply to Re^4: Adding simple HTTP controls to existing code
in thread Adding simple HTTP controls to existing code

Mine is a little different

use strict; use warnings; select (STDOUT); $| = 1; select(STDOUT); # # # # use IO::Handle; use IO::Socket; use IO::Select; my $server = IO::Socket::INET->new( Proto => 'tcp', PeerAddr => "", # only + us LocalPort => 80, Listen => SOMAXCONN, Reuse => 1); die "can't setup server on 80" unless $server; my $sel_read = IO::Select->new(); my $sel_write = IO::Select->new(); my $sel_error = IO::Select->new(); $sel_read-> add($server); my $cmdrun={}; $cmdrun->{'/exit'} =sub{exit;} ; $cmdrun->{'/hi'} =sub{my $fh=shift; print $fh "\nhi\n";} ; $cmdrun->{'/null'} =sub{} ; while(1) { checkforrequests(); } # superloop exit; sub checkforrequests { my $timeout=1; my ($rd_ptr,$wr_ptr,$er_ptr)=IO::Select->select($sel_read,undef,$s +el_error,$timeout); for my $fh (@$rd_ptr) { if ($fh == $server) { my $client = $server->accept; $client->autoflush(1); $sel_read->add($client); } # server else { my $line=<$fh>; print "line:$line"; my ($junk,$post)=split(' ',$line,3); my $cmd=$cmdrun->{$post}; if ($cmd) {&$cmd($fh);} else { print $fh "\n\nNO\n"; } $sel_read->remove($fh); close $fh; } # client } # for fh } # checkforrequests

Replies are listed 'Best First'.
Re^6: Adding simple HTTP controls to existing code
by brachism (Novice) on Jul 05, 2021 at 19:25 UTC

    Thanks Huck.

    I was able to quickly adapt Tybalt89's suggestion in my code. I will give your suggestion a try as well.

    Comparing and contrasting different solutions while learning is always helpful. Thanks again

      They have a similar base

      Mine does a two step between accept and read, a client CAN create the connection and wait a bit before writing, so mine wont block there. But in general http: clients shouldnt matter.

      His has a better read, the way i commonly use it is a lot like his, but i just collect data, then have another loop that looks for completed lines so i dont block on the \n. IN both of these examples we block on the \n read, mine till \n, his to blank line.

      I added a cmd lookup table for what to execute

      I only look at the first line sent.

      I added the /null command to show you what might happen if you dont send anything back

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11134677]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (10)
As of 2022-12-05 22:29 GMT
Find Nodes?
    Voting Booth?

    No recent polls found