Results 1 to 3 of 3

Thread: QTcpSocket Newbie Question (not all data being received)

  1. #1
    Join Date
    Apr 2010
    Posts
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default QTcpSocket Newbie Question (not all data being received)

    For the moment, I'm doing all this code in the same app and communicating over localhost (127.0.0.1). The first toWrite line saying hello world and the Port Number works, but my longer file gets truncated at 8192 characters. Its including the length of the thing to write, so I'm not sure what I'm doing wrong. I'm not new to Qt but I am new to this particular process.

    The idea here is that a client would send a file (which contains print job data) over the network through the specified port to the server's client where it will receive the data and do stuff with it, like write to a file, assign it to a printer on the server, etc.

    Client Sends the data:
    Qt Code:
    1. void Client::startTransfer() {
    2. QByteArray toWrite;// = QByteArray("Hello, world (Port: " + QByteArray::number(PORTNUMBER) + ")");
    3. //
    4. QFile file("./PrintJobs/exampleprintjob");
    5. if(!file.open(QIODevice::ReadOnly)) {
    6. QMessageBox::information(0, "error", file.errorString());
    7. }
    8. QTextStream in(&file);
    9. QStringList lines;
    10. while(!in.atEnd()) { lines.append(in.readLine()); }
    11. file.close();
    12. QString file2 = "";
    13. foreach(QString line, lines) { file2 += line + "\r\n"; }
    14. toWrite = file2.toLatin1();
    15. //
    16. client.write(toWrite,toWrite.length()); // length = 188,673 characters, 184 KB file size
    17. //
    18. client.flush();
    19. }
    To copy to clipboard, switch view to plain text mode 
    Server reads the data:
    Qt Code:
    1. Server::Server(QObject * parent): QObject(parent) {
    2. connect(&server, SIGNAL(newConnection()), this, SLOT(acceptConnection()));
    3. server.listen(QHostAddress::Any, PORTNUMBER);
    4. }
    5. void Server::acceptConnection() {
    6. client = server.nextPendingConnection();
    7. connect(client, SIGNAL(readyRead()), this, SLOT(startRead()));
    8. }
    9. void Server::startRead() {
    10. QByteArray buffer;
    11. buffer = client->read(client->bytesAvailable());
    12. emit readFromListening(buffer);
    13. client->close();
    14. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QTcpSocket Newbie Question (not all data being received)

    You are assuming that data written in one write() is transmitted as one lump, and received in one read(). This is almost never the case and certainly not for 182000 bytes. You need to buffer received data until you have received enough to be a full data unit (whatever that means for your protocol) before processing it.

  3. #3
    Join Date
    Apr 2010
    Posts
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTcpSocket Newbie Question (not all data being received)

    Just in case anyone has any pointers, this is what I rewrote to:

    Qt Code:
    1. void Client::startTransfer()
    2. {
    3. QByteArray toWrite;// = QByteArray("Hello, world (Port: " + QByteArray::number(PORTNUMBER) + ")");
    4. //
    5. QFile file("./PrintJobs/exampleprintjob");
    6. if(!file.open(QIODevice::ReadOnly)) {
    7. QMessageBox::information(0, "error", file.errorString());
    8. }
    9. QTextStream in(&file);
    10. QStringList lines;
    11. while(!in.atEnd()) { lines.append(in.readLine()); }
    12. file.close();
    13. QString file2 = "";
    14. foreach(QString line, lines) { file2 += line + "\r\n"; }
    15. toWrite = file2.toLatin1();
    16. QList<QByteArray> writeArray;
    17. writeArray.append(QByteArray(""));
    18. int charCount = toWrite.length();
    19. int charIndex = 0;int charIndexM = 1;
    20. int writeIndex = 0;
    21. while (charIndex < charCount) {
    22. if (charIndex > 8192 * writeIndex + 1) { writeIndex++; writeArray.append(QByteArray("")); }
    23. writeArray[writeIndex].append(toWrite[charIndex]);
    24. charIndex++;
    25. }
    26. foreach(QByteArray write, writeArray) {
    27. client.write(write,write.length());
    28. while(true) {
    29. bool b = client.waitForBytesWritten();
    30. if (b) {
    31. break;
    32. }
    33. }
    34. }
    35. //
    36. //client.write(toWrite,toWrite.length()); // length = 188,673 characters, 184 KB file size
    37. //
    38. client.flush();
    39. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 1
    Last Post: 15th January 2013, 19:08
  2. Replies: 31
    Last Post: 28th August 2012, 22:55
  3. Methods to display received data
    By pupqt in forum Qt Programming
    Replies: 3
    Last Post: 18th April 2011, 09:50
  4. Replies: 2
    Last Post: 6th November 2010, 05:06
  5. Replies: 1
    Last Post: 2nd November 2009, 12:02

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.