QTcpServer Class Reference

[ QtNetwork module]

QTcpServer 类提供基于 TCP 的服务器。 更多...

继承 QObject .

方法

Qt Signals


详细描述

QTcpServer 类提供基于 TCP 的服务器。

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.

调用 listen () to have the server listen for incoming connections. The newConnection () signal is then emitted each time a client connects to the server.

调用 nextPendingConnection () to accept the pending connection as a connected QTcpSocket . The function returns a pointer to a QTcpSocket in QAbstractSocket.ConnectedState ,可以用于与客户端进行通信。

若发生错误, 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 () 和 serverPort ().

调用 close () makes QTcpServer stop listening for incoming connections.

Although QTcpServer is 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.

Symbian Platform Security Requirements

On Symbian, processes which use this class must have the NetworkServices platform security capability. If the client process lacks this capability, it will lead to a panic.

Platform security capabilities are added via the TARGET.CAPABILITY qmake variable.


方法文档编制

QTcpServer.__init__ ( self , QObject   parent  = None)

parent argument, if not None, causes self to be owned by Qt instead of PyQt.

构造 QTcpServer 对象。

parent 会被传递给 QObject 构造函数。

另请参阅 listen () and setSocketDescriptor ().

QTcpServer.addPendingConnection ( self , QTcpSocket   socket )

此函数被调用由 QTcpServer.incomingConnection () to add the socket to the list of pending incoming connections.

注意: Don't forget to call this member from reimplemented incomingConnection () if you do not want to break the Pending Connections mechanism.

该函数在 Qt 4.7 引入。

另请参阅 incomingConnection ().

QTcpServer.close ( self )

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

另请参阅 listen ().

QString QTcpServer.errorString ( self )

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

另请参阅 serverError ().

bool QTcpServer.hasPendingConnections ( self )

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

另请参阅 nextPendingConnection () and setMaxPendingConnections ().

QTcpServer.incomingConnection ( self , int  handle )

此虚拟函数被调用由 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 () 被发射。

Reimplement this function to alter the server's behavior when a connection is available.

若此服务器正在使用 QNetworkProxy then the socketDescriptor may not be usable with native socket functions, and should only be used with QTcpSocket.setSocketDescriptor ().

注意: 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 () 方法。

另请参阅 newConnection (), nextPendingConnection (), and addPendingConnection ().

bool QTcpServer.isListening ( self )

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

另请参阅 listen ().

bool QTcpServer.listen ( self , QHostAddress   address  = QHostAddress.Any, int  port  = 0)

告诉服务器去监听传入连接在地址 address 和端口 port 。若 port is 0, a port is chosen automatically. If address is QHostAddress.Any , server will listen on all network interfaces.

返回 true 当成功时;否则返回 false。

另请参阅 isListening ().

int QTcpServer.maxPendingConnections ( self )

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

另请参阅 setMaxPendingConnections () and hasPendingConnections ().

QTcpSocket QTcpServer.nextPendingConnection ( self )

返回下一待决连接作为已连接 QTcpSocket 对象。

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.

注意: 返回的 QTcpSocket object cannot be used from another thread. If you want to use an incoming connection from another thread, you need to override incomingConnection ().

另请参阅 hasPendingConnections ().

QNetworkProxy QTcpServer.proxy ( self )

返回此套接字的网络代理。默认情况下 QNetworkProxy.DefaultProxy 被使用。

该函数在 Qt 4.1 引入。

另请参阅 setProxy () 和 QNetworkProxy .

QHostAddress QTcpServer.serverAddress ( self )

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

另请参阅 serverPort () 和 listen ().

QAbstractSocket.SocketError QTcpServer.serverError ( self )

返回最后发生错误的错误代码。

另请参阅 errorString ().

int QTcpServer.serverPort ( self )

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

另请参阅 serverAddress () 和 listen ().

QTcpServer.setMaxPendingConnections ( self , int  numConnections )

把最大待决接受连接数设为 numConnections . QTcpServer 将接受不超过 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.

另请参阅 maxPendingConnections () and hasPendingConnections ().

QTcpServer.setProxy ( self , QNetworkProxy   networkProxy )

将此套接字的显式网络代理设为 networkProxy .

要禁用此套接字所用代理,使用 QNetworkProxy.NoProxy 代理类型:

 server->setProxy(QNetworkProxy.NoProxy);
			

该函数在 Qt 4.1 引入。

另请参阅 proxy () 和 QNetworkProxy .

bool QTcpServer.setSocketDescriptor ( self , int  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.

假定套接字处于监听状态。

另请参阅 socketDescriptor () 和 isListening ().

int QTcpServer.socketDescriptor ( self )

Returns the native socket descriptor the server uses to listen for incoming instructions, or -1 if the server is not listening.

若服务器正在使用 QNetworkProxy , the returned descriptor may not be usable with native socket functions.

另请参阅 setSocketDescriptor () 和 isListening ().

(bool, bool  timedOut ) QTcpServer.waitForNewConnection ( self , int  msecs  = 0)

等待最多 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.

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 () 信号。

若 msec 为 -1,此函数不会超时。

另请参阅 hasPendingConnections () and nextPendingConnection ().


Qt Signal Documentation

void newConnection ()

This is the default overload of this signal.

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

另请参阅 hasPendingConnections () and nextPendingConnection ().