Page 2 of 3 FirstFirst 123 LastLast
Results 21 to 40 of 57

Thread: QSslSocket vs QTcpSocket problem

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

    Default Re: QSslSocket vs QTcpSocket problem

    Now I see the point and I am slowly moving to the idea you presented. But I have a problem constructing the while loop which is suppose to get the size of the next incoming data I tried this but I know that fails:

    Qt Code:
    1. QByteArray buffer;
    2.  
    3. void Server::onreadyRead()
    4. {
    5. while(buffer.size() < (int)sizeof(quint64))
    6. {
    7. clientSocket->waitForReadyRead(1);
    8. blockSizee.append(clientSocket->readAll());
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 

    TEST:
    The byte size of the "size information", sended by server, is about 8kB but after first readyRead signal client gets first 4kB, and after second signal buffer meets the while condition and skips it, and I can't get the full right size.
    Last edited by wysota; 8th March 2011 at 00:06. Reason: missing [code] tags

  2. #22
    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: QSslSocket vs QTcpSocket problem

    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. } else return;
    10. }
    11. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 8th March 2011 at 12:34.
    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. #23
    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?

  4. #24
    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: 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.


  5. #25
    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 

  6. #26
    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: 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.


  7. #27
    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 11:59.

  8. #28
    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: 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.


  9. #29
    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.

  10. #30
    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: 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.


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

    Default Re: QSslSocket vs QTcpSocket problem

    But you wrote sth like this :

    Quote Originally Posted by wysota View Post
    Either like so:
    Qt Code:
    1. QByteArray buffer;
    2. while(buffer.size() < blockSize) {
    3. socket->waitForReadyRead();
    4. buffer.append(socket->readAll());
    5. }
    6. doSomethingWith(buffer);
    To copy to clipboard, switch view to plain text mode 
    [/code]
    and I thought that this refers to receiving exact data data, and this blockSize I will get by this last code which I've posted thanks to you.

  12. #32
    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: QSslSocket vs QTcpSocket problem

    I don't see how this code snippet is related to what I have just said
    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.


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

    Default Re: QSslSocket vs QTcpSocket problem

    sorry for noobing, but after what I've read and like you so my first code which worked with QTcpSocket(examples from Qt), I understand it in this way:
    1.server is sending data and also at the begining of them server sends the size of data that were written to the socket
    2. At the other side client is waiting for data but firstly gets the size, and then knows when to stop receiving the data.

    It looked pretty like this in the examples and I've modified them for my needs. But now I see it isn't the right way when I use SSL however I can't understand it in 100% now, however I'm convinced that is a better method and more Qt-like, that it is why it is so important for me.

  14. #34
    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: QSslSocket vs QTcpSocket problem

    Please make sure you understand the code I gave before using it. And never use code you do not understand.
    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.


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

    Default Re: QSslSocket vs QTcpSocket problem

    Please tell me before I will study hard your code, what do you mean by this line

    processData(data) (I can't figure out when I should pull out the data I need and for example write the in to the file)

    becouse I don't know how to refer to it, and maybe are there any signals I should take a closer look?
    Last edited by camol; 10th March 2011 at 14:04.

  16. #36
    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: QSslSocket vs QTcpSocket problem

    processData() is a stub for your method for manipulating the data, the core of the server functionality. I don't know what your program is meant to do so I can't tell you what should be there.
    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.


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

    Default Re: QSslSocket vs QTcpSocket problem

    basically I am sending files. The server after establishing connection is sending many many files one by one.

  18. #38
    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: QSslSocket vs QTcpSocket problem

    So probably processData() should store the data on the disk or somewhere else.
    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.


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

    Default Re: QSslSocket vs QTcpSocket problem

    But I have noticed that processData() is in if-condition, and as you can see from my "tests" the program is in that if-condition (you wrote) only once (at the first enter to the slot) and at that time data.size is 0.

  20. #40
    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: QSslSocket vs QTcpSocket problem

    Did you adjust the sending side to the semantics of the receiving side or the other way round?
    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, 12:03
  2. QSslSocket - problem with connecting to the server
    By kremuwa in forum Qt Programming
    Replies: 9
    Last Post: 26th August 2010, 15:40
  3. Problem in QTcpSocket
    By navi1084 in forum Qt Programming
    Replies: 2
    Last Post: 16th October 2008, 13:12
  4. QSslSocket problem
    By The Storm in forum Qt Programming
    Replies: 5
    Last Post: 23rd March 2008, 13:58
  5. problem with QTcpSocket
    By SuperSonik in forum Qt Programming
    Replies: 8
    Last Post: 31st January 2007, 17: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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.