Not pretty, but this variation on the original code works fine for me without recreating sockets etc.:
#ifndef TESTSOC_H
#define TESTSOC_H
#include <QTimer>
#include <QTcpSocket>
#include <QDebug>
Q_OBJECT
int state;
public:
explicit testSoc
(QObject *p
= nullptr
);
~testSoc();
void test();
private slots:
void connected();
void handleTimeout();
private:
void nextState();
};
#endif // TESTSOC_H
#ifndef TESTSOC_H
#define TESTSOC_H
#include <QTimer>
#include <QTcpSocket>
#include <QDebug>
class testSoc: public QObject {
Q_OBJECT
int state;
QTcpSocket *soc;
QTimer *timeout;
public:
explicit testSoc(QObject *p = nullptr);
~testSoc();
void test();
private slots:
void connected();
void handleError(QAbstractSocket::SocketError socketError);
void handleTimeout();
private:
void nextState();
};
#endif // TESTSOC_H
To copy to clipboard, switch view to plain text mode
#include "testSoc.h"
#define VALID_HOST "www.google.com"
#define VALID_PORT 80
#define INVALID_HOST "localhost"
#define INVALID_PORT 81
#define TIMEOUT 3000
state(0),
soc(nullptr),
timeout(nullptr)
{
connect (soc, SIGNAL(connected()), this, SLOT(connected()));
timeout->setSingleShot(true);
timeout->setInterval(TIMEOUT);
connect (timeout, SIGNAL(timeout()), this, SLOT(handleTimeout()));
}
testSoc::~testSoc() {
}
void testSoc::test()
{
switch (state)
{
case 0:
case 2:
case 4:
qDebug () << "State" << state << "Connecting to a valid host. Should be Ok";
timeout->start();
soc->connectToHost(VALID_HOST, VALID_PORT);
break;
case 1:
qDebug () << "State" << state << "Connecting to a invalid port. Should fail";
timeout->start();
soc->connectToHost(VALID_HOST, INVALID_PORT);
break;
case 3:
qDebug () << "State" << state << "Connecting to a invalid host. Should fail";
timeout->start();
soc->connectToHost(INVALID_HOST, VALID_PORT);
break;
}
}
void testSoc::connected() {
timeout->stop();
qDebug() << "State" << state << "Connected to" << soc->peerName() << "port" << soc->peerPort();
// do some work here
soc->disconnectFromHost();
nextState();
}
timeout->stop();
qDebug() << "State" << state << "Error reported" << socketError;
nextState();
}
void testSoc::handleTimeout()
{
timeout->stop();
qDebug () << "State" << state << "Connection time out";
// Using disconnectFromHost() on a host that was never connected
// just causes a long pause for some internal timeout.
soc->abort();
nextState();
}
void testSoc::nextState()
{
state = (state + 1) % 5;
test();
}
#include "testSoc.h"
#define VALID_HOST "www.google.com"
#define VALID_PORT 80
#define INVALID_HOST "localhost"
#define INVALID_PORT 81
#define TIMEOUT 3000
testSoc::testSoc(QObject *p):
QObject(p),
state(0),
soc(nullptr),
timeout(nullptr)
{
soc = new QTcpSocket(this);
connect (soc, SIGNAL(connected()), this, SLOT(connected()));
connect (soc, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(handleError(QAbstractSocket::SocketError)));
timeout = new QTimer(this);
timeout->setSingleShot(true);
timeout->setInterval(TIMEOUT);
connect (timeout, SIGNAL(timeout()), this, SLOT(handleTimeout()));
}
testSoc::~testSoc() {
}
void testSoc::test()
{
switch (state)
{
case 0:
case 2:
case 4:
qDebug () << "State" << state << "Connecting to a valid host. Should be Ok";
timeout->start();
soc->connectToHost(VALID_HOST, VALID_PORT);
break;
case 1:
qDebug () << "State" << state << "Connecting to a invalid port. Should fail";
timeout->start();
soc->connectToHost(VALID_HOST, INVALID_PORT);
break;
case 3:
qDebug () << "State" << state << "Connecting to a invalid host. Should fail";
timeout->start();
soc->connectToHost(INVALID_HOST, VALID_PORT);
break;
}
}
void testSoc::connected() {
timeout->stop();
qDebug() << "State" << state << "Connected to" << soc->peerName() << "port" << soc->peerPort();
// do some work here
soc->disconnectFromHost();
nextState();
}
void testSoc::handleError(QAbstractSocket::SocketError socketError) {
timeout->stop();
qDebug() << "State" << state << "Error reported" << socketError;
nextState();
}
void testSoc::handleTimeout()
{
timeout->stop();
qDebug () << "State" << state << "Connection time out";
// Using disconnectFromHost() on a host that was never connected
// just causes a long pause for some internal timeout.
soc->abort();
nextState();
}
void testSoc::nextState()
{
state = (state + 1) % 5;
test();
}
To copy to clipboard, switch view to plain text mode
#include <QApplication>
#include "testSoc.h"
int main(int argc, char *argv[])
{
testSoc t;
t.test();
return a.exec();
}
#include <QApplication>
#include "testSoc.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
testSoc t;
t.test();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks