Results 1 to 4 of 4

Thread: QSslSocket reconnect to host

  1. #1

    Default QSslSocket reconnect to host

    If I am trying to connect to an invalid host, and, after the connection fails, I try to connect to a valid host, the connection still fails.
    Qt Code:
    1. void testSoc::test()
    2. {
    3. switch (state)
    4. {
    5. case 0:
    6. case 2:
    7. case 4:
    8. qDebug ("Connecting to a valid host. Should be Ok");
    9. timeout->start(10000);
    10. soc->connectToHost(VALID_HOST, VALID_PORT);
    11. break;
    12. case 1:
    13. qDebug ("Connecting to a invalid port. Should fail");
    14. timeout->start(10000);
    15. soc->connectToHost(VALID_HOST, INVALID_PORT);
    16. break;
    17. case 3:
    18. qDebug ("Connecting to a invalid host. Should fail");
    19. timeout->start(10000);
    20. soc->connectToHost(INVALID_HOST, VALID_PORT);
    21. break;
    22. }
    23. }
    24. void testSoc::checkConnection()
    25. {
    26. timeout->stop();
    27. if (soc->state() == QAbstractSocket::ConnectedState)
    28. {
    29. connected();
    30. } else {
    31. qDebug ("Connection failed");
    32. soc->disconnectFromHost();
    33. if (soc->state() != QAbstractSocket::UnconnectedState)
    34. soc->waitForDisconnected();
    35. soc->close();
    36. //delete soc;
    37. //soc = new QSslSocket;
    38. state ++;
    39. test();
    40. }
    41. }
    42.  
    43. * and of course there is
    44.  
    45. connect (soc, SIGNAL(connected()),
    46. this, SLOT(connected()));
    47. connect (timeout, SIGNAL(timeout()),
    48. this, SLOT(checkConnection()));
    To copy to clipboard, switch view to plain text mode 
    In this code, all gives the expected results, except for case 4 (including case 2, reconnecting after trying to connect to a valid host, with invalid port)

    deleting and re-creating the socket does not help
    (using Qt 4.4.3)

  2. #2
    Join Date
    Dec 2019
    Posts
    1
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: QSslSocket reconnect to host

    Hi,

    did you find a solution for your problem?

  3. #3
    Join Date
    Jun 2012
    Location
    Austria
    Posts
    9
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QSslSocket reconnect to host

    I have no experience with QSslSocket, but for QTcpSocket I never managed to establish a connection after an initial failure. I usually delete the failed QTcpSocket and create a new one for the next try. This seems to work fine without excessive overhead.
    For some reason I can't remember right now I disconnected the socket before deleting. Here the snippet from my code wich trigger the reconnect attempt:
    Qt Code:
    1. if(socket->state()!=QAbstractSocket::ConnectedState)
    2. {
    3. disconnect(socket, nullptr, nullptr, nullptr);
    4. socket->close();
    5. socket->deleteLater();
    6. socket=nullptr;
    7. lastErr_=1002;
    8. lastErrStr_=QString("NetCon: connection attempt timed out after %1 ms").arg(toCon_);
    9. qWarning()<<lastErrStr_;
    10. emit errorSig(lastErr_);
    11. emit readySig(false);
    12. if(!isServer_)
    13. {
    14. if(testOnly)
    15. qInfo()<<"test only - canceling reconnect attempt";
    16. if(toCon_<86400000)
    17. toCon_*=2; // will level off at 2^27 ~= 37h retry inteval
    18. qWarning()<<"NetCon: retry to connect to"<<address_.toString();
    19. start();
    20. }
    21. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,630
    Thanks
    13
    Thanked 1,592 Times in 1,520 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QSslSocket reconnect to host

    Not pretty, but this variation on the original code works fine for me without recreating sockets etc.:
    Qt Code:
    1. #ifndef TESTSOC_H
    2. #define TESTSOC_H
    3.  
    4. #include <QTimer>
    5. #include <QTcpSocket>
    6. #include <QDebug>
    7.  
    8.  
    9. class testSoc: public QObject {
    10. Q_OBJECT
    11. int state;
    12. QTcpSocket *soc;
    13. QTimer *timeout;
    14. public:
    15. explicit testSoc(QObject *p = nullptr);
    16. ~testSoc();
    17. void test();
    18.  
    19. private slots:
    20. void connected();
    21. void handleError(QAbstractSocket::SocketError socketError);
    22. void handleTimeout();
    23.  
    24. private:
    25. void nextState();
    26. };
    27.  
    28. #endif // TESTSOC_H
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #include "testSoc.h"
    2.  
    3. #define VALID_HOST "www.google.com"
    4. #define VALID_PORT 80
    5. #define INVALID_HOST "localhost"
    6. #define INVALID_PORT 81
    7. #define TIMEOUT 3000
    8.  
    9. testSoc::testSoc(QObject *p):
    10. QObject(p),
    11. state(0),
    12. soc(nullptr),
    13. timeout(nullptr)
    14. {
    15. soc = new QTcpSocket(this);
    16. connect (soc, SIGNAL(connected()), this, SLOT(connected()));
    17. connect (soc, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(handleError(QAbstractSocket::SocketError)));
    18.  
    19. timeout = new QTimer(this);
    20. timeout->setSingleShot(true);
    21. timeout->setInterval(TIMEOUT);
    22. connect (timeout, SIGNAL(timeout()), this, SLOT(handleTimeout()));
    23. }
    24.  
    25. testSoc::~testSoc() {
    26. }
    27.  
    28. void testSoc::test()
    29. {
    30. switch (state)
    31. {
    32. case 0:
    33. case 2:
    34. case 4:
    35. qDebug () << "State" << state << "Connecting to a valid host. Should be Ok";
    36. timeout->start();
    37. soc->connectToHost(VALID_HOST, VALID_PORT);
    38. break;
    39. case 1:
    40. qDebug () << "State" << state << "Connecting to a invalid port. Should fail";
    41. timeout->start();
    42. soc->connectToHost(VALID_HOST, INVALID_PORT);
    43. break;
    44. case 3:
    45. qDebug () << "State" << state << "Connecting to a invalid host. Should fail";
    46. timeout->start();
    47. soc->connectToHost(INVALID_HOST, VALID_PORT);
    48. break;
    49. }
    50. }
    51.  
    52. void testSoc::connected() {
    53. timeout->stop();
    54. qDebug() << "State" << state << "Connected to" << soc->peerName() << "port" << soc->peerPort();
    55. // do some work here
    56. soc->disconnectFromHost();
    57. nextState();
    58. }
    59.  
    60. void testSoc::handleError(QAbstractSocket::SocketError socketError) {
    61. timeout->stop();
    62. qDebug() << "State" << state << "Error reported" << socketError;
    63. nextState();
    64. }
    65.  
    66. void testSoc::handleTimeout()
    67. {
    68. timeout->stop();
    69. qDebug () << "State" << state << "Connection time out";
    70. // Using disconnectFromHost() on a host that was never connected
    71. // just causes a long pause for some internal timeout.
    72. soc->abort();
    73. nextState();
    74. }
    75.  
    76. void testSoc::nextState()
    77. {
    78. state = (state + 1) % 5;
    79. test();
    80. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #include <QApplication>
    2. #include "testSoc.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. testSoc t;
    8. t.test();
    9. return a.exec();
    10. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Reconnect signal/slot between two objects
    By MadBear in forum Qt Programming
    Replies: 6
    Last Post: 6th October 2010, 06:26
  2. need to reconnect QWebView socket
    By dimril in forum Qt Programming
    Replies: 1
    Last Post: 7th September 2010, 18:28
  3. Replies: 1
    Last Post: 17th July 2010, 12:02
  4. how to reconnect CORRECTLY qmysql database?
    By yaseminyilmaz in forum Newbie
    Replies: 7
    Last Post: 12th January 2010, 13:09
  5. Reconnect problem
    By xgoan in forum Qt Programming
    Replies: 1
    Last Post: 8th November 2006, 19:51

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.