<?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="index.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#addPendingConnection">addPendingConnection</a></b> (<i>self</i>, QTcpSocket <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 <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 <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 <i>numConnections</i>)</li><li><div class="fn" /><b><a href="qtcpserver.html#setProxy">setProxy</a></b> (<i>self</i>, QNetworkProxy <i>networkProxy</i>)</li><li><div class="fn" />bool <b><a href="qtcpserver.html#setSocketDescriptor">setSocketDescriptor</a></b> (<i>self</i>, int <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 <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> <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> <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><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> <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 <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> <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><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 <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> <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">></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 <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 <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><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 4.10.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> 2012</td><td align="right" width="25%">Qt 4.8.5</td></tr></table></div></address></body></html>