CopperSpice API
1.9.2
|
Provides a TCP-based server. More...
Public Signals | |
void | acceptError (QAbstractSocket::SocketError socketError) |
void | newConnection () |
Public Signals inherited from QObject | |
void | destroyed (QObject *obj=nullptr) |
void | objectNameChanged (const QString &objectName) |
Public Methods | |
QTcpServer (QObject *parent=nullptr) | |
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 () |
void | pauseAccepting () |
QNetworkProxy | proxy () const |
void | resumeAccepting () |
QHostAddress | serverAddress () const |
QAbstractSocket::SocketError | serverError () const |
quint16 | serverPort () const |
void | setMaxPendingConnections (int numConnections) |
void | setProxy (const QNetworkProxy &networkProxy) |
bool | setSocketDescriptor (qintptr socketDescriptor) |
qintptr | socketDescriptor () const |
bool | waitForNewConnection (int msec=0, bool *timedOut=nullptr) |
Public Methods inherited from QObject | |
QObject (QObject *parent=nullptr) | |
~QObject () | |
bool | blockSignals (bool block) |
const QList< QObject * > & | children () const |
bool | connect (const QObject *sender, const QString &signalMethod, const QString &location, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection) |
bool | connect (const QObject *sender, const QString &signalMethod, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection) |
bool | disconnect (const QObject *receiver, const QString &slotMethod=QString ()) const |
bool | disconnect (const QString &signalMethod, const QString &location, const QObject *receiver=nullptr, const QString &slotMethod=QString ()) const |
bool | disconnect (const QString &signalMethod=QString (), const QObject *receiver=nullptr, const QString &slotMethod=QString ()) const |
void | dumpObjectInfo () |
void | dumpObjectTree () |
QList< QString > | dynamicPropertyNames () const |
virtual bool | event (QEvent *event) |
virtual bool | eventFilter (QObject *watched, QEvent *event) |
template<typename T > | |
T | findChild (const QString &childName=QString ()) const |
template<class T > | |
QList< T > | findChildren (const QRegularExpression ®Exp, Qt::FindChildOptions options=Qt::FindChildrenRecursively) const |
template<class T > | |
QList< T > | findChildren (const QString &childName=QString (), Qt::FindChildOptions options=Qt::FindChildrenRecursively) const |
bool | inherits (const QString &className) const |
void | installEventFilter (QObject *filterObj) |
bool | isWidgetType () const |
bool | isWindowType () const |
void | killTimer (int id) |
const QMetaObject * | metaObject () const |
void | moveToThread (QThread *targetThread) |
QString | objectName () const |
QObject * | parent () const |
template<class T = QVariant> | |
T | property (const QString &name) const |
void | removeEventFilter (QObject *obj) |
void | setObjectName (const QString &name) |
void | setParent (QObject *parent) |
bool | setProperty (const QString &name, const QVariant &value) |
bool | signalsBlocked () const |
int | startTimer (int interval, Qt::TimerType timerType=Qt::CoarseTimer) |
QThread * | thread () const |
Protected Methods | |
void | addPendingConnection (QTcpSocket *socket) |
virtual void | incomingConnection (qintptr socketDescriptor) |
Protected Methods inherited from QObject | |
virtual void | childEvent (QChildEvent *event) |
virtual void | connectNotify (const QMetaMethod &signalMethod) const |
virtual void | customEvent (QEvent *event) |
virtual void | disconnectNotify (const QMetaMethod &signalMethod) const |
bool | isSignalConnected (const QMetaMethod &signalMethod) const |
int | receivers (const QString &signal) const |
QObject * | sender () const |
int | senderSignalIndex () const |
virtual void | timerEvent (QTimerEvent *event) |
Additional Inherited Members | |
Public Slots inherited from QObject | |
void | deleteLater () |
Static Public Methods inherited from QObject | |
static bool | connect (const QObject *sender, const QMetaMethod &signalMethod, const QObject *receiver, const QMetaMethod &slotMethod, Qt::ConnectionType type=Qt::AutoConnection) |
static bool | connect (const QObject *sender, const QString &signalMethod, const QObject *receiver, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection, const QString &location=QString ()) |
static bool | connect (const QObject *sender, const QString &signalMethod, const QString &location, const QObject *receiver, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection) |
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class SlotClass , class... SlotArgs, class SlotReturn > | |
static bool | connect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, SlotReturn (SlotClass::*slotMethod)(SlotArgs...), Qt::ConnectionType type=Qt::AutoConnection) |
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class T > | |
static bool | connect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, T slotLambda, Qt::ConnectionType type=Qt::AutoConnection) |
static bool | disconnect (const QObject *sender, const QMetaMethod &signalMethod, const QObject *receiver, const QMetaMethod &slotMethod) |
static bool | disconnect (const QObject *sender, const QString &signalMethod, const QObject *receiver, const QString &slotMethod) |
static bool | disconnect (const QObject *sender, const QString &signalMethod, const QString &location, const QObject *receiver, const QString &slotMethod) |
static bool | disconnect (const QObject *sender, std::nullptr_t, const QObject *receiver, std::nullptr_t) |
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class SlotClass , class... SlotArgs, class SlotReturn > | |
static bool | disconnect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, SlotReturn (SlotClass::*slotMethod)(SlotArgs...)) |
template<class Sender , class SignalClass , class... SignalArgs, class Receiver > | |
static bool | disconnect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, std::nullptr_t slotMethod=nullptr) |
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class T > | |
static bool | disconnect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, T slotMethod) |
static QMetaObject & | staticMetaObject () |
static QString | tr (const char *text, const char *comment=nullptr, std::optional< int > numArg=std::optional< int >()) |
Properties inherited from QObject | |
objectName | |
Related Functions inherited from QObject | |
T | qobject_cast (QObject *object) |
QObjectList | |
The QTcpServer class provides a TCP-based server. 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. The newConnection() signal is then emitted each time a client connects to the server.
Call nextPendingConnection() to accept the pending connection as a connected QTcpSocket. The function returns a pointer to a QTcpSocket in QAbstractSocket::ConnectedState that 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 QTcpServer stop listening for incoming connections.
Although QTcpServer is mostly designed for use with an event loop, it is 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.
|
explicit |
Constructs a QTcpServer object using the given parent.
|
virtual |
Destroys the QTcpServer object.
If the server is listening for connections, the socket is automatically closed. Any client QTcpSockets that are still connected must either disconnect or be reparented before the server is deleted.
|
signal |
This signal is emitted when accepting a new connection results in an error. The socketError parameter describes the type of error that occurred.
|
protected |
This method is called by QTcpServer::incomingConnection() to add the socket to the list of pending incoming connections.
void QTcpServer::close | ( | ) |
Closes the server. The server will no longer listen for incoming connections.
QString QTcpServer::errorString | ( | ) | const |
Returns a human readable description of the last error that occurred.
|
virtual |
Returns true if the server has a pending connection, otherwise returns false.
|
protectedvirtual |
This method is called by QTcpServer when a new connection is available. The socketDescriptor argument is the native socket descriptor for the accepted connection.
The base implementation creates a QTcpSocket, sets the socket descriptor and then stores the QTcpSocket in 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 QNetworkProxy then the socketDescriptor may not be usable with native socket functions, and should only be used with QTcpSocket::setSocketDescriptor().
bool QTcpServer::isListening | ( | ) | const |
Returns true if the server is currently listening for incoming connections, otherwise returns false.
bool QTcpServer::listen | ( | const QHostAddress & | address = QHostAddress::Any , |
quint16 | port = 0 |
||
) |
Tells the server to listen for incoming connections using the given address and 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.
int QTcpServer::maxPendingConnections | ( | ) | const |
Returns the maximum number of pending accepted connections. The default is 30.
|
signal |
This signal is emitted every time a new connection is available.
|
virtual |
Returns the next pending connection as a connected QTcpSocket object. If there are no pending connection nullptr is returned.
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.
void QTcpServer::pauseAccepting | ( | ) |
Pauses accepting new connections. Queued connections will remain in queue.
QNetworkProxy QTcpServer::proxy | ( | ) | const |
Returns the network proxy for this socket. By default QNetworkProxy::DefaultProxy is used.
void QTcpServer::resumeAccepting | ( | ) |
Resumes accepting new connections.
QHostAddress QTcpServer::serverAddress | ( | ) | const |
Returns the server's address if the server is listening for connections, otherwise returns QHostAddress::Null.
QAbstractSocket::SocketError QTcpServer::serverError | ( | ) | const |
Returns an error code for the last error that occurred.
quint16 QTcpServer::serverPort | ( | ) | const |
Returns the server's port if the server is listening for connections, otherwise returns 0.
void QTcpServer::setMaxPendingConnections | ( | int | numConnections | ) |
Sets the maximum number of pending accepted connections to numConnections. QTcpServer will accept no more than numConnections incoming 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., QTcpSocket can still emit the connected() signal). QTcpServer will stop accepting the new connections, but the operating system may still keep them in queue.
void QTcpServer::setProxy | ( | const QNetworkProxy & | networkProxy | ) |
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.
bool QTcpServer::setSocketDescriptor | ( | qintptr | socketDescriptor | ) |
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.
qintptr QTcpServer::socketDescriptor | ( | ) | const |
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.
bool QTcpServer::waitForNewConnection | ( | int | msec = 0 , |
bool * | timedOut = nullptr |
||
) |
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. If msec is -1, this function will not time out.
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.