Dear all,

I implemented a simple chat server in order to receive a msg from a device. The msg is a string and i would like to tokenize it in order to put the pieces of it to my sql database. Could somebody send me a code example how this can be done?

Thank you in advance!

Qt Code:
  1. #include "simplechatserver.h"
  2. #include <QBuffer>
  3. #include <QByteArray>
  4. #include <QString>
  5. #include <QTcpSocket>
  6. #include <QStringList>
  7. #include <iostream>
  8.  
  9. SimpleChatServer::SimpleChatServer(QObject* parent) : QTcpServer(parent)
  10. {
  11. std::cout << "Message server started" << std::endl;
  12. connect(this, SIGNAL(newConnection()), this, SLOT(addConnection()));
  13. }
  14.  
  15. SimpleChatServer::~SimpleChatServer()
  16. {
  17. }
  18.  
  19. void SimpleChatServer::addConnection()
  20. {
  21. std::cout <<"new incomming connection" << std::endl;
  22. QTcpSocket* connection = nextPendingConnection();
  23. connections.append(connection);
  24. QBuffer* buffer = new QBuffer(this);
  25. buffer->open(QIODevice::ReadWrite);
  26. buffers.insert(connection, buffer);
  27. connect(connection, SIGNAL(disconnected()), SLOT(removeConnection()));
  28. connect(connection, SIGNAL(readyRead()), SLOT(receiveMessage()));
  29. }
  30.  
  31. void SimpleChatServer::removeConnection()
  32. {
  33. std::cout << "removing connection "<< std::endl;
  34. QTcpSocket* socket = static_cast<QTcpSocket*>(sender());
  35. QBuffer* buffer = buffers.take(socket);
  36. buffer->close();
  37. buffer->deleteLater();
  38. connections.removeAll(socket);
  39. socket->deleteLater();
  40. }
  41.  
  42. void SimpleChatServer::receiveMessage()
  43. {
  44. std::cout << "receiving" <<std::endl;
  45. QTcpSocket* socket = static_cast<QTcpSocket*>(sender());
  46. QBuffer* buffer = buffers.value(socket);
  47.  
  48. // missing some checks for returns values for the sake of simplicity
  49. qint64 bytes = buffer->write(socket->readAll());
  50. // go back as many bytes as we just wrote so that it can be read
  51. buffer->seek(buffer->pos() - bytes);
  52.  
  53.  
  54. // while(buffer->canReadLine())
  55. {
  56. line = buffer->readLine();
  57. QString str(line.data());
  58. std::cout << str.toStdString() << std::endl;
  59.  
  60.  
  61. QStringList list[] = {str.split(QRegExp(" "), QString::SkipEmptyParts)}; ????????????
  62. //std::cout<< list.<<std::endl; ???????????
  63.  
  64.  
  65.  
  66. emit emitMessage(str);
  67.  
  68.  
  69. }
  70. }
To copy to clipboard, switch view to plain text mode