I want to use Qt UDP (not TCP) socket to transfer file. So I write code like this:
Sender
QFile file1(QString::fromStdString(filedir));
QByteArray bytes;
file1.open(QIODevice::ReadOnly);
QTextStream in(&file1);
while (!in.atEnd()) {
bytes = in.read(8192).toAscii();
udpSocket.writeDatagram(bytes, QHostAddress(ip), port.toInt());
}
Receiver
udpSocket1.bind(ui->sendPort->text().toInt());
connect(&udpSocket1,SIGNAL(readyRead()),this,SLOT(listenfile()));
void Widget::listenfile() {
QFile file("received.txt");
file.resize(0);
file.open(QIODevice::Append);
QTextStream out(&file);
do {
QByteArray data;
data.resize(udpSocket1.pendingDatagramSize());
udpSocket1.readDatagram(data.data(),data.size());
QString str=data.data();
ui->textBrowser1->append(str);
out << data;
} while (udpSocket1.hasPendingDatagrams());
}
When I send small file, there is no problem. However, if i want to send large file (> 8192 bytes), the receiver can only get the top 8KB data even tested on localhost. If I decrease the size number in sender, such as 1024. The receiver still only gets the top 8KB data. If I increase the size number in sender to X bytes (X > 8192). The receiver will get the top X Bytes data.
It seems like the minimum size of Qt UDP packet to transfer is 8192 bytes. The receiver always gets the first packet, but can not receive others.
I have little experience in Qt and network programming, so I don't know whether my conjecture is right or not. Can you tell me how to change these codes to support receiving packets after the first packet so I can transfer large data?