[190] | 1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
|
---|
| 2 | <!-- /home/espenr/tmp/qt-3.3.8-espenr-2499/qt-x11-free-3.3.8/examples/network/httpd/httpd.doc:5 -->
|
---|
| 3 | <html>
|
---|
| 4 | <head>
|
---|
| 5 | <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
---|
| 6 | <title>A simple HTTP daemon</title>
|
---|
| 7 | <style type="text/css"><!--
|
---|
| 8 | fn { margin-left: 1cm; text-indent: -1cm; }
|
---|
| 9 | a:link { color: #004faf; text-decoration: none }
|
---|
| 10 | a:visited { color: #672967; text-decoration: none }
|
---|
| 11 | body { background: #ffffff; color: black; }
|
---|
| 12 | --></style>
|
---|
| 13 | </head>
|
---|
| 14 | <body>
|
---|
| 15 |
|
---|
| 16 | <table border="0" cellpadding="0" cellspacing="0" width="100%">
|
---|
| 17 | <tr bgcolor="#E5E5E5">
|
---|
| 18 | <td valign=center>
|
---|
| 19 | <a href="index.html">
|
---|
| 20 | <font color="#004faf">Home</font></a>
|
---|
| 21 | | <a href="classes.html">
|
---|
| 22 | <font color="#004faf">All Classes</font></a>
|
---|
| 23 | | <a href="mainclasses.html">
|
---|
| 24 | <font color="#004faf">Main Classes</font></a>
|
---|
| 25 | | <a href="annotated.html">
|
---|
| 26 | <font color="#004faf">Annotated</font></a>
|
---|
| 27 | | <a href="groups.html">
|
---|
| 28 | <font color="#004faf">Grouped Classes</font></a>
|
---|
| 29 | | <a href="functions.html">
|
---|
| 30 | <font color="#004faf">Functions</font></a>
|
---|
| 31 | </td>
|
---|
| 32 | <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>
|
---|
| 33 |
|
---|
| 34 |
|
---|
| 35 | <p>
|
---|
| 36 | <p> This example shows how to use the <a href="qserversocket.html">QServerSocket</a> class. It is a very
|
---|
| 37 | simple implementation of a HTTP daemon that listens on port 8080 and
|
---|
| 38 | sends back a simple HTML page back for every GET request it gets. After
|
---|
| 39 | sending the page, it closes the connection.
|
---|
| 40 | <p> <hr>
|
---|
| 41 | <p> Implementation (httpd.cpp):
|
---|
| 42 | <p> <pre>/****************************************************************************
|
---|
| 43 | ** $Id: httpd-example.html 2051 2007-02-21 10:04:20Z chehrlic $
|
---|
| 44 | **
|
---|
| 45 | ** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
|
---|
| 46 | **
|
---|
| 47 | ** This file is part of an example program for Qt. This example
|
---|
| 48 | ** program may be used, distributed and modified without limitation.
|
---|
| 49 | **
|
---|
| 50 | *****************************************************************************/
|
---|
| 51 | #include <stdlib.h>
|
---|
| 52 | #include <<a href="qsocket-h.html">qsocket.h</a>>
|
---|
| 53 | #include <<a href="qregexp-h.html">qregexp.h</a>>
|
---|
| 54 | #include <<a href="qserversocket-h.html">qserversocket.h</a>>
|
---|
| 55 | #include <<a href="qapplication-h.html">qapplication.h</a>>
|
---|
| 56 | #include <<a href="qmainwindow-h.html">qmainwindow.h</a>>
|
---|
| 57 | #include <<a href="qtextstream-h.html">qtextstream.h</a>>
|
---|
| 58 | #include <<a href="qvbox-h.html">qvbox.h</a>>
|
---|
| 59 | #include <<a href="qlabel-h.html">qlabel.h</a>>
|
---|
| 60 | #include <<a href="qtextview-h.html">qtextview.h</a>>
|
---|
| 61 | #include <<a href="qpushbutton-h.html">qpushbutton.h</a>>
|
---|
| 62 |
|
---|
| 63 | // HttpDaemon is the the class that implements the simple HTTP server.
|
---|
| 64 | class HttpDaemon : public <a href="qserversocket.html">QServerSocket</a>
|
---|
| 65 | {
|
---|
| 66 | <a href="metaobjects.html#Q_OBJECT">Q_OBJECT</a>
|
---|
| 67 | public:
|
---|
| 68 | HttpDaemon( <a href="qobject.html">QObject</a>* parent=0 ) :
|
---|
| 69 | <a href="qserversocket.html">QServerSocket</a>(8080,1,parent)
|
---|
| 70 | {
|
---|
| 71 | if ( !ok() ) {
|
---|
| 72 | <a href="qapplication.html#qWarning">qWarning</a>("Failed to bind to port 8080");
|
---|
| 73 | exit( 1 );
|
---|
| 74 | }
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | void newConnection( int socket )
|
---|
| 78 | {
|
---|
| 79 | // When a new client connects, the server constructs a QSocket and all
|
---|
| 80 | // communication with the client is done over this QSocket. QSocket
|
---|
| 81 | // works asynchronouslyl, this means that all the communication is done
|
---|
| 82 | // in the two slots readClient() and discardClient().
|
---|
| 83 | <a href="qsocket.html">QSocket</a>* s = new <a href="qsocket.html">QSocket</a>( this );
|
---|
| 84 | <a name="x731"></a> connect( s, SIGNAL(<a href="qsocket.html#readyRead">readyRead</a>()), this, SLOT(readClient()) );
|
---|
| 85 | <a name="x729"></a> connect( s, SIGNAL(<a href="qsocket.html#delayedCloseFinished">delayedCloseFinished</a>()), this, SLOT(discardClient()) );
|
---|
| 86 | <a name="x732"></a> s-><a href="qsocket.html#setSocket">setSocket</a>( socket );
|
---|
| 87 | emit newConnect();
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | signals:
|
---|
| 91 | void newConnect();
|
---|
| 92 | void endConnect();
|
---|
| 93 | void wroteToClient();
|
---|
| 94 |
|
---|
| 95 | private slots:
|
---|
| 96 | void readClient()
|
---|
| 97 | {
|
---|
| 98 | // This slot is called when the client sent data to the server. The
|
---|
| 99 | // server looks if it was a get request and sends a very simple HTML
|
---|
| 100 | // document back.
|
---|
| 101 | <a href="qsocket.html">QSocket</a>* socket = (QSocket*)sender();
|
---|
| 102 | <a name="x727"></a> if ( socket-><a href="qsocket.html#canReadLine">canReadLine</a>() ) {
|
---|
| 103 | <a name="x733"></a><a name="x730"></a> <a href="qstringlist.html">QStringList</a> tokens = QStringList::<a href="qstringlist.html#split">split</a>( QRegExp("[ \r\n][ \r\n]*"), socket-><a href="qsocket.html#readLine">readLine</a>() );
|
---|
| 104 | if ( tokens[0] == "GET" ) {
|
---|
| 105 | <a href="qtextstream.html">QTextStream</a> os( socket );
|
---|
| 106 | <a name="x735"></a> os.<a href="qtextstream.html#setEncoding">setEncoding</a>( QTextStream::UnicodeUTF8 );
|
---|
| 107 | os << "HTTP/1.0 200 Ok\r\n"
|
---|
| 108 | "Content-Type: text/html; charset=\"utf-8\"\r\n"
|
---|
| 109 | "\r\n"
|
---|
| 110 | "<h1>Nothing to see here</h1>\n";
|
---|
| 111 | <a name="x728"></a> socket-><a href="qsocket.html#close">close</a>();
|
---|
| 112 | emit wroteToClient();
|
---|
| 113 | }
|
---|
| 114 | }
|
---|
| 115 | }
|
---|
| 116 | void discardClient()
|
---|
| 117 | {
|
---|
| 118 | <a href="qsocket.html">QSocket</a>* socket = (QSocket*)sender();
|
---|
| 119 | delete socket;
|
---|
| 120 | emit endConnect();
|
---|
| 121 | }
|
---|
| 122 | };
|
---|
| 123 |
|
---|
| 124 |
|
---|
| 125 | // HttpInfo provides a simple graphical user interface to the server and shows
|
---|
| 126 | // the actions of the server.
|
---|
| 127 | class HttpInfo : public <a href="qvbox.html">QVBox</a>
|
---|
| 128 | {
|
---|
| 129 | Q_OBJECT
|
---|
| 130 | public:
|
---|
| 131 | HttpInfo()
|
---|
| 132 | {
|
---|
| 133 | HttpDaemon *httpd = new HttpDaemon( this );
|
---|
| 134 |
|
---|
| 135 | <a href="qstring.html">QString</a> itext = QString(
|
---|
| 136 | "This is a small httpd example.\n"
|
---|
| 137 | "You can connect with your\n"
|
---|
| 138 | "web browser to port %1"
|
---|
| 139 | <a name="x726"></a> ).arg( httpd-><a href="qserversocket.html#port">port</a>() );
|
---|
| 140 | <a href="qlabel.html">QLabel</a> *lb = new <a href="qlabel.html">QLabel</a>( itext, this );
|
---|
| 141 | lb-><a href="qlabel.html#setAlignment">setAlignment</a>( AlignHCenter );
|
---|
| 142 | infoText = new <a href="qtextview.html">QTextView</a>( this );
|
---|
| 143 | <a href="qpushbutton.html">QPushButton</a> *quit = new <a href="qpushbutton.html">QPushButton</a>( "quit" , this );
|
---|
| 144 |
|
---|
| 145 | connect( httpd, SIGNAL(newConnect()), SLOT(newConnect()) );
|
---|
| 146 | connect( httpd, SIGNAL(endConnect()), SLOT(endConnect()) );
|
---|
| 147 | connect( httpd, SIGNAL(wroteToClient()), SLOT(wroteToClient()) );
|
---|
| 148 | <a name="x724"></a> connect( quit, SIGNAL(<a href="qbutton.html#pressed">pressed</a>()), qApp, SLOT(<a href="qapplication.html#quit">quit</a>()) );
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | ~HttpInfo()
|
---|
| 152 | {
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 | private slots:
|
---|
| 156 | void newConnect()
|
---|
| 157 | {
|
---|
| 158 | <a name="x734"></a> infoText-><a href="qtextedit.html#append">append</a>( "New connection" );
|
---|
| 159 | }
|
---|
| 160 | void endConnect()
|
---|
| 161 | {
|
---|
| 162 | infoText-><a href="qtextedit.html#append">append</a>( "Connection closed\n\n" );
|
---|
| 163 | }
|
---|
| 164 | void wroteToClient()
|
---|
| 165 | {
|
---|
| 166 | infoText-><a href="qtextedit.html#append">append</a>( "Wrote to client" );
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 | private:
|
---|
| 170 | <a href="qtextview.html">QTextView</a> *infoText;
|
---|
| 171 | };
|
---|
| 172 |
|
---|
| 173 |
|
---|
| 174 | int main( int argc, char** argv )
|
---|
| 175 | {
|
---|
| 176 | <a href="qapplication.html">QApplication</a> app( argc, argv );
|
---|
| 177 | HttpInfo info;
|
---|
| 178 | app.<a href="qapplication.html#setMainWidget">setMainWidget</a>( &info );
|
---|
| 179 | info.<a href="qwidget.html#show">show</a>();
|
---|
| 180 | return app.<a href="qapplication.html#exec">exec</a>();
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 | #include "httpd.moc"
|
---|
| 184 | </pre>
|
---|
| 185 |
|
---|
| 186 | <p>See also <a href="network-examples.html">Network Examples</a>.
|
---|
| 187 |
|
---|
| 188 | <!-- eof -->
|
---|
| 189 | <p><address><hr><div align=center>
|
---|
| 190 | <table width=100% cellspacing=0 border=0><tr>
|
---|
| 191 | <td>Copyright © 2007
|
---|
| 192 | <a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
|
---|
| 193 | <td align=right><div align=right>Qt 3.3.8</div>
|
---|
| 194 | </table></div></address></body>
|
---|
| 195 | </html>
|
---|