QTcpServer类

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’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.

(一)public functions
1.QTcpServer(QObject *parent = Q_NULLPTR)

2.virtual ~QTcpServer()

3.void close()

4.QString errorString() const

5.virtual bool hasPendingConnections() const

6.bool isListening() const

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

8.int **maxPending**Connections() const

9.virtual QTcpSocket * **nextPending**Connection()

10.void pauseAccepting()

11.QNetworkProxy proxy() const

12.void resumeAccepting()

13.QHostAddress serverAddress() const

14.QAbstractSocket::SocketError serverError() const

15.quint16 serverPort() const

16.void setMaxPendingConnections(int numConnections)

17.void setProxy(const QNetworkProxy &networkProxy)

18.bool setSocketDescriptor(qintptr socketDescriptor)

19.qintptr socketDescriptor() const

20.bool waitForNewConnection(int msec = 0, bool *timedOut = Q_NULLPTR)

(二)signals
void acceptError(QAbstractSocket::SocketError socketError)

void newConnection();每次有新的连接时发送此信号

(三)Protected Functions
void addPendingConnection(QTcpSocket *socket)

virtual void incomingConnection(qintptr socketDescriptor);虚函数,当新的连接可用时,被QTcpServer对象(派生类对象)调用(自动调用),socketDescriptor是接受连接的套接字描述符;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值