PySide6.QtNetwork.QTcpServer¶
- class QTcpServer¶
- The - QTcpServerclass provides a TCP-based server. More…- Inherited by: - QSslServer- Synopsis¶- Methods¶- def - __init__()
- def - close()
- def - errorString()
- def - isListening()
- def - listen()
- def - pauseAccepting()
- def - proxy()
- def - serverAddress()
- def - serverError()
- def - serverPort()
- def - setProxy()
 - Virtual methods¶- Signals¶- def - acceptError()
- def - newConnection()
 - Note - This documentation may contain snippets that were automatically translated from C++ to Python. We always welcome contributions to the snippet translation. If you see an issue with the translation, you can also let us know by creating a ticket on https:/bugreports.qt.io/projects/PYSIDE - Detailed Description¶- This class makes it possible to accept incoming TCP connections. You can specify the port or have - QTcpServerpick 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. The- newConnection()signal is then emitted each time a client connects to the server. When the client connection has been added to the pending connection queue using the- addPendingConnection()function, the- pendingConnectionAvailable()signal is emitted.- Call - nextPendingConnection()to accept the pending connection as a connected- QTcpSocket. The function returns a pointer to a- QTcpSocketin- ConnectedStatethat you can use for communicating with the client.- If an error occurs, - serverError()returns the type of error, and- 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 - serverAddress()and- serverPort().- Calling - close()makes- QTcpServerstop listening for incoming connections.- Although - QTcpServeris mostly designed for use with an event loop, it’s possible to use it without one. In that case, you must use- waitForNewConnection(), which blocks until either a connection is available or a timeout expires.- See also - QTcpSocketFortune Server Threaded Fortune Server Torrent Example- Constructs a - QTcpServerobject.- parentis passed to the QObject constructor.- See also - acceptError(socketError)¶
- Parameters:
- socketError – - SocketError
 
 - This signal is emitted when accepting a new connection results in an error. The - socketErrorparameter describes the type of error that occurred.- See also - addPendingConnection(socket)¶
- Parameters:
- socket – - QTcpSocket
 
 - This function is called by - incomingConnection()to add the- socketto 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. This function emits the- pendingConnectionAvailable()signal after the socket has been added.- close()¶
 - Closes the server. The server will no longer listen for incoming connections. - See also - errorString()¶
- Return type:
- str 
 
 - Returns a human readable description of the last error that occurred. - See also - hasPendingConnections()¶
- Return type:
- bool 
 
 - Returns - trueif the server has a pending connection; otherwise returns- false.- incomingConnection(handle)¶
- Parameters:
- handle – - qintptr
 
 - This virtual function is called by - QTcpServerwhen a new connection is available. The- socketDescriptorargument is the native socket descriptor for the accepted connection.- The base implementation creates a - QTcpSocket, sets the socket descriptor and then stores the- QTcpSocketin an internal list of pending connections. Finally- newConnection()is emitted.- Reimplement this function to alter the server’s behavior when a connection is available. - If this server is using - QNetworkProxythen the- socketDescriptormay not be usable with native socket functions, and should only be used with- setSocketDescriptor().- 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 - QTcpSocketobject in another thread you have to pass the- socketDescriptorto the other thread and create the- QTcpSocketobject there and use its- setSocketDescriptor()method.- isListening()¶
- Return type:
- bool 
 
 - Returns - trueif the server is currently listening for incoming connections; otherwise returns- false.- See also - listen([address=QHostAddress.Any[, port=0]])¶
- Parameters:
- address – - QHostAddress
- port – int 
 
- Return type:
- bool 
 
 - Tells the server to listen for incoming connections on address - addressand port- port. If- portis 0, a port is chosen automatically. If- addressis- Any, the server will listen on all network interfaces.- Returns - trueon success; otherwise returns- false.- See also - listenBacklogSize()¶
- Return type:
- int 
 
 - Returns the backlog queue size of to be accepted connections. - See also - maxPendingConnections()¶
- Return type:
- int 
 
 - Returns the maximum number of pending accepted connections. The default is 30. - newConnection()¶
 - This signal is emitted every time a new connection is available, regardless of whether it has been added to the pending connections queue or not. - nextPendingConnection()¶
- Return type:
 
 - Returns the next pending connection as a connected - QTcpSocketobject.- The socket is created as a child of the server, which means that it is automatically deleted when the - QTcpServerobject is destroyed. It is still a good idea to delete the object explicitly when you are done with it, to avoid wasting memory.- Noneis returned if this function is called when there are no pending connections.- Note - The returned - QTcpSocketobject cannot be used from another thread. If you want to use an incoming connection from another thread, you need to override- incomingConnection().- See also - pauseAccepting()¶
 - Pauses accepting new connections. Queued connections will remain in queue. - See also - pendingConnectionAvailable()¶
 - This signal is emitted every time a new connection has been added to the pending connections queue. - proxy()¶
- Return type:
 
 - Returns the network proxy for this socket. By default - DefaultProxyis used.- See also - resumeAccepting()¶
 - Resumes accepting new connections. - See also - serverAddress()¶
- Return type:
 
 - Returns the server’s address if the server is listening for connections; otherwise returns - Null.- See also - serverError()¶
- Return type:
 
 - Returns an error code for the last error that occurred. - See also - serverPort()¶
- Return type:
- int 
 
 - Returns the server’s port if the server is listening for connections; otherwise returns 0. - See also - setListenBacklogSize(size)¶
- Parameters:
- size – int 
 
 - Sets the backlog queue size of to be accepted connections to - size. The operating system might reduce or ignore this value. By default, the queue size is 50.- setMaxPendingConnections(numConnections)¶
- Parameters:
- numConnections – int 
 
 - Sets the maximum number of pending accepted connections to - numConnections.- QTcpServerwill accept no more than- numConnectionsincoming connections before- 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., - QTcpSocketcan still emit the connected() signal).- QTcpServerwill stop accepting the new connections, but the operating system may still keep them in queue.- setProxy(networkProxy)¶
- Parameters:
- networkProxy – - QNetworkProxy
 
 - Warning - This section contains snippets that were automatically translated from C++ to Python and may contain errors. - Sets the explicit network proxy for this socket to - networkProxy.- To disable the use of a proxy for this socket, use the - NoProxyproxy type:- server.setProxy(QNetworkProxy.NoProxy) - See also - setSocketDescriptor(socketDescriptor)¶
- Parameters:
- socketDescriptor – - qintptr
- Return type:
- bool 
 
 - Sets the socket descriptor this server should use when listening for incoming connections to - socketDescriptor. Returns- trueif the socket is set successfully; otherwise returns- false.- The socket is assumed to be in listening state. - See also - 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 - waitForNewConnection(msec)¶
- Parameters:
- msec – int 
- Return type:
- (retval, timedOut) 
 
 - Waits for at most - msecmilliseconds or until an incoming connection is available. Returns- trueif a connection is available; otherwise returns- false. If the operation timed out and- timedOutis not- None, *``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. waitForNewConnection() 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.