QTcpServer¶
The
QTcpServer
class provides a TCP-based server. More…
Synopsis¶
Functions¶
def
addPendingConnection
(socket)def
close
()def
errorString
()def
isListening
()def
listen
([address=QHostAddress.Any[, port=0]])def
maxPendingConnections
()def
pauseAccepting
()def
proxy
()def
resumeAccepting
()def
serverAddress
()def
serverError
()def
serverPort
()def
setMaxPendingConnections
(numConnections)def
setProxy
(networkProxy)def
setSocketDescriptor
(socketDescriptor)def
socketDescriptor
()def
waitForNewConnection
(msec)
Virtual functions¶
def
hasPendingConnections
()def
incomingConnection
(handle)def
nextPendingConnection
()
Signals¶
def
acceptError
(socketError)def
newConnection
()
Detailed Description¶
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.Call
listen()
to have the server listen for incoming connections. ThenewConnection()
signal is then emitted each time a client connects to the server.Call
nextPendingConnection()
to accept the pending connection as a connectedQTcpSocket
. The function returns a pointer to aQTcpSocket
inConnectedState
that you can use for communicating with the client.If an error occurs,
serverError()
returns the type of error, anderrorString()
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
serverAddress()
andserverPort()
.Calling
close()
makesQTcpServer
stop listening for incoming connections.Although
QTcpServer
is mostly designed for use with an event loop, it’s possible to use it without one. In that case, you must usewaitForNewConnection()
, which blocks until either a connection is available or a timeout expires.
- class PySide2.QtNetwork.QTcpServer([parent=None])¶
- param parent:
Constructs a
QTcpServer
object.parent
is passed to theQObject
constructor.See also
- PySide2.QtNetwork.QTcpServer.acceptError(socketError)¶
- Parameters:
socketError –
SocketError
- PySide2.QtNetwork.QTcpServer.addPendingConnection(socket)¶
- Parameters:
socket –
PySide2.QtNetwork.QTcpSocket
This function is called by
incomingConnection()
to add thesocket
to the list of pending incoming connections.Note
Don’t forget to call this member from reimplemented
incomingConnection()
if you do not want to break the Pending Connections mechanism.See also
- PySide2.QtNetwork.QTcpServer.close()¶
Closes the server. The server will no longer listen for incoming connections.
See also
- PySide2.QtNetwork.QTcpServer.errorString()¶
- Return type:
str
Returns a human readable description of the last error that occurred.
See also
- PySide2.QtNetwork.QTcpServer.hasPendingConnections()¶
- Return type:
bool
Returns
true
if the server has a pending connection; otherwise returnsfalse
.
- PySide2.QtNetwork.QTcpServer.incomingConnection(handle)¶
- Parameters:
handle –
qintptr
This virtual function is called by
QTcpServer
when a new connection is available. ThesocketDescriptor
argument is the native socket descriptor for the accepted connection.The base implementation creates a
QTcpSocket
, sets the socket descriptor and then stores theQTcpSocket
in an internal list of pending connections. FinallynewConnection()
is emitted.Reimplement this function to alter the server’s behavior when a connection is available.
If this server is using
QNetworkProxy
then thesocketDescriptor
may not be usable with native socket functions, and should only be used withsetSocketDescriptor()
.Note
If another socket is created in the reimplementation of this method, it needs to be added to the Pending Connections mechanism by calling
addPendingConnection()
.Note
If you want to handle an incoming connection as a new
QTcpSocket
object in another thread you have to pass thesocketDescriptor
to the other thread and create theQTcpSocket
object there and use itssetSocketDescriptor()
method.
- PySide2.QtNetwork.QTcpServer.isListening()¶
- Return type:
bool
Returns
true
if the server is currently listening for incoming connections; otherwise returnsfalse
.See also
- PySide2.QtNetwork.QTcpServer.listen([address=QHostAddress.Any[, port=0]])¶
- Parameters:
address –
PySide2.QtNetwork.QHostAddress
port –
quint16
- Return type:
bool
Tells the server to listen for incoming connections on address
address
and portport
. Ifport
is 0, a port is chosen automatically. Ifaddress
isAny
, the server will listen on all network interfaces.Returns
true
on success; otherwise returnsfalse
.See also
- PySide2.QtNetwork.QTcpServer.maxPendingConnections()¶
- Return type:
int
Returns the maximum number of pending accepted connections. The default is 30.
- PySide2.QtNetwork.QTcpServer.newConnection()¶
- PySide2.QtNetwork.QTcpServer.nextPendingConnection()¶
- Return type:
Returns the next pending connection as a connected
QTcpSocket
object.The socket is created as a child of the server, which means that it is automatically deleted when the
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.None
is returned if this function is called when there are no pending connections.Note
The returned
QTcpSocket
object cannot be used from another thread. If you want to use an incoming connection from another thread, you need to overrideincomingConnection()
.See also
- PySide2.QtNetwork.QTcpServer.pauseAccepting()¶
Pauses accepting new connections. Queued connections will remain in queue.
See also
- PySide2.QtNetwork.QTcpServer.proxy()¶
- Return type:
Returns the network proxy for this socket. By default
DefaultProxy
is used.See also
- PySide2.QtNetwork.QTcpServer.resumeAccepting()¶
Resumes accepting new connections.
See also
- PySide2.QtNetwork.QTcpServer.serverAddress()¶
- Return type:
Returns the server’s address if the server is listening for connections; otherwise returns
Null
.See also
- PySide2.QtNetwork.QTcpServer.serverError()¶
- Return type:
Returns an error code for the last error that occurred.
See also
- PySide2.QtNetwork.QTcpServer.serverPort()¶
- Return type:
quint16
Returns the server’s port if the server is listening for connections; otherwise returns 0.
See also
- PySide2.QtNetwork.QTcpServer.setMaxPendingConnections(numConnections)¶
- Parameters:
numConnections – int
Sets the maximum number of pending accepted connections to
numConnections
.QTcpServer
will accept no more thannumConnections
incoming connections beforenextPendingConnection()
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.,
QTcpSocket
can still emit the connected() signal).QTcpServer
will stop accepting the new connections, but the operating system may still keep them in queue.
- PySide2.QtNetwork.QTcpServer.setProxy(networkProxy)¶
- Parameters:
networkProxy –
PySide2.QtNetwork.QNetworkProxy
Sets the explicit network proxy for this socket to
networkProxy
.To disable the use of a proxy for this socket, use the
NoProxy
proxy type:server.setProxy(QNetworkProxy.NoProxy)
See also
- PySide2.QtNetwork.QTcpServer.setSocketDescriptor(socketDescriptor)¶
- Parameters:
socketDescriptor –
qintptr
- Return type:
bool
Sets the socket descriptor this server should use when listening for incoming connections to
socketDescriptor
. Returnstrue
if the socket is set successfully; otherwise returnsfalse
.The socket is assumed to be in listening state.
See also
- PySide2.QtNetwork.QTcpServer.socketDescriptor()¶
- Return type:
qintptr
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
QNetworkProxy
, the returned descriptor may not be usable with native socket functions.See also
- PySide2.QtNetwork.QTcpServer.waitForNewConnection(msec)¶
- Parameters:
msec – int
- Return type:
(retval, timeOut)
Waits for at most
msec
milliseconds or until an incoming connection is available. Returnstrue
if a connection is available; otherwise returnsfalse
. If the operation timed out andtimedOut
is notNone
, *``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. is mostly useful when there is no event loop available.
The non-blocking alternative is to connect to the
newConnection()
signal.If msec is -1, this function will not time out.
© 2022 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.