Hello,everybody!

I'm writing a LAN communication programming,and I need to transport a file between different host computer.
I know that the TCP is a kind of stream-oriented connection.But look at the fallowing codes:

void TripPlanner::sendRequest()
{
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_1);
out << quint16(0) << quint8('S') << fromComboBox->currentText()
<< toComboBox->currentText() << dateEdit->date()
<< timeEdit->time();
if (departureRadioButton->isChecked()) {
out << quint8('D');
} else {
out << quint8('A');
}
out.device()->seek(0);
out << quint16(block.size() - sizeof(quint16));
tcpSocket.write(block);
statusLabel->setText(tr("Sending request..."));
}

I don't think it's a stream but a segment,do I?

And my other question is how to transport a file by TcpSocket.
I am Chinese,so the file may not be Enlish single,and the file may be alse a binary data file. How do I transport it?

I know that my English is poor,but I will try my best to exchange it!
Help me,please!
Thank anybody who look at this thread!

By the way, I had another question: what's the meaning of "hen & egg problem"(I'm not a English mother language men).

Dani AI

Generated

Quick summary and practical pattern (answers both questions). TCP is a byte stream, not a message transport; the length-prefix you build in 's snippet is the usual solution: write a small header (filename, filesize, optional checksum) then stream the raw file bytes. The peer must read and buffer data until it has the complete header, parse it, then keep reading until the declared file size is received.

Suggested sender pattern (header + raw chunks):

QFile file(filePath);
if (!file.open(QIODevice::ReadOnly)) return;
quint64 fileSize = static_cast<quint64>(file.size());
QByteArray fileNameUtf8 = QFileInfo(file).fileName().toUtf8();

QByteArray header;
QDataStream hdr(&header, QIODevice::WriteOnly);
hdr.setByteOrder(QDataStream::BigEndian);        // explicit byte order
hdr << fileNameUtf8 << fileSize;

// send 4-byte header length, then header, then file bytes in chunks
QByteArray prefix;
QDataStream p(&prefix, QIODevice::WriteOnly);
p.setByteOrder(QDataStream::BigEndian);
p << quint32(header.size());
socket->write(prefix);
socket->write(header);

const qint64 CHUNK = 64*1024;
while (!file.atEnd()) {
    QByteArray chunk = file.read(CHUNK);
    socket->write(chunk);
    socket->waitForBytesWritten(); // avoid in GUI thread for big transfers
}

Receiver outline (stateful parsing in readyRead):

// 1) read 4-byte header length
// 2) read headerSize bytes -> QDataStream to extract fileNameUtf8 and fileSize
// 3) open QFile for write and loop reading socket->read(min(bytesAvailable, remaining))
//    until received == fileSize
// 4) close file and optionally compare received hash with header checksum

Extra notes and pitfalls: follow 's hint — be explicit about byte order and QDataStream version on both sides. For non-English filenames send UTF-8 (toUtf8 / fromUtf8). Treat file contents as raw bytes (no QString conversion). Use a 64-bit length for large files, compute a hash (QCryptographicHash) for integrity, and avoid blocking the GUI (use asynchronous writes or a worker thread). Test with small and large files, and implement a simple retry/timeout and a way to resume or restart interrupted transfers.

Recommended Answers

All 2 Replies

chicken & egg proglem

If the file contains binary representation of integers then the bytes may have to be reversed on the receiving os. This is common problem when transferring files between MS-Windowos and *nix/Unix --

As for your specific question -- I don't know, sorry.

chicken & egg proglem
Thanks! Maybe I could find it in google by myself first, I'm sorry for my lazy.

If the file contains binary representation of integers then the bytes may have to be reversed on the receiving os. This is common problem when transferring files between MS-Windowos and *nix/Unix -- big endian little endian
This is important for me. I guess that Qt may encapsulate it in an easy way.
As for your specific question -- I don't know, sorry.

You have help me so much!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.