Results 1 to 7 of 7

Thread: problem with QByteArray data !

  1. #1
    Join Date
    Jan 2011
    Posts
    33
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default problem with QByteArray data !

    i am writing a chat application using TCP.
    i found a simple exemple of text chat here : http://www.qtcentre.org/wiki/index.p...le=Simple_chat.
    i edited its code so that it can send QByteArray data instead of Qstring like it did in the beginning,but after editing, Server didn't send back message to clients.
    help me fix my code.

    a part of simplechatclient.cpp :
    Qt Code:
    1. void SimpleChatClient::sendMessage()
    2. {
    3. if(socket == NULL) return;
    4. if(socket->state() != QAbstractSocket::ConnectedState ) return;
    5. QString msg = "TEXT";
    6. QString data = "<" + nick->text().toLatin1() + "> " + message->text().toLatin1() + "\n";
    7. QByteArray block;
    8. QDataStream out(&block, QIODevice::WriteOnly);
    9. out.setVersion(QDataStream::Qt_4_2);
    10. out << (quint32) 0;
    11. out << msg;
    12. out << data;
    13. out.device()->seek(0);
    14. out << (quint32)(block.size() - sizeof(quint32));
    15.  
    16. socket->write(block);
    17. socket->flush();
    18. message->clear();
    19. QMessageBox::information( new QWidget, "Application name",
    20. "Client send ok." );
    21. }
    22.  
    23. void SimpleChatClient::receiveMessage()
    24. {
    25. if (socket == NULL) return;
    26. if (socket->state() != QAbstractSocket::ConnectedState) return;
    27.  
    28. QDataStream in(socket);
    29. in.setVersion(QDataStream::Qt_4_2);
    30. //in.setVersion( in.version() ); // set to the current Qt version instead
    31.  
    32. if(blockSize == 0) {
    33. if (socket->bytesAvailable() < (int)sizeof(quint32)) return;
    34. in >> blockSize;
    35. }
    36.  
    37. if(socket->bytesAvailable() < blockSize) return;
    38. QString msgString;
    39. QString msgData;
    40. in >> msgString;
    41. in >> msgData;
    42.  
    43. chat->append(msgData);
    44.  
    45. blockSize = 0;
    46.  
    47. if(socket->bytesAvailable() > 0) receiveMessage();
    48. }
    To copy to clipboard, switch view to plain text mode 

    a part of simplechatsever.cpp :
    Qt Code:
    1. void SimpleChatServer::receiveMessage()
    2. {
    3. QTcpSocket* socket = static_cast<QTcpSocket*>(sender());
    4.  
    5. if (socket == NULL) return;
    6. if ( socket->state() != QAbstractSocket::ConnectedState ) return;
    7.  
    8. QDataStream in(socket);
    9. in.setVersion(QDataStream::Qt_4_2);
    10.  
    11. if (blockSize == 0) {
    12. if (socket->bytesAvailable() < (int)sizeof(quint32)) return;
    13. in >> blockSize;
    14. }
    15. if (socket->bytesAvailable() < blockSize) return;
    16. QString msgString;
    17. QString msgData;
    18. in >> msgString;
    19. in >> msgData;
    20.  
    21. emit onMessage( msgString, msgData );
    22. blockSize = 0;
    23. if (socket->bytesAvailable() > 0) receiveMessage();
    24.  
    25. }
    26. void SimpleChatServer::onMessage(const QString &msg, const QString &data)
    27. {
    28. QByteArray block;
    29. QDataStream out(&block, QIODevice::WriteOnly);
    30. out.setVersion(QDataStream::Qt_4_2);
    31. out << (quint32) 0;
    32. out << msg;
    33. out << data;
    34. out.device()->seek(0);
    35. out << (quint32)(block.size() - sizeof(quint32));
    36. foreach (QTcpSocket* connection, connections)
    37. {
    38. connection->write(block);
    39. connection->flush();
    40. }
    41. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: problem with QByteArray data !

    i edited its code so that it can send QByteArray data instead of Qstring like it did in the beginning,but after editing, Server didn't send back message to clients.
    I hope you don't expect us to go to the other thread, and compare the code there with your code.
    Please post the changes you made so that we can see them in relation to the original.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Jan 2011
    Posts
    33
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: problem with QByteArray data !

    i' sorry .

    this is the original code :

    a part of simplechatclient.cpp :
    Qt Code:
    1. void SimpleChatClient::sendMessage()
    2. {
    3. // "<nick> message\n"
    4. socket->write("<" + nick->text().toLatin1() + "> " + message->text().toLatin1() + "\n");
    5. message->clear();
    6. }
    7.  
    8. void SimpleChatClient::receiveMessage()
    9. {
    10. // missing some checks for returns values for the sake of simplicity
    11. qint64 bytes = buffer->write(socket->readAll());
    12. // go back as many bytes as we just wrote so that it can be read
    13. buffer->seek(buffer->pos() - bytes);
    14. // read only full lines, line by line
    15. while (buffer->canReadLine())
    16. {
    17. QString line = buffer->readLine();
    18. chat->append(line.simplified());
    19. }
    20. }
    To copy to clipboard, switch view to plain text mode 

    a part of simplechatsever.cpp :

    Qt Code:
    1. void SimpleChatServer::receiveMessage()
    2. {
    3. QTcpSocket* socket = static_cast<QTcpSocket*>(sender());
    4. QBuffer* buffer = buffers.value(socket);
    5.  
    6. // missing some checks for returns values for the sake of simplicity
    7. qint64 bytes = buffer->write(socket->readAll());
    8. // go back as many bytes as we just wrote so that it can be read
    9. buffer->seek(buffer->pos() - bytes);
    10. // read only full lines, line by line
    11. while (buffer->canReadLine())
    12. {
    13. QByteArray line = buffer->readLine();
    14. foreach (QTcpSocket* connection, connections)
    15. {
    16. connection->write(line);
    17. }
    18. }
    19. }
    To copy to clipboard, switch view to plain text mode 

    I had fix only at these methods And declare quint32 blockSize in the header file.

  4. #4
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: problem with QByteArray data !

    Did you not try to debug your code before posting here?

    Eg. does sendMessage transmit a valid message?
    Which lines of receiveMessage are actually executed?

  5. #5
    Join Date
    Jan 2011
    Posts
    33
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: problem with QByteArray data !

    i try to debug program and i get some error on "if (socket->bytesAvailable() < blockSize) return; ", i delete it from my source code then sendMessage and receiveMessage run on both server and client, but messenge data not receive, i have just known a little about it, but not mastered yet!

    help me ,i really need it :-(( thank in advance .

  6. #6
    Join Date
    Jan 2011
    Posts
    33
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: problem with QByteArray data !

    UP ... help me :-(( i verry need it :-S

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: problem with QByteArray data !

    Check what blockSize contains after you stream data to it from the data stream.

    By the way, I think the code for the chat needs to be reviewed. Creating a data stream in a slot connected to readyRead() is really a bad idea.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. QByteArray data manuplation.
    By nagabathula in forum Qt Programming
    Replies: 6
    Last Post: 4th December 2010, 15:08
  2. Replies: 3
    Last Post: 29th April 2010, 19:11
  3. inver data from QByteArray
    By aj2903 in forum Qt Programming
    Replies: 2
    Last Post: 16th January 2010, 07:56
  4. QByteArray problem
    By Misenko in forum Qt Programming
    Replies: 17
    Last Post: 4th October 2008, 21:53
  5. QByteArray with network data
    By merlvingian in forum Qt Programming
    Replies: 1
    Last Post: 1st June 2007, 17:53

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.