QTcpServer

Synopsis

Functions

Virtual functions

Signals

Detailed Description

The PySide.QtNetwork.QTcpServer class provides a TCP-based server.

This class makes it possible to accept incoming TCP connections. You can specify the port or have PySide.QtNetwork.QTcpServer pick one automatically. You can listen on a specific address or on all the machine’s addresses.

Call PySide.QtNetwork.QTcpServer.listen() to have the server listen for incoming connections. The PySide.QtNetwork.QTcpServer.newConnection() signal is then emitted each time a client connects to the server.

Call PySide.QtNetwork.QTcpServer.nextPendingConnection() to accept the pending connection as a connected PySide.QtNetwork.QTcpSocket . The function returns a pointer to a PySide.QtNetwork.QTcpSocket in QAbstractSocket.ConnectedState that you can use for communicating with the client.

If an error occurs, PySide.QtNetwork.QTcpServer.serverError() returns the type of error, and PySide.QtNetwork.QTcpServer.errorString() can be called to get a human readable description of what happened.

When listening for connections, the address and port on which the server is listening are available as PySide.QtNetwork.QTcpServer.serverAddress() and PySide.QtNetwork.QTcpServer.serverPort() .

Calling PySide.QtNetwork.QTcpServer.close() makes PySide.QtNetwork.QTcpServer stop listening for incoming connections.

Although PySide.QtNetwork.QTcpServer is mostly designed for use with an event loop, it’s possible to use it without one. In that case, you must use PySide.QtNetwork.QTcpServer.waitForNewConnection() , which blocks until either a connection is available or a timeout expires.

Symbian Platform Security Requirements

On Symbian, processes which use this class must have the NetworkServices platform security capability. If the client process lacks this capability, it will lead to a panic.

Platform security capabilities are added via the TARGET.CAPABILITY qmake variable.

See also

PySide.QtNetwork.QTcpSocket Fortune Server Example Threaded Fortune Server Example Loopback Example Torrent Example

class PySide.QtNetwork.QTcpServer([parent=None])
Parameters:parentPySide.QtCore.QObject

Constructs a PySide.QtNetwork.QTcpServer object.

parent is passed to the PySide.QtCore.QObject constructor.

PySide.QtNetwork.QTcpServer.addPendingConnection(socket)
Parameters:socketPySide.QtNetwork.QTcpSocket

This function is called by QTcpServer.incomingConnection() to add the socket to the list of pending incoming connections.

Note

Don’t forget to call this member from reimplemented PySide.QtNetwork.QTcpServer.incomingConnection() if you do not want to break the Pending Connections mechanism.

PySide.QtNetwork.QTcpServer.close()

Closes the server. The server will no longer listen for incoming connections.

PySide.QtNetwork.QTcpServer.errorString()
Return type:unicode

Returns a human readable description of the last error that occurred.

PySide.QtNetwork.QTcpServer.hasPendingConnections()
Return type:PySide.QtCore.bool

Returns true if the server has a pending connection; otherwise returns false.

PySide.QtNetwork.QTcpServer.incomingConnection(handle)
Parameters:handlePySide.QtCore.int

This virtual function is called by PySide.QtNetwork.QTcpServer when a new connection is available. The socketDescriptor argument is the native socket descriptor for the accepted connection.

The base implementation creates a PySide.QtNetwork.QTcpSocket , sets the socket descriptor and then stores the PySide.QtNetwork.QTcpSocket in an internal list of pending connections. Finally PySide.QtNetwork.QTcpServer.newConnection() is emitted.

Reimplement this function to alter the server’s behavior when a connection is available.

If this server is using PySide.QtNetwork.QNetworkProxy then the socketDescriptor may not be usable with native socket functions, and should only be used with QTcpSocket.setSocketDescriptor() .

Note

If you want to handle an incoming connection as a new PySide.QtNetwork.QTcpSocket object in another thread you have to pass the socketDescriptor to the other thread and create the PySide.QtNetwork.QTcpSocket object there and use its PySide.QtNetwork.QTcpServer.setSocketDescriptor() method.

PySide.QtNetwork.QTcpServer.isListening()
Return type:PySide.QtCore.bool

Returns true if the server is currently listening for incoming connections; otherwise returns false.

PySide.QtNetwork.QTcpServer.listen([address=QHostAddress.Any[, port=0]])
Parameters:
Return type:

PySide.QtCore.bool

Tells the server to listen for incoming connections on address address and port port . If port is 0, a port is chosen automatically. If address is QHostAddress.Any , the server will listen on all network interfaces.

Returns true on success; otherwise returns false.

PySide.QtNetwork.QTcpServer.maxPendingConnections()
Return type:PySide.QtCore.int

Returns the maximum number of pending accepted connections. The default is 30.

PySide.QtNetwork.QTcpServer.newConnection()
PySide.QtNetwork.QTcpServer.nextPendingConnection()
Return type:PySide.QtNetwork.QTcpSocket

Returns the next pending connection as a connected PySide.QtNetwork.QTcpSocket object.

The socket is created as a child of the server, which means that it is automatically deleted when the PySide.QtNetwork.QTcpServer object is destroyed. It is still a good idea to delete the object explicitly when you are done with it, to avoid wasting memory.

0 is returned if this function is called when there are no pending connections.

Note

The returned PySide.QtNetwork.QTcpSocket object cannot be used from another thread. If you want to use an incoming connection from another thread, you need to override PySide.QtNetwork.QTcpServer.incomingConnection() .

PySide.QtNetwork.QTcpServer.proxy()
Return type:PySide.QtNetwork.QNetworkProxy

Returns the network proxy for this socket. By default QNetworkProxy.DefaultProxy is used.

PySide.QtNetwork.QTcpServer.serverAddress()
Return type:PySide.QtNetwork.QHostAddress

Returns the server’s address if the server is listening for connections; otherwise returns QHostAddress.Null .

PySide.QtNetwork.QTcpServer.serverError()
Return type:PySide.QtNetwork.QAbstractSocket.SocketError

Returns an error code for the last error that occurred.

PySide.QtNetwork.QTcpServer.serverPort()
Return type:PySide.QtCore.quint16

Returns the server’s port if the server is listening for connections; otherwise returns 0.

PySide.QtNetwork.QTcpServer.setMaxPendingConnections(numConnections)
Parameters:numConnectionsPySide.QtCore.int

Sets the maximum number of pending accepted connections to numConnections . PySide.QtNetwork.QTcpServer will accept no more than numConnections incoming connections before PySide.QtNetwork.QTcpServer.nextPendingConnection() is called. By default, the limit is 30 pending connections.

Clients may still able to connect after the server has reached its maximum number of pending connections (i.e., PySide.QtNetwork.QTcpSocket can still emit the connected() signal). PySide.QtNetwork.QTcpServer will stop accepting the new connections, but the operating system may still keep them in queue.

PySide.QtNetwork.QTcpServer.setProxy(networkProxy)
Parameters:networkProxyPySide.QtNetwork.QNetworkProxy

Sets the explicit network proxy for this socket to networkProxy .

To disable the use of a proxy for this socket, use the QNetworkProxy.NoProxy proxy type:

server.setProxy(QNetworkProxy.NoProxy)
PySide.QtNetwork.QTcpServer.setSocketDescriptor(socketDescriptor)
Parameters:socketDescriptorPySide.QtCore.int
Return type:PySide.QtCore.bool

Sets the socket descriptor this server should use when listening for incoming connections to socketDescriptor . Returns true if the socket is set successfully; otherwise returns false.

The socket is assumed to be in listening state.

PySide.QtNetwork.QTcpServer.socketDescriptor()
Return type:PySide.QtCore.int

Returns the native socket descriptor the server uses to listen for incoming instructions, or -1 if the server is not listening.

If the server is using PySide.QtNetwork.QNetworkProxy , the returned descriptor may not be usable with native socket functions.

PySide.QtNetwork.QTcpServer.waitForNewConnection(msec)
Parameters:msecPySide.QtCore.int
Return type:(retval, timeOut)

Waits for at most msec milliseconds or until an incoming connection is available. Returns true if a connection is available; otherwise returns false. If the operation timed out and timedOut is not 0, *``timedOut`` will be set to true.

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. PySide.QtNetwork.QTcpServer.waitForNewConnection() is mostly useful when there is no event loop available.

The non-blocking alternative is to connect to the PySide.QtNetwork.QTcpServer.newConnection() signal.

If msec is -1, this function will not time out.