Sophie

Sophie

distrib > Mandriva > 8.2 > i586 > media > contrib > by-pkgid > 112b0974ad288f6cd55bf971ee6026a9 > files > 742

libqt3-devel-3.0.2-2mdk.i586.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- /tmp/qt-3.0-reggie-28534/qt-x11-free-3.0.2/examples/network/httpd/httpd.doc:5 -->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>A simple HTTP daemon</title>
<style type="text/css"><!--
h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }
a:link { color: #004faf; text-decoration: none }
a:visited { color: #672967; text-decoration: none }
body { background: #ffffff; color: black; }
--></style>
</head>
<body>

<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr bgcolor="#E5E5E5">
<td valign=center>
 <a href="index.html">
<font color="#004faf">Home</font></a>
 | <a href="classes.html">
<font color="#004faf">All&nbsp;Classes</font></a>
 | <a href="mainclasses.html">
<font color="#004faf">Main&nbsp;Classes</font></a>
 | <a href="annotated.html">
<font color="#004faf">Annotated</font></a>
 | <a href="groups.html">
<font color="#004faf">Grouped&nbsp;Classes</font></a>
 | <a href="functions.html">
<font color="#004faf">Functions</font></a>
</td>
<td align="right" valign="center"><img src="logo32.png" align="right" width="64" height="32" border="0"></td></tr></table><h1 align=center>A simple HTTP daemon</h1>

 
<p> 
<p> This example shows how to use the <a href="qserversocket.html">QServerSocket</a> class. It is a very
simple implementation of a HTTP daemon that listens on port 8080 and
sends back a simple HTML page back for every GET request it gets. After
sending the page, it closes the connection.
<p> <hr>
<p> Implementation (httpd.cpp):
<p> <pre>/****************************************************************************
** $Id:  qt/httpd.cpp   3.0.2   edited Nov 6 19:46 $
**
** Copyright (C) 1992-2000 Trolltech AS.  All rights reserved.
**
** This file is part of an example program for Qt.  This example
** program may be used, distributed and modified without limitation.
**
*****************************************************************************/
#include &lt;stdlib.h&gt;
#include &lt;<a href="qsocket-h.html">qsocket.h</a>&gt;
#include &lt;<a href="qregexp-h.html">qregexp.h</a>&gt;
#include &lt;<a href="qserversocket-h.html">qserversocket.h</a>&gt;
#include &lt;<a href="qapplication-h.html">qapplication.h</a>&gt;
#include &lt;<a href="qmainwindow-h.html">qmainwindow.h</a>&gt;
#include &lt;<a href="qtextstream-h.html">qtextstream.h</a>&gt;
#include &lt;<a href="qvbox-h.html">qvbox.h</a>&gt;
#include &lt;<a href="qlabel-h.html">qlabel.h</a>&gt;
#include &lt;<a href="qtextview-h.html">qtextview.h</a>&gt;
#include &lt;<a href="qpushbutton-h.html">qpushbutton.h</a>&gt;

// HttpDaemon is the the class that implements the simple HTTP server.
class HttpDaemon : public <a href="qserversocket.html">QServerSocket</a>
{
    <a href="metaobjects.html#Q_OBJECT">Q_OBJECT</a>
public:
    HttpDaemon( <a href="qobject.html">QObject</a>* parent=0 ) :
        <a href="qserversocket.html">QServerSocket</a>(8080,1,parent)
    {
        if ( !ok() ) {
            <a href="qapplication.html#qWarning">qWarning</a>("Failed to bind to port 8080");
            exit( 1 );
        }
    }

    void newConnection( int socket )
    {
        // When a new client connects, the server constructs a QSocket and all
        // communication with the client is done over this QSocket. QSocket
        // works asynchronouslyl, this means that all the communication is done
        // in the two slots readClient() and discardClient().
        <a href="qsocket.html">QSocket</a>* s = new <a href="qsocket.html">QSocket</a>( this );
<a name="x996"></a>        connect( s, SIGNAL(<a href="qsocket.html#readyRead">readyRead</a>()), this, SLOT(readClient()) );
<a name="x995"></a>        connect( s, SIGNAL(<a href="qsocket.html#delayedCloseFinished">delayedCloseFinished</a>()), this, SLOT(discardClient()) );
<a name="x997"></a>        s-&gt;<a href="qsocket.html#setSocket">setSocket</a>( socket );
        emit newConnect();
    }

signals:
    void newConnect();
    void endConnect();
    void wroteToClient();

private slots:
    void 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.
        <a href="qsocket.html">QSocket</a>* socket = (QSocket*)sender();
<a name="x993"></a>        if ( socket-&gt;<a href="qsocket.html#canReadLine">canReadLine</a>() ) {
<a name="x998"></a><a name="x990"></a>            <a href="qstringlist.html">QStringList</a> tokens = QStringList::<a href="qstringlist.html#split">split</a>( QRegExp("[ \n\r][ \n\r]*"), socket-&gt;<a href="qsocket.html#readLine">readLine</a>() );
            if ( tokens[0] == "GET" ) {
                <a href="qtextstream.html">QTextStream</a> os( socket );
<a name="x1000"></a>                os.<a href="qtextstream.html#setEncoding">setEncoding</a>( QTextStream::UnicodeUTF8 );
                os &lt;&lt; "HTTP/1.0 200 Ok\n\r"
                    "Content-Type: text/html; charset=\"utf-8\"\n\r"
                    "\n\r"
                    "&lt;h1&gt;Nothing to see here&lt;/h1&gt;\n";
<a name="x994"></a>                socket-&gt;<a href="qsocket.html#close">close</a>();
                emit wroteToClient();
            }
        }
    }
    void discardClient()
    {
        <a href="qsocket.html">QSocket</a>* socket = (QSocket*)sender();
        delete socket;
        emit endConnect();
    }
};


// HttpInfo provides a simple graphical user interface to the server and shows
// the actions of the server.
class HttpInfo : public <a href="qvbox.html">QVBox</a>
{
    Q_OBJECT
public:
    HttpInfo()
    {
        HttpDaemon *httpd = new HttpDaemon( this );

        <a href="qstring.html">QString</a> itext = QString(
                "This is a small httpd example.\n"
                "You can connect with your\n"
                "web browser to port %1"
<a name="x992"></a>            ).arg( httpd-&gt;<a href="qserversocket.html#port">port</a>() );
        <a href="qlabel.html">QLabel</a> *lb = new <a href="qlabel.html">QLabel</a>( itext, this );
        lb-&gt;<a href="qlabel.html#setAlignment">setAlignment</a>( AlignHCenter );
        infoText = new <a href="qtextview.html">QTextView</a>( this );
        <a href="qpushbutton.html">QPushButton</a> *quit = new <a href="qpushbutton.html">QPushButton</a>( "quit" , this );

        connect( httpd, SIGNAL(newConnect()), SLOT(newConnect()) );
        connect( httpd, SIGNAL(endConnect()), SLOT(endConnect()) );
        connect( httpd, SIGNAL(wroteToClient()), SLOT(wroteToClient()) );
<a name="x989"></a>        connect( quit, SIGNAL(<a href="qbutton.html#pressed">pressed</a>()), qApp, SLOT(<a href="qapplication.html#quit">quit</a>()) );
    }

    ~HttpInfo()
    {
    }

private slots:
    void newConnect()
    {
<a name="x999"></a>        infoText-&gt;<a href="qtextedit.html#append">append</a>( "New connection" );
    }
    void endConnect()
    {
        infoText-&gt;<a href="qtextedit.html#append">append</a>( "Connection closed\n\n" );
    }
    void wroteToClient()
    {
        infoText-&gt;<a href="qtextedit.html#append">append</a>( "Wrote to client" );
    }

private:
    <a href="qtextview.html">QTextView</a> *infoText;
};


int main( int argc, char** argv )
{
    <a href="qapplication.html">QApplication</a> app( argc, argv );
    HttpInfo info;
    app.<a href="qapplication.html#setMainWidget">setMainWidget</a>( &amp;info );
    info.<a href="qwidget.html#show">show</a>();
    return app.<a href="qapplication.html#exec">exec</a>();
}

#include "httpd.moc"
</pre>

<p>See also <a href="network-examples.html">Network Examples</a>.

<!-- eof -->
<p><address><hr><div align=center>
<table width=100% cellspacing=0 border=0><tr>
<td>Copyright &copy; 2001 
<a href="http://www.trolltech.com">Trolltech</a><td><a href="http://www.trolltech.com/trademarks.html">Trademarks</a>
<td align=right><div align=right>Qt version 3.0.2</div>
</table></div></address></body>
</html>