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
|
<?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">  </td><td class="postheader" valign="center"><a href="../pyqt4ref.html"><font color="#004faf">Home</font></a> · <a href="classes.html"><font color="#004faf">All Classes</font></a> · <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 <i>parent</i> = None)</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)</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 <i>address</i> = QHostAddress.Any, int <i>port</i> = 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)</li><li><div class="fn" /><b><a href="qtcpserver.html#setProxy">setProxy</a></b> (<i>self</i>, QNetworkProxy)</li><li><div class="fn" />bool <b><a href="qtcpserver.html#setSocketDescriptor">setSocketDescriptor</a></b> (<i>self</i>, int)</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 <i>timedOut</i>) <b><a href="qtcpserver.html#waitForNewConnection">waitForNewConnection</a></b> (<i>self</i>, int <i>msecs</i> = 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>
<p>See also <a href="qtcpsocket.html">QTcpSocket</a>, <a href="network-fortuneserver.html">Fortune Server Example</a>, <a href="network-threadedfortuneserver.html">Threaded Fortune Server Example</a>, <a href="network-loopback.html">Loopback Example</a>, and <a href="network-torrent.html">Torrent Example</a>.</p>
<hr /><h2>Method Documentation</h2><h3 class="fn"><a name="QTcpServer" />QTcpServer.__init__ (<i>self</i>, <a href="qobject.html">QObject</a> <i>parent</i> = 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>See also <a href="qtcpserver.html#listen">listen</a>() and <a href="qtcpserver.html#setSocketDescriptor">setSocketDescriptor</a>().</p>
<a name="//apple_ref/cpp/instm/QTcpServer/~QTcpServer" />
<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>See also <a href="qtcpserver.html#listen">listen</a>().</p>
<a name="//apple_ref/cpp/instm/QTcpServer/errorString" />
<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>See also <a href="qtcpserver.html#serverError">serverError</a>().</p>
<a name="//apple_ref/cpp/instm/QTcpServer/hasPendingConnections" />
<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>See also <a href="qtcpserver.html#nextPendingConnection">nextPendingConnection</a>() and <a href="qtcpserver.html#setMaxPendingConnections">setMaxPendingConnections</a>().</p>
<a name="//apple_ref/cpp/instm/QTcpServer/incomingConnection" />
<h3 class="fn"><a name="incomingConnection" />QTcpServer.incomingConnection (<i>self</i>, int)</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>See also <a href="qtcpserver.html#newConnection">newConnection</a>() and <a href="qtcpserver.html#nextPendingConnection">nextPendingConnection</a>().</p>
<a name="//apple_ref/cpp/instm/QTcpServer/isListening" />
<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>See also <a href="qtcpserver.html#listen">listen</a>().</p>
<a name="//apple_ref/cpp/instm/QTcpServer/listen" />
<h3 class="fn"><a name="listen" />bool QTcpServer.listen (<i>self</i>, <a href="qhostaddress.html">QHostAddress</a> <i>address</i> = QHostAddress.Any, int <i>port</i> = 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>See also <a href="qtcpserver.html#isListening">isListening</a>().</p>
<a name="//apple_ref/cpp/instm/QTcpServer/maxPendingConnections" />
<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>See also <a href="qtcpserver.html#setMaxPendingConnections">setMaxPendingConnections</a>() and <a href="qtcpserver.html#hasPendingConnections">hasPendingConnections</a>().</p>
<a name="//apple_ref/cpp/instm/QTcpServer/newConnection" />
<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>See also <a href="qtcpserver.html#hasPendingConnections">hasPendingConnections</a>().</p>
<a name="//apple_ref/cpp/instm/QTcpServer/proxy" />
<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>See also <a href="qtcpserver.html#setProxy">setProxy</a>() and <a href="qnetworkproxy.html">QNetworkProxy</a>.</p>
<a name="//apple_ref/cpp/instm/QTcpServer/serverAddress" />
<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>See also <a href="qtcpserver.html#serverPort">serverPort</a>() and <a href="qtcpserver.html#listen">listen</a>().</p>
<a name="//apple_ref/cpp/instm/QTcpServer/serverError" />
<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>See also <a href="qtcpserver.html#errorString">errorString</a>().</p>
<a name="//apple_ref/cpp/instm/QTcpServer/serverPort" />
<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>See also <a href="qtcpserver.html#serverAddress">serverAddress</a>() and <a href="qtcpserver.html#listen">listen</a>().</p>
<a name="//apple_ref/cpp/instm/QTcpServer/setMaxPendingConnections" />
<h3 class="fn"><a name="setMaxPendingConnections" />QTcpServer.setMaxPendingConnections (<i>self</i>, int)</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>See also <a href="qtcpserver.html#maxPendingConnections">maxPendingConnections</a>() and <a href="qtcpserver.html#hasPendingConnections">hasPendingConnections</a>().</p>
<a name="//apple_ref/cpp/instm/QTcpServer/setProxy" />
<h3 class="fn"><a name="setProxy" />QTcpServer.setProxy (<i>self</i>, <a href="qnetworkproxy.html">QNetworkProxy</a>)</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> server->setProxy(QNetworkProxy.NoProxy);</pre>
<p>This function was introduced in Qt 4.1.</p>
<p>See also <a href="qtcpserver.html#proxy">proxy</a>() and <a href="qnetworkproxy.html">QNetworkProxy</a>.</p>
<a name="//apple_ref/cpp/instm/QTcpServer/setSocketDescriptor" />
<h3 class="fn"><a name="setSocketDescriptor" />bool QTcpServer.setSocketDescriptor (<i>self</i>, int)</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>See also <a href="qtcpserver.html#socketDescriptor">socketDescriptor</a>() and <a href="qtcpserver.html#isListening">isListening</a>().</p>
<a name="//apple_ref/cpp/instm/QTcpServer/socketDescriptor" />
<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>See also <a href="qtcpserver.html#setSocketDescriptor">setSocketDescriptor</a>() and <a href="qtcpserver.html#isListening">isListening</a>().</p>
<a name="//apple_ref/cpp/instm/QTcpServer/waitForNewConnection" />
<h3 class="fn"><a name="waitForNewConnection" />(bool, bool <i>timedOut</i>) QTcpServer.waitForNewConnection (<i>self</i>, int <i>msecs</i> = 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>See also <a href="qtcpserver.html#hasPendingConnections">hasPendingConnections</a>() and <a href="qtcpserver.html#nextPendingConnection">nextPendingConnection</a>().</p>
<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>See also <a href="qtcpserver.html#hasPendingConnections">hasPendingConnections</a>() and <a href="qtcpserver.html#nextPendingConnection">nextPendingConnection</a>().</p>
<a name="//apple_ref/cpp/instm/QTcpServer/nextPendingConnection" />
<address><hr /><div align="center"><table border="0" cellspacing="0" width="100%"><tr class="address"><td align="left" width="25%">PyQt 4.7.3 for X11</td><td align="center" width="50%">Copyright © <a href="http://www.riverbankcomputing.com">Riverbank Computing Ltd</a> and <a href="http://www.qtsoftware.com">Nokia</a> 2010</td><td align="right" width="25%">Qt 4.6.2</td></tr></table></div></address></body></html>
|