QSctpSocket 类

The QSctpSocket class provides an SCTP socket. 更多...

头: #include <QSctpSocket>
qmake: QT += network
Since: Qt 5.8
继承: QTcpSocket

该类在 Qt 5.8 引入。

公共函数

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 () override
virtual void disconnectFromHost () override

重实现保护函数

virtual qint64 readData (char * data , qint64 maxSize ) override
virtual qint64 readLineData (char * data , qint64 maxlen ) override

详细描述

SCTP (流控制传输协议) 是充当类似流行 TCP 和 UDP 协议角色的传输层协议。像 UDP,SCTP 面向消息,但它采用像 TCP 的拥塞控制确保消息的可靠、按顺序传输。

SCTP is connection-oriented protocol, which provides the complete simultaneous transmission of multiple data streams between endpoints. This multi-streaming allows data to be delivered by independent channels, so that if there is data loss in one stream, delivery will not be affected for the other streams.

Being message-oriented, SCTP transports a sequence of messages, rather than transporting an unbroken stream of bytes as does TCP. Like in UDP, in SCTP a sender sends a message in one operation, and that exact message is passed to the receiving application process in one operation. But unlike UDP, the delivery is guaranteed.

It also supports multi-homing, meaning that a connected endpoint can have alternate IP addresses associated with it in order to route around network failure or changing conditions.

QSctpSocket is a convenience subclass of QTcpSocket that allows you to emulate TCP data stream over SCTP or establish an SCTP connection for reliable datagram service.

QSctpSocket can operate in one of two possible modes:

  • 连续字节流 (TCP 仿真)。
  • 多流数据报模式。

To set a continuous byte stream mode, instantiate QSctpSocket and call setMaximumChannelCount () with a negative value. This gives the ability to use QSctpSocket as a regular buffered QTcpSocket 。可以调用 connectToHost () to initiate connection with endpoint, write () to transmit and read () to receive data from the peer, but you cannot distinguish message boundaries.

By default, QSctpSocket operates in datagram mode. Before connecting, call setMaximumChannelCount () to set the maximum number of channels that the application is prepared to support. This number is a parameter negotiated with the remote endpoint and its value can be bounded by the operating system. The default value of 0 indicates to use the peer's value. If both endpoints have default values, then number of connection channels is system-dependent. After establishing a connection, you can fetch the actual number of channels by calling 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();
    ....
}
					

In datagram mode, QSctpSocket performs the buffering of datagrams independently for each channel. You can queue a datagram to the buffer of the current channel by calling writeDatagram () and read a pending datagram by calling readDatagram () respectively.

Using the standard QIODevice 函数 read (), readLine (), write (), etc. is allowed in datagram mode with the same limitations as in continuous byte stream mode.

注意: Windows 平台不支持此特征。

另请参阅 QSctpServer , QTcpSocket ,和 QAbstractSocket .

成员函数文档编制

QSctpSocket:: QSctpSocket ( QObject * parent = nullptr)

Creates a QSctpSocket object in state UnconnectedState .

设置数据报操作模式。 parent 自变量会被传递给 QObject 的构造函数。

另请参阅 socketType () 和 setMaximumChannelCount ().

[virtual] QSctpSocket:: ~QSctpSocket ()

销毁套接字,关闭连接 (若有必要)。

另请参阅 close ().

[override virtual] void QSctpSocket:: close ()

重实现: QAbstractSocket::close ().

[override virtual] void QSctpSocket:: disconnectFromHost ()

重实现: QAbstractSocket::disconnectFromHost ().

bool QSctpSocket:: isInDatagramMode () const

返回 true 若套接字正运行在数据报模式下。

另请参阅 setMaximumChannelCount ().

int QSctpSocket:: maximumChannelCount () const

Returns the maximum number of channels that QSctpSocket is able to support.

0 值 (默认) 意味着连接通道数将由远程端点进行设置。

返回 -1,若 QSctpSocket is running in continuous byte stream mode.

另请参阅 setMaximumChannelCount (), readChannelCount (),和 writeChannelCount ().

[override virtual protected] qint64 QSctpSocket:: readData ( char * data , qint64 maxSize )

重实现: 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 对象,除发送器的主机地址和端口外。若可能,此函数还会试着确定数据报的目的地地址、端口及接待时的跳跃计数。

当故障时,返回 QNetworkDatagram 报告 无效 .

另请参阅 writeDatagram (), isInDatagramMode (),和 currentReadChannel ().

[override virtual protected] qint64 QSctpSocket:: readLineData ( char * data , qint64 maxlen )

重实现: 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 。若 count is 0, endpoint's value for maximum number of channels is used. Negative count sets a continuous byte stream mode.

才调用此方法当 QSctpSocket 是在 UnconnectedState .

另请参阅 maximumChannelCount (), readChannelCount (),和 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.

另请参阅 readDatagram (), isInDatagramMode (),和 currentWriteChannel ().