QTcpServer Class

QTcpServer 提供了一个基于 TCP 协议的服务器. More...

头文件: #include <QTcpServer>
qmake: QT += network
基类: QObject
派生类:

QSctpServer

Note: All functions in this class are reentrant.

公有函数

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 = Q_NULLPTR)
  • 32 个公有函数继承自 QObject

信号

void acceptError(QAbstractSocket::SocketError socketError)
void newConnection()

受保护的函数

void addPendingConnection(QTcpSocket *socket)
virtual void incomingConnection(qintptr socketDescriptor)
  • 9 个受保护的函数继承自 QObject

其他继承的成员

  • 1 个属性继承自 QObject
  • 1 个公有槽函数继承自 QObject
  • 1 个公有变量继承自 QObject
  • 10 个静态公有成员继承自 QObject
  • 2 个受保护的变量继承自 QObject

详细描述

QTcpServer 可以接受传入的 TCP 连接. 你可以指定端口或让 QTcpServer 自动选择一个端口. 你可以监听特定地址或所有机器的地址.

调用 listen() 让服务器监听传入的连接. 每次客户端连接到服务器时都会发出 newConnection() 信号.

调用 nextPendingConnection() 以接受挂起的连接作为已连接的 QTcpSocket. 该函数返回一个指向 QTcpSocket 的的指针, QTcpSocket 处于 QAbstractSocket::ConnectedState 状态, 你可以使用该指针与客户端进行通信.

如果发生错误, serverError() 返回错误的类型, 并且可以调用 errorString() 获取所发生事件的描述.

侦听连接时, 服务器侦听的地址和端口可用作 serverAddress() 和 serverPort().

调用 close() 使 QTcpServer 停止侦听传入连接.

尽管, QTcpServer 主要设计用于与事件循环一起使用, 但也可以在没有事件循环的情况下使用它. 在这种情况下, 你必须使用 waitForNewConnection(), 它会阻塞, 直到连接可用或超时到期.

另见 QTcpSocket, Fortune Server Example, Threaded Fortune Server Example, Loopback Example, Torrent Example.

Member Function Documentation

QTcpServer::QTcpServer(QObject *parent = nullptr)

Constructs a QTcpServer object.

parent is passed to the QObject constructor.

See also listen() and setSocketDescriptor().

[signal] void QTcpServer::acceptError(QAbstractSocket::SocketError socketError)

This signal is emitted when accepting a new connection results in an error. The socketError parameter describes the type of error that occurred.

This function was introduced in Qt 5.0.

See also pauseAccepting() and resumeAccepting().

[signal] void QTcpServer::newConnection()

This signal is emitted every time a new connection is available.

See also hasPendingConnections() and nextPendingConnection().

[virtual] QTcpServer::~QTcpServer()

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.

See also close().

[protected] void QTcpServer::addPendingConnection(QTcpSocket *socket)

This function is called by QTcpServer::incomingConnection() to add the socket 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.

This function was introduced in Qt 4.7.

See also incomingConnection().

void QTcpServer::close()

Closes the server. The server will no longer listen for incoming connections.

See also listen().

QString QTcpServer::errorString() const

Returns a human readable description of the last error that occurred.

See also serverError().

[virtual] bool QTcpServer::hasPendingConnections() const

Returns true if the server has a pending connection; otherwise returns false.

See also nextPendingConnection() and setMaxPendingConnections().

[virtual protected] void QTcpServer::incomingConnection(qintptr socketDescriptor)

This virtual function 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().

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 the socketDescriptor to the other thread and create the QTcpSocket object there and use its setSocketDescriptor() method.

See also newConnection(), nextPendingConnection(), and addPendingConnection().

bool QTcpServer::isListening() const

Returns true if the server is currently listening for incoming connections; otherwise returns false.

See also listen().

bool QTcpServer::listen(const QHostAddress &address = QHostAddress::Any, quint16 port = 0)

Tells the server to listen for incoming connections on address address and port 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.

See also isListening().

int QTcpServer::maxPendingConnections() const

Returns the maximum number of pending accepted connections. The default is 30.

See also setMaxPendingConnections() and hasPendingConnections().

[virtual] QTcpSocket *QTcpServer::nextPendingConnection()

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.

nullptr 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 override incomingConnection().

See also hasPendingConnections().

void QTcpServer::pauseAccepting()

Pauses accepting new connections. Queued connections will remain in queue.

This function was introduced in Qt 5.0.

See also resumeAccepting().

QNetworkProxy QTcpServer::proxy() const

Returns the network proxy for this socket. By default QNetworkProxy::DefaultProxy is used.

This function was introduced in Qt 4.1.

See also setProxy() and QNetworkProxy.

void QTcpServer::resumeAccepting()

Resumes accepting new connections.

This function was introduced in Qt 5.0.

See also pauseAccepting().

QHostAddress QTcpServer::serverAddress() const

Returns the server's address if the server is listening for connections; otherwise returns QHostAddress::Null.

See also serverPort() and listen().

QAbstractSocket::SocketError QTcpServer::serverError() const

Returns an error code for the last error that occurred.

See also errorString().

quint16 QTcpServer::serverPort() const

Returns the server's port if the server is listening for connections; otherwise returns 0.

See also serverAddress() and listen().

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.

See also maxPendingConnections() and hasPendingConnections().

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:

 server->setProxy(QNetworkProxy::NoProxy);

This function was introduced in Qt 4.1.

See also proxy() and QNetworkProxy.

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.

See also socketDescriptor() and isListening().

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.

See also setSocketDescriptor() and isListening().

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 nullptr, *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.

See also hasPendingConnections() and nextPendingConnection().