Results 1 to 20 of 57

Thread: QSslSocket vs QTcpSocket problem

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2011
    Posts
    53
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSslSocket vs QTcpSocket problem

    Quote Originally Posted by wysota View Post
    You don't need such while loops.

    Qt Code:
    1. void Server::onReadyRead(){
    2. buffer.append(socket->readAll());
    3. while(buffer.size() >=4) {
    4. uint32 blockSize = buffer.at(0) << 24 | buffer.at(1) << 16 | buffer.at(2) << 8 | buffer.at(3); // or similar, e.g. qFromBigEndian
    5. if(buffer.size()>=4+blockSize){
    6. QByteArray data = buffer.mid(4,blockSize);
    7. buffer = buffer.mid(4+blockSize);
    8. processData(data);
    9. }
    10. }
    11. }
    To copy to clipboard, switch view to plain text mode 
    why you use while(buffer.size() >=4)? I mean for what "4" stands here for?

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

    Default Re: QSslSocket vs QTcpSocket problem

    4 stands for sizeof(quint32).

    Actually my code had a bug, it was missing an "else" statement. I just corrected it.
    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 2011
    Posts
    53
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSslSocket vs QTcpSocket problem

    Qt Code:
    1. void Server::onReadyRead()
    2. {
    3. if(data == Size_of_Data)
    4. {
    5. QTextStream(stdout) << " Entering the slot = "<<a++ <<endl;
    6. buffer.append(clientSocket->readAll());
    7. while(buffer.size() >=4)
    8. {
    9. QTextStream(stdout) << " in while = "<<b++ <<endl;
    10. quint32 blockSize = buffer.at(0) << 24 | buffer.at(1) << 16 | buffer.at(2) << 8 | buffer.at(3); // or similar, e.g. qFromBigEndian
    11.  
    12. QTextStream(stdout) << " before IF buffer = "<<buffer.size() <<endl;
    13. QTextStream(stdout) << " before IF blockSize = "<<sizeof(blockSize)<<endl;
    14.  
    15. if(buffer.size()>=4+blockSize)
    16. {
    17. QTextStream(stdout) << " entering IF = "<<c++ <<endl;
    18. QByteArray data = buffer.mid(4,blockSize);
    19. buffer = buffer.mid(4+blockSize);
    20. QTextStream(stdout) << " in IF data = "<<data.size() <<endl;
    21. QTextStream(stdout) << " in IF buffer = "<<buffer.size() <<endl;
    22. //processData(data);
    23. }
    24. else
    25. {
    26. QTextStream(stdout) << " on output buffer.size = "<<buffer.size() <<endl;
    27. return;
    28. }
    29. }
    30. }
    31.  
    32. else if(data == Exact_Data)
    33. {
    34. QTextStream(stdout) << "getting the right data"<<endl;
    35. }
    36. }
    To copy to clipboard, switch view to plain text mode 

    I wrote sth like this, but i don't know exactle when I should pull out the right buffer.size out of the slot (to use it for expecting the incomimg file data)becouse, I've run a simple test and got:

    Qt Code:
    1. Entering the slot = 0
    2. in while = 0
    3. before IF buffer = 4096
    4. before IF blockSize = 4
    5. entering IF = 0
    6. in IF data = 0
    7. in IF buffer = 4092
    8. in while = 1
    9. before IF buffer = 4092
    10. before IF blockSize = 4
    11. on output buffer.size = 4092
    12. Entering the slot = 1
    13. in while = 2
    14. before IF buffer = 8188
    15. before IF blockSize = 4
    16. on output buffer.size = 8188
    17. Entering the slot = 2
    18. in while = 3
    19. before IF buffer = 8848
    20. before IF blockSize = 4
    21. on output buffer.size = 8848
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: QSslSocket vs QTcpSocket problem

    Lines #3 and #18 suggest you have two variables called "data" where one shadows the other. What's the purpose of if in #3 anyway?
    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 2011
    Posts
    53
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSslSocket vs QTcpSocket problem

    But one is global the first one and the second one is local for the method, I've changed the first "data" to in_data but it doesn't has any influence. The in_data is an enum type, which I've created, becouse I thought slot onReadyRead always starts after signal readyRead so in_data informs that the incoming data is no longer the size of the file, but exactly the expected file.

    EDIT:
    When I'm sending a bigger file and I try get the size of it, the result of my test is a bit different:
    Qt Code:
    1. Entering the slot = 0
    2. in while = 0
    3. before IF buffer = 4096
    4. before IF blockSize = 4
    5. entering IF = 0
    6. in IF data = 0
    7. in IF buffer = 4092
    8. in while = 1
    9. before IF buffer = 4092
    10. before IF blockSize = 4
    11. on output buffer.size = 4092
    12. Entering the slot = 1
    13. in while = 2
    14. before IF buffer = 8188
    15. before IF blockSize = 4
    16. on output buffer.size = 8188
    17. Entering the slot = 2
    18. in while = 3
    19. before IF buffer = 12284
    20. before IF blockSize = 4
    21. on output buffer.size = 12284
    22. Entering the slot = 3
    23. in while = 4
    24. before IF buffer = 16380
    25. before IF blockSize = 4
    26. on output buffer.size = 16380
    27. Entering the slot = 4
    28. in while = 5
    29. before IF buffer = 17688
    30. before IF blockSize = 4
    31. entering IF = 1
    32. in IF data = 17684
    33. in IF buffer = 0
    To copy to clipboard, switch view to plain text mode 
    Becouse my "test" ends in IF instruction now.

    Another EDIT:
    I've changed "4" in your code to "8" and now it stands for quint64 and everything looks like in the first example.
    Last edited by camol; 10th March 2011 at 10:59.

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

    Default Re: QSslSocket vs QTcpSocket problem

    I still don't understand what the if is for. Anyway, sizeof(blockSize) will always return 4 (it's a 32 bit integer, after all) so I don't understand why you're checking it. Just to make sure - does the sending side encode the block size the same way the receiving size decodes it?
    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.


  7. #7
    Join Date
    Mar 2011
    Posts
    53
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSslSocket vs QTcpSocket problem

    I'm sending it like this:

    Qt Code:
    1. void DServer::response_to_replication()
    2. {
    3. QTextStream(stdout) << "wszedlem do RESPONSE_TO_REPLICATION" <<endl;
    4.  
    5. /********************************************************************/
    6. /*SENDING LIST WITH STORED FILES TO SERVER WHICH ESTABLISHED CONNECTION*/
    7. /********************************************************************/
    8.  
    9. blockSize = 0;
    10. QByteArray block;
    11. block.clear();
    12. QDataStream out(&block, QIODevice::WriteOnly);
    13. out.setVersion(QDataStream::Qt_4_6);
    14. serverfiles.clear();
    15. serverfiles = rfilelist();
    16.  
    17.  
    18. out << (quint64)0;
    19. out << (out,serverfiles);
    20.  
    21. out.device()->seek(0);
    22. out << (quint64)(block.size() - sizeof(quint64));
    23. }
    To copy to clipboard, switch view to plain text mode 

    the IF-thing: becouse date are coming to the client and when they are ready to read at client side the signal readyRead is emitted which is connected to one single slot called onReadyRead, so when I get the incoming data and interpretate them as the information about the size which was sent by server side of the connection. The signal readyRead will be still emited becouse now client will be receiving the exact data, and the onReadyRead slot will be triggered, so I want to prevent by using this IF-thing to prevent this slot from behaving as it is receiving size when it is already receving the exact expected data.

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

    Default Re: QSslSocket vs QTcpSocket problem

    Quote Originally Posted by camol View Post
    I'm sending it like this:
    This won't work, obviously. The receiving end expects something completely different.


    the IF-thing: becouse date are coming to the client and when they are ready to read at client side the signal readyRead is emitted which is connected to one single slot called onReadyRead, so when I get the incoming data and interpretate them as the information about the size which was sent by server side of the connection. The signal readyRead will be still emited becouse now client will be receiving the exact data, and the onReadyRead slot will be triggered, so I want to prevent by using this IF-thing to prevent this slot from behaving as it is receiving size when it is already receving the exact expected data.
    Why do you want to prevent it? I think you don't understand the code I gave you. The slot is meant to fire each time new data arrives into the socket. Without any exceptions or special conditions. Otherwise you'll just lifelock your application.
    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. problem with QTcpSocket
    By Fallen_ in forum Qt Programming
    Replies: 10
    Last Post: 28th November 2010, 11:03
  2. QSslSocket - problem with connecting to the server
    By kremuwa in forum Qt Programming
    Replies: 9
    Last Post: 26th August 2010, 14:40
  3. Problem in QTcpSocket
    By navi1084 in forum Qt Programming
    Replies: 2
    Last Post: 16th October 2008, 12:12
  4. QSslSocket problem
    By The Storm in forum Qt Programming
    Replies: 5
    Last Post: 23rd March 2008, 12:58
  5. problem with QTcpSocket
    By SuperSonik in forum Qt Programming
    Replies: 8
    Last Post: 31st January 2007, 16:00

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.