📄 qtcpserver.cpp
字号:
Closes the server. The server will no longer listen for incoming connections. \sa listen()*/void QTcpServer::close(){ Q_D(QTcpServer); qDeleteAll(d->pendingConnections); d->pendingConnections.clear(); if (d->socketEngine) { d->socketEngine->close(); d->socketEngine->deleteLater(); d->socketEngine = 0; } d->state = QAbstractSocket::UnconnectedState;}/*! 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. \sa setSocketDescriptor(), isListening()*/int QTcpServer::socketDescriptor() const{ Q_D(const QTcpServer); Q_CHECK_SOCKETENGINE(-1); return d->socketEngine->socketDescriptor();}/*! Sets the socket descriptor this server should use when listening for incoming connections to \a socketDescriptor. Returns true if the socket is set successfully; otherwise returns false. The socket is assumed to be in listening state. \sa socketDescriptor(), isListening()*/bool QTcpServer::setSocketDescriptor(int socketDescriptor){ Q_D(QTcpServer); if (isListening()) { qWarning("QTcpServer::setSocketDescriptor() called when already listening"); return false; } if (d->socketEngine) delete d->socketEngine; d->socketEngine = QAbstractSocketEngine::createSocketEngine(socketDescriptor, this); if (!d->socketEngine->initialize(socketDescriptor, QAbstractSocket::ListeningState)) { d->serverSocketError = d->socketEngine->error(); d->serverSocketErrorString = d->socketEngine->errorString();#if defined (QTCPSERVER_DEBUG) qDebug("QTcpServer::setSocketDescriptor(%i) failed (%s)", socketDescriptor, d->serverSocketErrorString.toLatin1().constData());#endif return false; } d->socketEngine->setReceiver(d); d->socketEngine->setReadNotificationEnabled(true); d->state = d->socketEngine->state(); d->address = d->socketEngine->localAddress(); d->port = d->socketEngine->localPort();#if defined (QTCPSERVER_DEBUG) qDebug("QTcpServer::setSocketDescriptor(%i) succeeded.", socketDescriptor);#endif return true;}/*! Returns the server's port if the server is listening for connections; otherwise returns 0. \sa serverAddress(), listen()*/quint16 QTcpServer::serverPort() const{ Q_D(const QTcpServer); Q_CHECK_SOCKETENGINE(0); return d->socketEngine->localPort();}/*! Returns the server's address if the server is listening for connections; otherwise returns QHostAddress::Null. \sa serverPort(), listen()*/QHostAddress QTcpServer::serverAddress() const{ Q_D(const QTcpServer); Q_CHECK_SOCKETENGINE(QHostAddress(QHostAddress::Null)); return d->socketEngine->localAddress();}/*! Waits for at most \a 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 \a timedOut is not 0, *\a 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. \sa hasPendingConnections(), nextPendingConnection()*/bool QTcpServer::waitForNewConnection(int msec, bool *timedOut){ Q_D(QTcpServer); if (d->state != QAbstractSocket::ListeningState) return false; if (!d->socketEngine->waitForRead(msec, timedOut)) { d->serverSocketError = d->socketEngine->error(); d->serverSocketErrorString = d->socketEngine->errorString(); return false; } if (timedOut && *timedOut) return false; d->readNotification(); return true;}/*! Returns true if the server has a pending connection; otherwise returns false. \sa nextPendingConnection(), setMaxPendingConnections()*/bool QTcpServer::hasPendingConnections() const{ return !d_func()->pendingConnections.isEmpty();}/*! 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. 0 is returned if this function is called when there are no pending connections. \sa hasPendingConnections()*/QTcpSocket *QTcpServer::nextPendingConnection(){ Q_D(QTcpServer); if (d->pendingConnections.isEmpty()) return 0; if (!d->socketEngine->isReadNotificationEnabled()) d->socketEngine->setReadNotificationEnabled(true); return d->pendingConnections.takeFirst();}/*! This virtual function is called by QTcpServer when a new connection is available. The \a 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 \a socketDescriptor may not be usable with native socket functions, and should only be used with QTcpSocket::setSocketDescriptor(). \sa newConnection(), nextPendingConnection()*/void QTcpServer::incomingConnection(int socketDescriptor){#if defined (QTCPSERVER_DEBUG) qDebug("QTcpServer::incomingConnection(%i)", socketDescriptor);#endif QTcpSocket *socket = new QTcpSocket(this); socket->setSocketDescriptor(socketDescriptor); d_func()->pendingConnections.append(socket);}/*! Sets the maximum number of pending accepted connections to \a numConnections. QTcpServer will accept no more than \a 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. \sa maxPendingConnections(), hasPendingConnections()*/void QTcpServer::setMaxPendingConnections(int numConnections){ d_func()->maxConnections = numConnections;}/*! Returns the maximum number of pending accepted connections. The default is 30. \sa setMaxPendingConnections(), hasPendingConnections()*/int QTcpServer::maxPendingConnections() const{ return d_func()->maxConnections;}/*! Returns an error code for the last error that occurred. \sa errorString()*/QAbstractSocket::SocketError QTcpServer::serverError() const{ return d_func()->serverSocketError;}/*! Returns a human readable description of the last error that occurred. \sa serverError()*/QString QTcpServer::errorString() const{ return d_func()->serverSocketErrorString;}#ifndef QT_NO_NETWORKPROXY/*! \since 4.1 Sets the explicit network proxy for this socket to \a networkProxy. To disable the use of a proxy for this socket, use the QNetworkProxy::NoProxy proxy type: \code server->setProxy(QNetworkProxy::NoProxy); \endcode \sa proxy(), QNetworkProxy*/void QTcpServer::setProxy(const QNetworkProxy &networkProxy){ Q_D(QTcpServer); if (!d->proxy) d->proxy = new QNetworkProxy(); *d->proxy = networkProxy;}/*! \since 4.1 Returns the network proxy for this socket. By default QNetworkProxy::DefaultProxy is used. \sa setProxy(), QNetworkProxy*/QNetworkProxy QTcpServer::proxy() const{ Q_D(const QTcpServer); if (d->proxy) return *d->proxy; return QNetworkProxy();}#endif // QT_NO_NETWORKPROXY#include "moc_qtcpserver.cpp"
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -