Results 1 to 6 of 6

Thread: Problem with QTcpSockets and QVector

  1. #1
    Join Date
    Mar 2012
    Posts
    3
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Problem with QTcpSockets and QVector

    Hello i am just writing a computer Game , called BattleShips. It's just a computer game with a client,server,client communication where the main communication is just throw a QVector and QTcpsockets,and i am sending these QVectors with an overloaded Function.


    This is the Function where i send my QVector to the Server.

    Qt Code:
    1. void Client::sendPlayground(){
    2.  
    3. // QByteArray wird definiert
    4. QByteArray block;
    5. // QDataStream zum Schreiben in das QByteArray
    6. QDataStream out(&block, QIODevice::WriteOnly);
    7. // Version des DataStereams setzen
    8. out.setVersion(QDataStream::Qt_4_5);
    9. // Daten in den Stream schreiben
    10. out << qint16(0)
    11. << qint8('P')
    12. << *m_playground;
    13.  
    14. // Zurueckspulen und
    15. out.device()->seek(0);
    16. // Datenlaenge in Byte schreiben
    17.  
    18. qint16 blocksize=qint16(block.size() - sizeof(qint16));
    19. qDebug() << "Size of mine Playground " << sizeof(*m_playground);
    20. qDebug() << "Size of Vector 1"<< sizeof(m_playground->m_defArea->getVector());
    21. qDebug() << "Size of Vector 1"<< sizeof(m_playground->m_offArea->getVector());
    22.  
    23. qDebug() << "Blocksize Playground Client" << blocksize << "Size of DataStream" << block.size();
    24. out << blocksize;
    25.  
    26. // QByteArray an den Client schicken
    27. m_serverConnection->write(block);
    28. m_serverConnection->flush();
    29.  
    30. }
    To copy to clipboard, switch view to plain text mode 

    Here is my function for overloading my object called Playground , Playground inherits two Objects called GameArea also stores one QVectors for saving some stats.

    Serialize

    Qt Code:
    1. QDataStream& operator <<(QDataStream & os,const Playground& pg ){
    2.  
    3. os << pg.m_defArea->getSize() << pg.m_defArea->getVector() << pg.m_offArea->getSize() << pg.m_offArea->getVector();
    4.  
    5. return os;
    6.  
    7. }
    8.  
    9.  
    10.  
    11. //Deserialize
    12.  
    13. QDataStream& operator>>(QDataStream & is,Playground& pg ){
    14.  
    15.  
    16.  
    17. QVector<QVector<int> > vec1;
    18.  
    19. QSize size1;
    20.  
    21.  
    22.  
    23. QVector<QVector<int> > vec2;
    24.  
    25. QSize size2;
    26.  
    27.  
    28.  
    29. is >> size1 >> vec2 >> size2 >> vec1;
    30.  
    31.  
    32.  
    33.  
    34.  
    35. pg.m_offArea->setSize(size1);
    36.  
    37. pg.m_offArea->setVector(vec1);
    38.  
    39.  
    40.  
    41. qDebug() << "Size 1" << size1;
    42.  
    43. qDebug() << "Size 2" << size2;
    44.  
    45.  
    46.  
    47. pg.m_defArea->setSize(size1);
    48.  
    49. pg.m_defArea->setVector(vec2);
    50.  
    51.  
    52.  
    53. return is;
    54.  
    55.  
    56.  
    57. }
    To copy to clipboard, switch view to plain text mode 
    and here is my GameArea , this Objects stores a QVector<QVector<int> > m_cells which saves some stats.

    Qt Code:
    1. #ifndef GAMEAREA_H
    2. #define GAMEAREA_H
    3.  
    4. #include <QWidget>
    5. #include <vector>
    6. #include <QMouseEvent>
    7. #include "Game.h"
    8. #include <QDebug>
    9. #include <QVector>
    10. #include <QMap>
    11. #include <QObject>
    12.  
    13.  
    14. class Client;
    15.  
    16. class GameArea : public QWidget
    17. {
    18. Q_OBJECT
    19. public:
    20. GameArea(Client* client);
    21. ~GameArea();
    22. //KopyConstructor
    23. GameArea(const GameArea &a);
    24. GameArea& operator=(const GameArea&a);
    25.  
    26. void setCellState(int x,int y, int value);
    27. int getCellState(int x, int y);
    28. bool isCellEmpty(int x, int y);
    29. void setlocked(bool flag);
    30. void clear();
    31. void setShipMode(Shiptype type);
    32. Shiptype getShipMode();
    33. bool createShip(int x, int y);
    34. void loadResources();
    35.  
    36. //Returns the stored Vector
    37. const QVector<QVector<int> >& getVector() const;
    38. //Returns the size of the Vector
    39. QSize getSize() const;
    40.  
    41. //sets the new Vector
    42. void setVector(QVector<QVector<int> > vec);
    43. //sets new Size
    44. void setSize(const QSize& size);
    45.  
    46. void count_ships();//Hits and Fails in the GameArea for Enemy and You
    47.  
    48. //Get my count ships
    49. QMap<char,int> * get_m_c_ships();
    50.  
    51. //Get all maximum of all Ships
    52. QMap<char,int> * get_m_m_ships();
    53.  
    54. //Get my hits and fails
    55. QMap<char,int> * get_m_hit_fails();
    56.  
    57. //Get hits and fails of the enemy
    58. QMap<char,int> * get_e_hit_fails();
    59.  
    60. bool isReady();
    61.  
    62. signals:
    63. void new_Move();
    64. void won();
    65. void lost();
    66.  
    67. protected:
    68. virtual void paintEvent(QPaintEvent *e);
    69. virtual void mousePressEvent(QMouseEvent *event);
    70. virtual void mouseMoveEvent(QMouseEvent *event);
    71. void getRectForCell(int x, int y, QRect& rect);
    72.  
    73. private:
    74. QPoint m_tagged;
    75. int m_width;
    76. int m_height;
    77. int m_offsetX; // width-offset from margin
    78. int m_offsetY; // height-offset from margin
    79. QVector<QVector<int> > m_cells;
    80. void reserve_m_cells(int rows,int col);
    81. void resize_m_cells(int rows,int col);
    82.  
    83. bool isInitialized;
    84. bool m_locked;
    85. Client* m_client;
    86. Shiptype m_shipType;
    87. QImage* image;
    88.  
    89. QMap<char,int> m_c_ships;
    90. QMap<char,int> m_m_ships;
    91.  
    92.  
    93. QMap<char,int> m_hit_fails;
    94. QMap<char,int> e_hit_fails;
    95.  
    96. };
    97.  
    98. Q_DECLARE_TYPEINFO(GameArea, Q_MOVABLE_TYPE );
    99. Q_DECLARE_TYPEINFO(QVector<QVector<int> >, Q_MOVABLE_TYPE );
    100.  
    101. #endif // GAMEAREA_H
    To copy to clipboard, switch view to plain text mode 

    Here is my function to get this QVector from my Object

    Qt Code:
    1. const QVector<QVector<int> >& GameArea::getVector() const{
    2.  
    3.  
    4. return m_cells;
    5.  
    6. }
    To copy to clipboard, switch view to plain text mode 



    I don't know where the problem is , it's possible to exchange this QVectors via QDataStream andQTcpSockets but after some time after relogging both to the QTcpServer,the size of the QDataStream just increased and is increasing,i don't know is it a memory leak or a problem with the QVector and writing it into the QDataStream.It's possible to play against another without any problems but after ending the session and relogging to the QTcpServer this failure occures.
    It's a new session,in a new thread of the server.


    With best regards Mark
    Last edited by wysota; 26th March 2012 at 14:38. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Problem with QTcpSockets and QVector

    What does the code for receiving data over network look like?
    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.


  3. #3
    Join Date
    Mar 2012
    Posts
    3
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Problem with QTcpSockets and QVector

    Here is it from the QTcpServer, one function for one socket , just sychronised in the run methode , i know that the QTcpSocket has an internal buffer which is limited , but this is no the main problem.

    Qt Code:
    1. bool ServerThread::readData_1(){
    2. // QDataSteram aus TcpSocket erzeugen
    3. QDataStream in(m_client_Connection_1);
    4. in.setVersion(QDataStream::Qt_4_5);
    5. qint16 blocksize=0;
    6. qint8 ch;
    7.  
    8. qDebug() << "Size Packet Client 1" << m_client_Connection_1->size();
    9.  
    10. // versuche Blockgroesse zu lesen
    11. if (m_client_Connection_1->bytesAvailable() < (int)sizeof(qint16)) {
    12. // keine Daten vorhanden
    13. qDebug() << "Blocksize 1 false";
    14. return false;
    15. }
    16. in >> blocksize;
    17.  
    18.  
    19. // versuche Daten zu lesen
    20. if (m_client_Connection_1->bytesAvailable() < blocksize) {
    21. // Daten in Blockgroesse nicht vorhanden
    22. qDebug() << "Blocksize 1 false " <<"Blocksize" << blocksize << "Size" << m_client_Connection_1->size();
    23. return false;
    24. }
    25.  
    26. in >> ch;
    27.  
    28. qDebug() << (char)ch;
    29.  
    30. //Player 1 give up //Player 2 won
    31. if(ch == 'Q'){
    32. qDebug() << "Client 1 Quit" ;
    33. client_1_has_left=true;
    34. client_sendData_Q(m_client_Connection_2);
    35. client_sendData_L(m_client_Connection_2);
    36. m_client_Connection_2->waitForBytesWritten(10);
    37. m_client_Connection_1->close();
    38. m_client_Connection_2->close();
    39. this->quit();
    40. return false;
    41. }
    42. //Name
    43. if(ch == 'N'){
    44. readName(in);
    45. return true;
    46. }
    47. //Ready
    48. if(ch == 'R' ){
    49. this->client_1_isready=true;
    50. qDebug() << "Client 1 is ready ";
    51. return true;
    52. }
    53. if(ch == 'L'){
    54. qDebug() << "Client 1 got Won Signal";
    55. client_sendData_L(m_client_Connection_2);
    56. return true;
    57. }
    58. if(ch == 'P'){
    59.  
    60. QVector<QVector<int> > vec1;
    61. QSize size1;
    62.  
    63. QVector<QVector<int> > vec2;
    64. QSize size2;
    65.  
    66. in >> size1 >> vec1 >> size2 >> vec2;
    67.  
    68. client_sendData_P(m_client_Connection_2,vec1,size1,vec2,size2);
    69.  
    70. vec1.clear();
    71. vec2.clear();
    72.  
    73. if(m_client_Connection_1->size() > 0){
    74. readData_1();
    75.  
    76. }
    77.  
    78. return true;
    79. }
    80.  
    81. return false;
    82.  
    83. }
    To copy to clipboard, switch view to plain text mode 

    Here is the main problem at client, in the function send_playground(),i just did an screen shot of my console and the during the first game the size of my stream has not changed,but after relogging to the server the size of my stream which is send in the function send_playground has changed from 4377 to 10905, so why is it , during the last game and the next game the size has not changed of my object(m_playground) or the Vectors which are stored in it,but the size of my stream which is send throw the socket to server has changed.



    client.jpg

    so the datasize has just increased(QDataStream),but (perhaps) not the size of my objects
    Last edited by wysota; 26th March 2012 at 21:05. Reason: missing [code] tags

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

    Default Re: Problem with QTcpSockets and QVector

    What is the type of m_client_Connection_1? It looks like you are trying to read 16 bits of data each time the function is called which corrupts your stream if there is not enough data to read after you already know the block size. Then you seem to be completely ignoring the block size you just read and you start reading from the stream regardless if there is enough data available or not.
    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.


  5. #5
    Join Date
    Mar 2012
    Posts
    3
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Problem with QTcpSockets and QVector

    Yeah it's true , that i am not looking if there is enough space or not,but in the beginning 16 bits of data are just enough to communicate there is no problem ,that the size of my stream is not enough,the communication between my clients and servers works.But afterwards the size of stream has changed, and i don't know why.

    Here is a picture,that u know how my games looks like , there is a grid and the status are stored in an QVector<QVector<int> > m_cells, and the size of my grid hasn't changed during playing so also the size of my vectors but the size of my stream has changed after relogging and maybe it's getting more and more,i am also looking if the stream is clear or not at the clients and at the server during starting and stopping the session.
    Game.jpg

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Problem with QTcpSockets and QVector

    Quote Originally Posted by Krieger910 View Post
    Yeah it's true , that i am not looking if there is enough space or not,but in the beginning 16 bits of data are just enough to communicate there is no problem ,that the size of my stream is not enough,the communication between my clients and servers works. But afterwards the size of stream has changed, and i don't know why.
    You are losing sync with your protocol (which is QDataStream). You get bogus data and you misinterpret it. You need to redesign how your communication looks like, taking into consideration the nature of TCP which is often forgotten by people. You can't neglect properties of a protocol when trying to use it. If your stream contains 1000 messages, you will only read the first one. That's one of the possibilities why your stream changes its size.
    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. Replies: 5
    Last Post: 3rd September 2011, 00:11
  2. Replies: 4
    Last Post: 22nd July 2010, 16:49
  3. Multiple connections with QTcpSockets
    By DrDonut in forum Qt Programming
    Replies: 1
    Last Post: 11th September 2009, 11:58
  4. QTCPSockets
    By Thunder in forum Qt Programming
    Replies: 0
    Last Post: 6th August 2008, 19:19
  5. QTcpSockets and QThreads
    By TheRonin in forum Newbie
    Replies: 3
    Last Post: 21st June 2006, 10:41

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
  •  
Qt is a trademark of The Qt Company.