
We bake cookies in your browser for a better experience. Using this site means that you consent.Read More
TheQTcpServer class provides a TCP-based server.More...
| Header: | #include <QTcpServer> |
| Inherits: | QObject |
Note: All functions in this class arereentrant.
| QTcpServer(QObject * parent = 0) | |
| virtual | ~QTcpServer() |
| void | close() |
| QString | errorString() const |
| virtual bool | hasPendingConnections() const |
| bool | isListening() const |
| bool | listen(const QHostAddress & address = QHostAddress::Any, quint16 port = 0) |
| int | maxPendingConnections() const |
| virtual QTcpSocket * | nextPendingConnection() |
| QNetworkProxy | proxy() const |
| QHostAddress | serverAddress() const |
| QAbstractSocket::SocketError | serverError() const |
| quint16 | serverPort() const |
| void | setMaxPendingConnections(int numConnections) |
| void | setProxy(const QNetworkProxy & networkProxy) |
| bool | setSocketDescriptor(int socketDescriptor) |
| int | socketDescriptor() const |
| bool | waitForNewConnection(int msec = 0, bool * timedOut = 0) |
| void | newConnection() |
| void | addPendingConnection(QTcpSocket * socket) |
| virtual void | incomingConnection(int socketDescriptor) |
TheQTcpServer class provides a TCP-based server.
This class makes it possible to accept incoming TCP connections. You can specify the port or haveQTcpServer pick one automatically. You can listen on a specific address or on all the machine's addresses.
Calllisten() to have the server listen for incoming connections. ThenewConnection() signal is then emitted each time a client connects to the server.
CallnextPendingConnection() to accept the pending connection as a connectedQTcpSocket. The function returns a pointer to aQTcpSocket inQAbstractSocket::ConnectedState 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 asserverAddress() andserverPort().
Callingclose() makesQTcpServer stop listening for incoming connections.
AlthoughQTcpServer 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.
On Symbian, processes which use this class must have theNetworkServices platform security capability. If the client process lacks this capability, it will lead to a panic.
Platform security capabilities are added via theTARGET.CAPABILITY qmake variable.
See alsoQTcpSocket,Fortune Server Example,Threaded Fortune Server Example,Loopback Example, andTorrent Example.
Constructs aQTcpServer object.
parent is passed to theQObject constructor.
See alsolisten() andsetSocketDescriptor().
[virtual]QTcpServer::~QTcpServer()Destroys theQTcpServer object. If the server is listening for connections, the socket is automatically closed.
Any clientQTcpSockets that are still connected must either disconnect or be reparented before the server is deleted.
See alsoclose().
[protected]void QTcpServer::addPendingConnection(QTcpSocket * socket)This function is called byQTcpServer::incomingConnection() to add thesocket to the list of pending incoming connections.
Note:Don't forget to call this member from reimplementedincomingConnection() if you do not want to break the Pending Connections mechanism.
This function was introduced in Qt 4.7.
See alsoincomingConnection().
Closes the server. The server will no longer listen for incoming connections.
See alsolisten().
Returns a human readable description of the last error that occurred.
See alsoserverError().
[virtual]bool QTcpServer::hasPendingConnections() constReturns true if the server has a pending connection; otherwise returns false.
See alsonextPendingConnection() andsetMaxPendingConnections().
[virtual protected]void QTcpServer::incomingConnection(int socketDescriptor)This virtual function is called byQTcpServer when a new connection is available. ThesocketDescriptor argument is the native socket descriptor for the accepted connection.
The base implementation creates aQTcpSocket, 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 usingQNetworkProxy then thesocketDescriptor may not be usable with native socket functions, and should only be used withQTcpSocket::setSocketDescriptor().
Note:If you want to handle an incoming connection as a newQTcpSocket object in another thread you have to pass thesocketDescriptor to the other thread and create theQTcpSocket object there and use itssetSocketDescriptor() method.
See alsonewConnection(),nextPendingConnection(), andaddPendingConnection().
Returns true if the server is currently listening for incoming connections; otherwise returns false.
See alsolisten().
Tells the server to listen for incoming connections on addressaddress and portport. Ifport is 0, a port is chosen automatically. Ifaddress isQHostAddress::Any, the server will listen on all network interfaces.
Returns true on success; otherwise returns false.
See alsoisListening().
Returns the maximum number of pending accepted connections. The default is 30.
See alsosetMaxPendingConnections() andhasPendingConnections().
[signal]void QTcpServer::newConnection()This signal is emitted every time a new connection is available.
See alsohasPendingConnections() andnextPendingConnection().
[virtual]QTcpSocket * QTcpServer::nextPendingConnection()Returns the next pending connection as a connectedQTcpSocket object.
The socket is created as a child of the server, which means that it is automatically deleted when theQTcpServer 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 returnedQTcpSocket object cannot be used from another thread. If you want to use an incoming connection from another thread, you need to overrideincomingConnection().
See alsohasPendingConnections().
Returns the network proxy for this socket. By defaultQNetworkProxy::DefaultProxy is used.
This function was introduced in Qt 4.1.
See alsosetProxy() andQNetworkProxy.
Returns the server's address if the server is listening for connections; otherwise returnsQHostAddress::Null.
See alsoserverPort() andlisten().
Returns an error code for the last error that occurred.
See alsoerrorString().
Returns the server's port if the server is listening for connections; otherwise returns 0.
See alsoserverAddress() andlisten().
Sets the maximum number of pending accepted connections tonumConnections.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.
See alsomaxPendingConnections() andhasPendingConnections().
Sets the explicit network proxy for this socket tonetworkProxy.
To disable the use of a proxy for this socket, use theQNetworkProxy::NoProxy proxy type:
server->setProxy(QNetworkProxy::NoProxy);
This function was introduced in Qt 4.1.
See alsoproxy() andQNetworkProxy.
Sets the socket descriptor this server should use when listening for incoming connections tosocketDescriptor. Returns true if the socket is set successfully; otherwise returns false.
The socket is assumed to be in listening state.
See alsosocketDescriptor() andisListening().
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 usingQNetworkProxy, the returned descriptor may not be usable with native socket functions.
See alsosetSocketDescriptor() andisListening().
Waits for at mostmsec milliseconds or until an incoming connection is available. Returns true if a connection is available; otherwise returns false. If the operation timed out andtimedOut 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. waitForNewConnection() is mostly useful when there is no event loop available.
The non-blocking alternative is to connect to thenewConnection() signal.
If msec is -1, this function will not time out.
See alsohasPendingConnections() andnextPendingConnection().
© 2016 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 theGNU 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.