sub PortListener { ########################### # Listening and monitoring Client connections # ########################### my $Thread; my $client_name; my $lsn = new IO::Socket::INET(Listen => 1, LocalPort => $port); my $sel = new IO::Select($lsn); my $j; while(my @ready = $sel->can_read) { # as long as we can read from foreach my $fh (@ready) { if($fh == $lsn) { # Create a new socket my ($new, $c_addr) = $lsn->accept; $sel->add($new); my ($client_port, $client_ip) = sockaddr_in($c_addr); my $client_ipnum = inet_ntoa($client_ip); my $i; for $i ( 0 .. $#HostList ) { if ($client_ipnum eq $HostList[$i][1]){ $client_name = $HostList[$i][0]; $HostList[$i][2] = 1; my $dummy_name = "HostConn_$i"; $main->$dummy_name->Text("connected"); $main->$dummy_name->Change( -foreground => [ 0, 255, 0 ] ); $main->$dummy_name->InvalidateRect(1); $j = $i; last; } } print "got a connection from: $client_name [$client_ipnum]\n"; if ($client_name eq "Win1") { my $Thread = threads->create( "nautserver", $new); #start the echo-server as a seperate thread } else { my $Thread = threads->create( "restoreserver", $new, $j); #start the echo-server as a seperate thread our ${restque."$j"} = Thread::Queue->new; } #$Thread->detach(); } else { $sel->remove($fh); my $client_ip = IO::Socket::INET::peeraddr($fh); my $client_ipnum = inet_ntoa($client_ip); print "$client_ipnum resigned \n"; STDOUT->autoflush(1); my $i; for $i ( 0 .. $#HostList ) { if ($client_ipnum eq $HostList[$i][1]){ $client_name = $HostList[$i][0]; $HostList[$i][2] = 0; my $dummy_name = "HostConn_$i"; $main->$dummy_name->Text("disconnected"); $main->$dummy_name->Change( -foreground => [ 255, 0, 0 ] ); $main->$dummy_name->InvalidateRect(1); last; } } if ($client_name eq "Win1") { $main->NautActualDisp->Text("connecting ..."); $run->RunNautActualDisp->Text("connecting ..."); } $fh->close; $go->enqueue("2"); # This tells our nautserver that it should terminate, as we will reopen a new socket } } } } sub restoreserver{ my $session = $_[0]; my $hostno = $_[1]; print "Server for host number $hostno started\n"; STDOUT->autoflush(1); while(1) { # keep looping for ever print "waiting for button\n"; my $queuedvar = ${restque."$hostno"}->dequeue; # sleep until something is in the queue print "Button clicked"; # or do something useful later } }