File: qtcpserver.html

package info (click to toggle)
python-qt4 4.12.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 40,300 kB
  • ctags: 6,185
  • sloc: python: 125,988; cpp: 13,291; xml: 292; makefile: 246; php: 27; sh: 2
file content (210 lines) | stat: -rw-r--r-- 18,449 bytes parent folder | download
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
<html><head><title>QTcpServer Class Reference</title><style>h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm }
a:link { color: #004faf; text-decoration: none }
a:visited { color: #672967; text-decoration: none }
td.postheader { font-family: sans-serif }
tr.address { font-family: sans-serif }
body { background: #ffffff; color: black; }
</style></head><body><table border="0" cellpadding="0" cellspacing="0" width="100%"><tr /><td align="left" valign="top" width="32"><img align="left" border="0" height="32" src="images/rb-logo.png" width="32" /></td><td width="1">&#160;&#160;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a>&#160;&#183; <a href="classes.html"><font color="#004faf">All Classes</font></a>&#160;&#183; <a href="modules.html"><font color="#004faf">Modules</font></a></td></table><h1 align="center">QTcpServer Class Reference<br /><sup><sup>[<a href="qtnetwork.html">QtNetwork</a> module]</sup></sup></h1><p>The QTcpServer class provides a TCP-based server. <a href="#details">More...</a></p>

<p>Inherits <a href="qobject.html">QObject</a>.</p><h3>Methods</h3><ul><li><div class="fn" /><b><a href="qtcpserver.html#QTcpServer">__init__</a></b> (<i>self</i>, QObject&#160;<i>parent</i>&#160;=&#160;None)</li><li><div class="fn" /><b><a href="qtcpserver.html#addPendingConnection">addPendingConnection</a></b> (<i>self</i>, QTcpSocket&#160;<i>socket</i>)</li><li><div class="fn" /><b><a href="qtcpserver.html#close">close</a></b> (<i>self</i>)</li><li><div class="fn" />QString <b><a href="qtcpserver.html#errorString">errorString</a></b> (<i>self</i>)</li><li><div class="fn" />bool <b><a href="qtcpserver.html#hasPendingConnections">hasPendingConnections</a></b> (<i>self</i>)</li><li><div class="fn" /><b><a href="qtcpserver.html#incomingConnection">incomingConnection</a></b> (<i>self</i>, int&#160;<i>handle</i>)</li><li><div class="fn" />bool <b><a href="qtcpserver.html#isListening">isListening</a></b> (<i>self</i>)</li><li><div class="fn" />bool <b><a href="qtcpserver.html#listen">listen</a></b> (<i>self</i>, QHostAddress&#160;<i>address</i>&#160;=&#160;QHostAddress.Any, int&#160;<i>port</i>&#160;=&#160;0)</li><li><div class="fn" />int <b><a href="qtcpserver.html#maxPendingConnections">maxPendingConnections</a></b> (<i>self</i>)</li><li><div class="fn" />QTcpSocket <b><a href="qtcpserver.html#nextPendingConnection">nextPendingConnection</a></b> (<i>self</i>)</li><li><div class="fn" />QNetworkProxy <b><a href="qtcpserver.html#proxy">proxy</a></b> (<i>self</i>)</li><li><div class="fn" />QHostAddress <b><a href="qtcpserver.html#serverAddress">serverAddress</a></b> (<i>self</i>)</li><li><div class="fn" />QAbstractSocket.SocketError <b><a href="qtcpserver.html#serverError">serverError</a></b> (<i>self</i>)</li><li><div class="fn" />int <b><a href="qtcpserver.html#serverPort">serverPort</a></b> (<i>self</i>)</li><li><div class="fn" /><b><a href="qtcpserver.html#setMaxPendingConnections">setMaxPendingConnections</a></b> (<i>self</i>, int&#160;<i>numConnections</i>)</li><li><div class="fn" /><b><a href="qtcpserver.html#setProxy">setProxy</a></b> (<i>self</i>, QNetworkProxy&#160;<i>networkProxy</i>)</li><li><div class="fn" />bool <b><a href="qtcpserver.html#setSocketDescriptor">setSocketDescriptor</a></b> (<i>self</i>, int&#160;<i>socketDescriptor</i>)</li><li><div class="fn" />int <b><a href="qtcpserver.html#socketDescriptor">socketDescriptor</a></b> (<i>self</i>)</li><li><div class="fn" />(bool, bool&#160;<i>timedOut</i>) <b><a href="qtcpserver.html#waitForNewConnection">waitForNewConnection</a></b> (<i>self</i>, int&#160;<i>msecs</i>&#160;=&#160;0)</li></ul><h3>Qt Signals</h3><ul><li><div class="fn" />void <b><a href="qtcpserver.html#newConnection">newConnection</a></b> ()</li></ul><a name="details" /><hr /><h2>Detailed Description</h2><p>The QTcpServer class provides a TCP-based server.</p>
<p>This class makes it possible to accept incoming TCP connections.
You can specify the port or have QTcpServer pick one automatically.
You can listen on a specific address or on all the machine's
addresses.</p>
<p>Call <a href="qtcpserver.html#listen">listen</a>() to have the
server listen for incoming connections. The <a href="qtcpserver.html#newConnection">newConnection</a>() signal is then
emitted each time a client connects to the server.</p>
<p>Call <a href="qtcpserver.html#nextPendingConnection">nextPendingConnection</a>()
to accept the pending connection as a connected <a href="qtcpsocket.html">QTcpSocket</a>. The function returns a pointer to
a <a href="qtcpsocket.html">QTcpSocket</a> in <a href="qabstractsocket.html#SocketState-enum">QAbstractSocket.ConnectedState</a>
that you can use for communicating with the client.</p>
<p>If an error occurs, <a href="qtcpserver.html#serverError">serverError</a>() returns the type of
error, and <a href="qtcpserver.html#errorString">errorString</a>()
can be called to get a human readable description of what
happened.</p>
<p>When listening for connections, the address and port on which
the server is listening are available as <a href="qtcpserver.html#serverAddress">serverAddress</a>() and <a href="qtcpserver.html#serverPort">serverPort</a>().</p>
<p>Calling <a href="qtcpserver.html#close">close</a>() makes
QTcpServer stop listening for incoming connections.</p>
<p>Although QTcpServer is mostly designed for use with an event
loop, it's possible to use it without one. In that case, you must
use <a href="qtcpserver.html#waitForNewConnection">waitForNewConnection</a>(),
which blocks until either a connection is available or a timeout
expires.</p>
<a id="symbian-platform-security-requirements" name="symbian-platform-security-requirements" />
<h3>Symbian Platform Security Requirements</h3>
<p>On Symbian, processes which use this class must have the
<tt>NetworkServices</tt> platform security capability. If the
client process lacks this capability, it will lead to a panic.</p>
<p>Platform security capabilities are added via the <a href="qmake-variable-reference.html#target-capability">TARGET.CAPABILITY</a>
qmake variable.</p>
<hr /><h2>Method Documentation</h2><h3 class="fn"><a name="QTcpServer" />QTcpServer.__init__ (<i>self</i>, <a href="qobject.html">QObject</a>&#160;<i>parent</i>&#160;=&#160;None)</h3><p>The <i>parent</i> argument, if not None, causes <i>self</i> to be owned by Qt instead of PyQt.</p><p>Constructs a <a href="qtcpserver.html">QTcpServer</a>
object.</p>
<p><i>parent</i> is passed to the <a href="qobject.html">QObject</a> constructor.</p>
<p><b>See also</b> <a href="qtcpserver.html#listen">listen</a>()
and <a href="qtcpserver.html#setSocketDescriptor">setSocketDescriptor</a>().</p>


<h3 class="fn"><a name="addPendingConnection" />QTcpServer.addPendingConnection (<i>self</i>, <a href="qtcpsocket.html">QTcpSocket</a>&#160;<i>socket</i>)</h3><p>This function is called by <a href="qtcpserver.html#incomingConnection">QTcpServer.incomingConnection</a>()
to add the <i>socket</i> to the list of pending incoming
connections.</p>
<p><b>Note:</b> Don't forget to call this member from reimplemented
<a href="qtcpserver.html#incomingConnection">incomingConnection</a>() if
you do not want to break the Pending Connections mechanism.</p>
<p>This function was introduced in Qt 4.7.</p>
<p><b>See also</b> <a href="qtcpserver.html#incomingConnection">incomingConnection</a>().</p>


<h3 class="fn"><a name="close" />QTcpServer.close (<i>self</i>)</h3><p>Closes the server. The server will no longer listen for incoming
connections.</p>
<p><b>See also</b> <a href="qtcpserver.html#listen">listen</a>().</p>


<h3 class="fn"><a name="errorString" />QString QTcpServer.errorString (<i>self</i>)</h3><p>Returns a human readable description of the last error that
occurred.</p>
<p><b>See also</b> <a href="qtcpserver.html#serverError">serverError</a>().</p>


<h3 class="fn"><a name="hasPendingConnections" />bool QTcpServer.hasPendingConnections (<i>self</i>)</h3><p>Returns true if the server has a pending connection; otherwise
returns false.</p>
<p><b>See also</b> <a href="qtcpserver.html#nextPendingConnection">nextPendingConnection</a>()
and <a href="qtcpserver.html#setMaxPendingConnections">setMaxPendingConnections</a>().</p>


<h3 class="fn"><a name="incomingConnection" />QTcpServer.incomingConnection (<i>self</i>, int&#160;<i>handle</i>)</h3><p>This virtual function is called by <a href="qtcpserver.html">QTcpServer</a> when a new connection is
available. The <i>socketDescriptor</i> argument is the native
socket descriptor for the accepted connection.</p>
<p>The base implementation creates a <a href="qtcpsocket.html">QTcpSocket</a>, sets the socket descriptor and
then stores the <a href="qtcpsocket.html">QTcpSocket</a> in an
internal list of pending connections. Finally <a href="qtcpserver.html#newConnection">newConnection</a>() is emitted.</p>
<p>Reimplement this function to alter the server's behavior when a
connection is available.</p>
<p>If this server is using <a href="qnetworkproxy.html">QNetworkProxy</a> then the
<i>socketDescriptor</i> may not be usable with native socket
functions, and should only be used with <a href="qabstractsocket.html#setSocketDescriptor">QTcpSocket.setSocketDescriptor</a>().</p>
<p><b>Note:</b> If you want to handle an incoming connection as a
new <a href="qtcpsocket.html">QTcpSocket</a> object in another
thread you have to pass the socketDescriptor to the other thread
and create the <a href="qtcpsocket.html">QTcpSocket</a> object
there and use its <a href="qtcpserver.html#setSocketDescriptor">setSocketDescriptor</a>()
method.</p>
<p><b>See also</b> <a href="qtcpserver.html#newConnection">newConnection</a>(), <a href="qtcpserver.html#nextPendingConnection">nextPendingConnection</a>(),
and <a href="qtcpserver.html#addPendingConnection">addPendingConnection</a>().</p>


<h3 class="fn"><a name="isListening" />bool QTcpServer.isListening (<i>self</i>)</h3><p>Returns true if the server is currently listening for incoming
connections; otherwise returns false.</p>
<p><b>See also</b> <a href="qtcpserver.html#listen">listen</a>().</p>


<h3 class="fn"><a name="listen" />bool QTcpServer.listen (<i>self</i>, <a href="qhostaddress.html">QHostAddress</a>&#160;<i>address</i>&#160;=&#160;QHostAddress.Any, int&#160;<i>port</i>&#160;=&#160;0)</h3><p>Tells the server to listen for incoming connections on address
<i>address</i> and port <i>port</i>. If <i>port</i> is 0, a port is
chosen automatically. If <i>address</i> is <a href="qhostaddress.html#SpecialAddress-enum">QHostAddress.Any</a>, the
server will listen on all network interfaces.</p>
<p>Returns true on success; otherwise returns false.</p>
<p><b>See also</b> <a href="qtcpserver.html#isListening">isListening</a>().</p>


<h3 class="fn"><a name="maxPendingConnections" />int QTcpServer.maxPendingConnections (<i>self</i>)</h3><p>Returns the maximum number of pending accepted connections. The
default is 30.</p>
<p><b>See also</b> <a href="qtcpserver.html#setMaxPendingConnections">setMaxPendingConnections</a>()
and <a href="qtcpserver.html#hasPendingConnections">hasPendingConnections</a>().</p>


<h3 class="fn"><a name="nextPendingConnection" /><a href="qtcpsocket.html">QTcpSocket</a> QTcpServer.nextPendingConnection (<i>self</i>)</h3><p>Returns the next pending connection as a connected <a href="qtcpsocket.html">QTcpSocket</a> object.</p>
<p>The socket is created as a child of the server, which means that
it is automatically deleted when the <a href="qtcpserver.html">QTcpServer</a> object is destroyed. It is still a
good idea to delete the object explicitly when you are done with
it, to avoid wasting memory.</p>
<p>0 is returned if this function is called when there are no
pending connections.</p>
<p><b>Note:</b> The returned <a href="qtcpsocket.html">QTcpSocket</a> object cannot be used from another
thread. If you want to use an incoming connection from another
thread, you need to override <a href="qtcpserver.html#incomingConnection">incomingConnection</a>().</p>
<p><b>See also</b> <a href="qtcpserver.html#hasPendingConnections">hasPendingConnections</a>().</p>


<h3 class="fn"><a name="proxy" /><a href="qnetworkproxy.html">QNetworkProxy</a> QTcpServer.proxy (<i>self</i>)</h3><p>Returns the network proxy for this socket. By default <a href="qnetworkproxy.html#ProxyType-enum">QNetworkProxy.DefaultProxy</a>
is used.</p>
<p>This function was introduced in Qt 4.1.</p>
<p><b>See also</b> <a href="qtcpserver.html#setProxy">setProxy</a>() and <a href="qnetworkproxy.html">QNetworkProxy</a>.</p>


<h3 class="fn"><a name="serverAddress" /><a href="qhostaddress.html">QHostAddress</a> QTcpServer.serverAddress (<i>self</i>)</h3><p>Returns the server's address if the server is listening for
connections; otherwise returns <a href="qhostaddress.html#SpecialAddress-enum">QHostAddress.Null</a>.</p>
<p><b>See also</b> <a href="qtcpserver.html#serverPort">serverPort</a>() and <a href="qtcpserver.html#listen">listen</a>().</p>


<h3 class="fn"><a name="serverError" /><a href="qabstractsocket.html#SocketError-enum">QAbstractSocket.SocketError</a> QTcpServer.serverError (<i>self</i>)</h3><p>Returns an error code for the last error that occurred.</p>
<p><b>See also</b> <a href="qtcpserver.html#errorString">errorString</a>().</p>


<h3 class="fn"><a name="serverPort" />int QTcpServer.serverPort (<i>self</i>)</h3><p>Returns the server's port if the server is listening for
connections; otherwise returns 0.</p>
<p><b>See also</b> <a href="qtcpserver.html#serverAddress">serverAddress</a>() and <a href="qtcpserver.html#listen">listen</a>().</p>


<h3 class="fn"><a name="setMaxPendingConnections" />QTcpServer.setMaxPendingConnections (<i>self</i>, int&#160;<i>numConnections</i>)</h3><p>Sets the maximum number of pending accepted connections to
<i>numConnections</i>. <a href="qtcpserver.html">QTcpServer</a>
will accept no more than <i>numConnections</i> incoming connections
before <a href="qtcpserver.html#nextPendingConnection">nextPendingConnection</a>()
is called. By default, the limit is 30 pending connections.</p>
<p>Clients may still able to connect after the server has reached
its maximum number of pending connections (i.e., <a href="qtcpsocket.html">QTcpSocket</a> can still emit the connected()
signal). <a href="qtcpserver.html">QTcpServer</a> will stop
accepting the new connections, but the operating system may still
keep them in queue.</p>
<p><b>See also</b> <a href="qtcpserver.html#maxPendingConnections">maxPendingConnections</a>()
and <a href="qtcpserver.html#hasPendingConnections">hasPendingConnections</a>().</p>


<h3 class="fn"><a name="setProxy" />QTcpServer.setProxy (<i>self</i>, <a href="qnetworkproxy.html">QNetworkProxy</a>&#160;<i>networkProxy</i>)</h3><p>Sets the explicit network proxy for this socket to
<i>networkProxy</i>.</p>
<p>To disable the use of a proxy for this socket, use the <a href="qnetworkproxy.html#ProxyType-enum">QNetworkProxy.NoProxy</a>
proxy type:</p>
<pre class="cpp">
 server<span class="operator">-</span><span class="operator">&gt;</span>setProxy(<span class="type"><a href="qnetworkproxy.html">QNetworkProxy</a></span><span class="operator">.</span>NoProxy);
</pre>
<p>This function was introduced in Qt 4.1.</p>
<p><b>See also</b> <a href="qtcpserver.html#proxy">proxy</a>() and
<a href="qnetworkproxy.html">QNetworkProxy</a>.</p>


<h3 class="fn"><a name="setSocketDescriptor" />bool QTcpServer.setSocketDescriptor (<i>self</i>, int&#160;<i>socketDescriptor</i>)</h3><p>Sets the socket descriptor this server should use when listening
for incoming connections to <i>socketDescriptor</i>. Returns true
if the socket is set successfully; otherwise returns false.</p>
<p>The socket is assumed to be in listening state.</p>
<p><b>See also</b> <a href="qtcpserver.html#socketDescriptor">socketDescriptor</a>() and
<a href="qtcpserver.html#isListening">isListening</a>().</p>


<h3 class="fn"><a name="socketDescriptor" />int QTcpServer.socketDescriptor (<i>self</i>)</h3><p>Returns the native socket descriptor the server uses to listen
for incoming instructions, or -1 if the server is not
listening.</p>
<p>If the server is using <a href="qnetworkproxy.html">QNetworkProxy</a>, the returned descriptor may
not be usable with native socket functions.</p>
<p><b>See also</b> <a href="qtcpserver.html#setSocketDescriptor">setSocketDescriptor</a>() and
<a href="qtcpserver.html#isListening">isListening</a>().</p>


<h3 class="fn"><a name="waitForNewConnection" />(bool, bool&#160;<i>timedOut</i>) QTcpServer.waitForNewConnection (<i>self</i>, int&#160;<i>msecs</i>&#160;=&#160;0)</h3><p>Waits for at most <i>msec</i> milliseconds or until an incoming
connection is available. Returns true if a connection is available;
otherwise returns false. If the operation timed out and
<i>timedOut</i> is not 0, *<i>timedOut</i> will be set to true.</p>
<p>This is a blocking function call. Its use is disadvised in a
single-threaded GUI application, since the whole application will
stop responding until the function returns. waitForNewConnection()
is mostly useful when there is no event loop available.</p>
<p>The non-blocking alternative is to connect to the <a href="qtcpserver.html#newConnection">newConnection</a>() signal.</p>
<p>If msec is -1, this function will not time out.</p>
<p><b>See also</b> <a href="qtcpserver.html#hasPendingConnections">hasPendingConnections</a>()
and <a href="qtcpserver.html#nextPendingConnection">nextPendingConnection</a>().</p>
<hr /><h2>Qt Signal Documentation</h2><h3 class="fn"><a name="newConnection" />void newConnection ()</h3><p>This is the default overload of this signal.</p><p>This signal is emitted every time a new connection is
available.</p>
<p><b>See also</b> <a href="qtcpserver.html#hasPendingConnections">hasPendingConnections</a>()
and <a href="qtcpserver.html#nextPendingConnection">nextPendingConnection</a>().</p>


<address><hr /><div align="center"><table border="0" cellspacing="0" width="100%"><tr class="address"><td align="left" width="25%">PyQt&#160;4.12.1 for X11</td><td align="center" width="50%">Copyright &#169; <a href="http://www.riverbankcomputing.com">Riverbank&#160;Computing&#160;Ltd</a> and <a href="http://www.qt.io">The Qt Company</a> 2015</td><td align="right" width="25%">Qt&#160;4.8.7</td></tr></table></div></address></body></html>