QSctpSocket Class
QSctpSocket 提供了一个 SCTP 类型的套接字. More...
头文件: | #include <QSctpSocket> |
qmake: | QT += network |
开始支持版本: | Qt 5.8 |
基类: | QTcpSocket |
公有函数
QSctpSocket(QObject *parent = nullptr) | |
virtual | ~QSctpSocket() |
bool | isInDatagramMode() const |
int | maximumChannelCount() const |
QNetworkDatagram | readDatagram() |
void | setMaximumChannelCount(int count) |
bool | writeDatagram(const QNetworkDatagram &datagram) |
重新实现的公有函数
virtual void | close() |
virtual void | disconnectFromHost() |
- 37 个公有函数继承自 QAbstractSocket
- 43 个公有函数继承自 QIODevice
- 32 个公有函数继承自 QObject
重新实现的受保护函数
virtual qint64 | readData(char *data, qint64 maxSize) |
virtual qint64 | readLineData(char *data, qint64 maxlen) |
- 10 个受保护的函数继承自 QAbstractSocket
- 5 个受保护的函数继承自 QIODevice
- 9 个受保护的函数继承自 QObject
其他继承的成员
- 1 个属性继承自 QObject
- 1 个公有槽函数继承自 QObject
- 6 个信号继承自 QAbstractSocket
- 6 个信号继承自 QIODevice
- 2 个信号继承自 QObject
- 1 个公有变量继承自 QObject
- 10 个静态公有成员继承自 QObject
- 10 个受保护的函数继承自 QAbstractSocket
- 5 个受保护的函数继承自 QIODevice
- 9 个受保护的函数继承自 QObject
- 2 个受保护的变量继承自 QObject
详细描述
SCTP (Stream Control Transmission Protocol) 是一种传输层协议, 其作用与流行的协议 TCP 和 UDP 类似. 像 UDP, SCTP 也是面向消息的, 但是它可以通过类似 TCP 的拥塞控制来确保消息的可靠性, 使消息按顺序传输.
SCTP 是面向连接的协议, 它提供端点之间多个数据流的完全同时传输. 这种多数据流允许通过独立的通道传输数据, 因此, 如果一个数据流中有数据丢失, 则其他数据流的传输将不会受到影响.
作为一个面向消息的传输协议, SCTP 以序列方式传输消息, 而不是像 TCP 一样传输连续的字节流. 像在 UDP 中一样, 在 SCTP 中, 发送方在一个操作中发送一条消息, 而确切的消息则在一个操作中传递给接收应用程序进程. 但是与 UDP 不同, 消息的传递是有可靠性保证的.
SCTP 还支持多宿主, 这意味着连接的端点可以具有一些与其关联的备用 IP 地址, 以便绕过网络故障或变化的条件进行数据传输.
QSctpSocket 是 QTcpSocket 的便利子类, 它使你可以通过 SCTP 模拟 TCP 数据流或建立 SCTP 连接以实现可靠的数据报服务.
QSctpSocket 可以在以下两种模式之一中运行:
- 连续字节流(TCP 模拟).
- 多流数据报模式.
要设置连续字节流模式,你可以首先实例化一个 QSctpSocket 对象, 并使用一个负值作为参数调用 setMaximumChannelCount(). 这样就可以将 QSctpSocket 用作常规的缓冲 QTcpSocket 类型套接字使用. 你可以调用 connectToHost() 发起与端点的连接, 可以调用 write() 和 read() 与对等方进行消息发送与接收, 但是在这种模式下你无法区分消息的边界.
默认情况下, QSctpSocket 工作在数据报模式下. 建立连接前, 先调用 setMaximumChannelCount() 设置应用程序准备支持的最大通道数. 此数字是与远程端点协商的参数, 其值受操作系统的限制. 默认值0表示使用对等端的值. 如果两个端点都使用默认值, 则连接通道的数量取决于系统. 建立连接后, 可以通过调用 readChannelCount() 和 writeChannelCount() 获取实际的通道数.
QSctpSocket *socket = new QSctpSocket(this); socket->setMaxChannelCount(16); socket->connectToHost(QHostAddress::LocalHost, 1973); if (socket->waitForConnected(1000)) { int inputChannels = socket->readChannelCount(); int outputChannels = socket->writeChannelCount(); .... }
在数据报模式, QSctpSocket 为每一个通道单独执行数据报的缓冲操作. 你可以通过调用 writeDatagram() 函数将当前通道的一个数据报加入缓冲队列, 调用 readDatagram() 函数从当前通道读取一个待处理的数据报.
在数据报模式下, 允许使用标准的 QIODevice 成员函数 read(), readLine(), write() 等. 其限制与连续字节流模式相同.
注意: 多通道数据报模式并不被 Windows 操作系统支持.
另见 QSctpServer, QTcpSocket, QAbstractSocket.
Member Function Documentation
QSctpSocket::QSctpSocket(QObject *parent = nullptr)
Creates a QSctpSocket object in state UnconnectedState
.
Sets the datagram operation mode. The parent argument is passed to QObject's constructor.
See also socketType() and setMaximumChannelCount().
[virtual]
QSctpSocket::~QSctpSocket()
Destroys the socket, closing the connection if necessary.
See also close().
[override virtual]
void QSctpSocket::close()
Reimplements: QAbstractSocket::close().
[override virtual]
void QSctpSocket::disconnectFromHost()
Reimplements: QAbstractSocket::disconnectFromHost().
bool QSctpSocket::isInDatagramMode() const
Returns true
if the socket is running in datagram mode.
See also setMaximumChannelCount().
int QSctpSocket::maximumChannelCount() const
Returns the maximum number of channels that QSctpSocket is able to support.
A value of 0 (the default) means that the number of connection channels would be set by the remote endpoint.
Returns -1 if QSctpSocket is running in continuous byte stream mode.
See also setMaximumChannelCount(), readChannelCount(), and writeChannelCount().
[override virtual protected]
qint64 QSctpSocket::readData(char *data, qint64 maxSize)
Reimplements: QAbstractSocket::readData(char *data, qint64 maxSize).
QNetworkDatagram QSctpSocket::readDatagram()
Reads a datagram from the buffer of the current read channel, and returns it as a QNetworkDatagram object, along with the sender's host address and port. If possible, this function will also try to determine the datagram's destination address, port, and the number of hop counts at reception time.
On failure, returns a QNetworkDatagram that reports not valid.
See also writeDatagram(), isInDatagramMode(), and currentReadChannel().
[override virtual protected]
qint64 QSctpSocket::readLineData(char *data, qint64 maxlen)
Reimplements: QAbstractSocket::readLineData(char *data, qint64 maxlen).
void QSctpSocket::setMaximumChannelCount(int count)
Sets the maximum number of channels that the application is prepared to support in datagram mode, to count. If count is 0, endpoint's value for maximum number of channels is used. Negative count sets a continuous byte stream mode.
Call this method only when QSctpSocket is in UnconnectedState.
See also maximumChannelCount(), readChannelCount(), and writeChannelCount().
bool QSctpSocket::writeDatagram(const QNetworkDatagram &datagram)
Writes a datagram to the buffer of the current write channel. Returns true on success; otherwise returns false.
See also readDatagram(), isInDatagramMode(), and currentWriteChannel().