#!/usr/bin/perl -w ## This program is based on an example program for TQt. It ## may be used, distributed and modified without limitation. ## ## Copyright (C) 1992-2000 Trolltech AS. All rights reserved. # When a new client connects, the server constructs a TQt::Socket and all # communication with the client is done over this Socket object. TQt::Socket # works asynchronously - this means that all the communication is done # through the two slots readClient() and discardClient(). package HttpDaemon; use TQt; use TQt::isa qw(TQt::ServerSocket); use TQt::signals newConnect => [], endConnect => [], wroteToClient => []; use TQt::slots readClient => [], discardClient => []; use TQt::attributes qw( sockets ); sub NEW { shift->SUPER::NEW(8080, 1, $_[0]); if( !this->ok() ) { die "Failed to bind to port 8080\n"; } sockets = {}; } sub newConnection { my $s = TQt::Socket( this ); this->connect( $s, TQT_SIGNAL 'readyRead()', this, TQT_SLOT 'readClient()' ); this->connect( $s, TQT_SIGNAL 'delayedCloseFinished()', this, TQT_SLOT 'discardClient()' ); $s->setSocket( shift ); sockets->{ $s } = $s; emit newConnect(); } sub readClient { # This slot is called when the client sent data to the server. The # server looks if it was a get request and sends a very simple HTML # document back. my $s = sender(); if ( $s->canReadLine() ) { my @tokens = split( /\s\s*/, $s->readLine() ); if ( $tokens[0] eq "GET" ) { my $string = "HTTP/1.0 200 Ok\n\rContent-Type: text/html; charset=\"utf-8\"\n\r". "\n\r