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