Thirty-four of Qt foundation: QTcpSocket and QTcpServer source code analysis

From Qt network programming (TCP&UDP) , we know that QTcpSocket has its own read and write buffers. The following will analyze how these two buffers are implemented on Windows.

1. QTcpSocket

Before the analysis, let me introduce two access methods of the device: continuous access and random access. The continuous access device has no concept of start, end, size and current position, and does not support seeking. It can only be read when the device notifies that there is data to read. The most common serial access device is the socket. On Unix, some special files, such as /dev/zero and fifo pipes, are serial access devices.
Correspondingly, ordinary files support random access, they all have the concept of size and current position, and support data flow forward or backward seeking.
Therefore, for QTcpSocket, isSequential() returns true.

1. Write data

Let’s take a look at writing data first. A typical code for writing data is as follows:

QString data = "CSDN: CaoShangPa";
QByteArray buffer = data.toUtf8();
tcpSocket.write(buffer);

write comes from the base class QIODevice of QTcpSocket

qint64 QIODevice::write(const char *data)
{
    return write(data, qstrlen(data));
}
qint64 QIODevice::write(const char *data, qint64 maxSize)

Guess you like

Origin blog.csdn.net/caoshangpa/article/details/129942104