#!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11134663 use warnings; use IO::Socket; use IO::Select; my %data; my $listen = IO::Socket::INET->new( LocalPort => 8080, Listen => 256, Reuse => 1) or die $@; my $sel = IO::Select->new($listen); sub checkforrequests { for my $fh ( $sel->can_read(0) ) # 0 for poll { if( $fh == $listen ) { my $socket = $listen->accept; $data{$socket} = ''; $sel->add( $socket ); print "new client $socket\n"; } elsif( sysread $fh, $data{$fh}, 4096, length $data{$fh} ) { if( $data{$fh} =~ s/^(.*?\n\r?\n)//s ) # any whole HTTP commands { my $command = $1; print $command =~ s/^/got HTTP packet: /gmr; # FIXME process request here print $fh "OK\n"; # FIXME sample response shutdown $fh, 1; $command =~ /quit/ and die "exiting on 'quit' command\n"; } } else { delete $data{$fh}; $sel->remove( $fh ); print "client left $fh\n"; } } } while( 1 ) # your loop as I understand it { select undef, undef, undef, 0.2; # FIXME your non-network stuff checkforrequests(); # this is added to your loop }