1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>A simple HTTP daemon</title>
<style type="text/css"><!--
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 Classes</font></a>
| <a href="mainclasses.html">
<font color="#004faf">Main Classes</font></a>
| <a href="annotated.html">
<font color="#004faf">Annotated</font></a>
| <a href="groups.html">
<font color="#004faf">Grouped 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="tqserversocket.html">TQServerSocket</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.3.8 edited Jan 11 14:37 $
**
** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
**
** This file is part of an example program for TQt. This example
** program may be used, distributed and modified without limitation.
**
*****************************************************************************/
#include <stdlib.h>
#include <<a href="tqsocket-h.html">tqsocket.h</a>>
#include <<a href="tqregexp-h.html">tqregexp.h</a>>
#include <<a href="tqserversocket-h.html">tqserversocket.h</a>>
#include <<a href="tqapplication-h.html">tqapplication.h</a>>
#include <<a href="tqmainwindow-h.html">tqmainwindow.h</a>>
#include <<a href="tqtextstream-h.html">tqtextstream.h</a>>
#include <<a href="tqvbox-h.html">tqvbox.h</a>>
#include <<a href="tqlabel-h.html">tqlabel.h</a>>
#include <<a href="tqtextview-h.html">tqtextview.h</a>>
#include <<a href="tqpushbutton-h.html">tqpushbutton.h</a>>
// HttpDaemon is the the class that implements the simple HTTP server.
class HttpDaemon : public <a href="tqserversocket.html">TQServerSocket</a>
{
<a href="metaobjects.html#TQ_OBJECT">TQ_OBJECT</a>
public:
HttpDaemon( <a href="tqobject.html">TQObject</a>* parent=0 ) :
<a href="tqserversocket.html">TQServerSocket</a>(8080,1,parent)
{
if ( !ok() ) {
<a href="tqapplication.html#qWarning">tqWarning</a>("Failed to bind to port 8080");
exit( 1 );
}
}
void newConnection( int socket )
{
// When a new client connects, the server constructs a TQSocket and all
// communication with the client is done over this TQSocket. TQSocket
// works asynchronouslyl, this means that all the communication is done
// in the two slots readClient() and discardClient().
<a href="tqsocket.html">TQSocket</a>* s = new <a href="tqsocket.html">TQSocket</a>( this );
<a name="x731"></a> connect( s, TQ_SIGNAL(<a href="tqsocket.html#readyRead">readyRead</a>()), this, TQ_SLOT(readClient()) );
<a name="x729"></a> connect( s, TQ_SIGNAL(<a href="tqsocket.html#delayedCloseFinished">delayedCloseFinished</a>()), this, TQ_SLOT(discardClient()) );
<a name="x732"></a> s-><a href="tqsocket.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="tqsocket.html">TQSocket</a>* socket = (TQSocket*)sender();
<a name="x727"></a> if ( socket-><a href="tqsocket.html#canReadLine">canReadLine</a>() ) {
<a name="x733"></a><a name="x730"></a> <a href="tqstringlist.html">TQStringList</a> tokens = TQStringList::<a href="tqstringlist.html#split">split</a>( TQRegExp("[ \r\n][ \r\n]*"), socket-><a href="tqsocket.html#readLine">readLine</a>() );
if ( tokens[0] == "GET" ) {
<a href="tqtextstream.html">TQTextStream</a> os( socket );
<a name="x735"></a> os.<a href="tqtextstream.html#setEncoding">setEncoding</a>( TQTextStream::UnicodeUTF8 );
os << "HTTP/1.0 200 Ok\r\n"
"Content-Type: text/html; charset=\"utf-8\"\r\n"
"\r\n"
"<h1>Nothing to see here</h1>\n";
<a name="x728"></a> socket-><a href="tqsocket.html#close">close</a>();
emit wroteToClient();
}
}
}
void discardClient()
{
<a href="tqsocket.html">TQSocket</a>* socket = (TQSocket*)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="tqvbox.html">TQVBox</a>
{
TQ_OBJECT
public:
HttpInfo()
{
HttpDaemon *httpd = new HttpDaemon( this );
<a href="tqstring.html">TQString</a> itext = TQString(
"This is a small httpd example.\n"
"You can connect with your\n"
"web browser to port %1"
<a name="x726"></a> ).arg( httpd-><a href="tqserversocket.html#port">port</a>() );
<a href="tqlabel.html">TQLabel</a> *lb = new <a href="tqlabel.html">TQLabel</a>( itext, this );
lb-><a href="tqlabel.html#setAlignment">setAlignment</a>( AlignHCenter );
infoText = new <a href="tqtextview.html">TQTextView</a>( this );
<a href="tqpushbutton.html">TQPushButton</a> *quit = new <a href="tqpushbutton.html">TQPushButton</a>( "quit" , this );
connect( httpd, TQ_SIGNAL(newConnect()), TQ_SLOT(newConnect()) );
connect( httpd, TQ_SIGNAL(endConnect()), TQ_SLOT(endConnect()) );
connect( httpd, TQ_SIGNAL(wroteToClient()), TQ_SLOT(wroteToClient()) );
<a name="x724"></a> connect( quit, TQ_SIGNAL(<a href="tqbutton.html#pressed">pressed</a>()), tqApp, TQ_SLOT(<a href="tqapplication.html#quit">quit</a>()) );
}
~HttpInfo()
{
}
private slots:
void newConnect()
{
<a name="x734"></a> infoText-><a href="tqtextedit.html#append">append</a>( "New connection" );
}
void endConnect()
{
infoText-><a href="tqtextedit.html#append">append</a>( "Connection closed\n\n" );
}
void wroteToClient()
{
infoText-><a href="tqtextedit.html#append">append</a>( "Wrote to client" );
}
private:
<a href="tqtextview.html">TQTextView</a> *infoText;
};
int main( int argc, char** argv )
{
<a href="tqapplication.html">TQApplication</a> app( argc, argv );
HttpInfo info;
app.<a href="tqapplication.html#setMainWidget">setMainWidget</a>( &info );
info.<a href="tqwidget.html#show">show</a>();
return app.<a href="tqapplication.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 © 2007
<a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
<td align=right><div align=right>TQt 3.3.8</div>
</table></div></address></body>
</html>
|