'Ghost' QTcpSockets show up in QTcpServer
-
Hi all!
In my application I have a server based on the functionality of QTcpServer and i client based on QTcpSocket. The socket continusly tries to reconnect to the server when the connection is lost, and i works fine. The problem I am experiencing is that every once in a while when i shut down the server (and let the client try to reconnect indefenetly) and the start the server back up the server gets one or several incomingConnections that when created as QTcpSockets disconnect immidiatly. This doesnt happen every time, but often enough for it to be a problem. Can anyone please help me figure this one out? I am kind of lingering towards that this might be a Qt-bug :SCode for testing this as follows: (sorry for the long post)
@
// theserver.h
#ifndef THESERVER
#define THESERVER
#include <QTcpServer>
class ServerImpl;
class TheServer : public QObject
{
Q_OBJECTpublic:
TheServer(QObject *pParent);
~TheServer() { Stop(); }
void Start();
void Stop();protected slots:
void Started();
void Stopped();private:
ServerImpl *m_pServerImpl;
};
#endif
@@
// theserver.cpp
#include "theserver.h"
#include <QTcpSocket>
#include <QThread>
#include <QDebug>
class SocketHandler : QObject
{
Q_OBJECTpublic:
SocketHandler(int socketDesc) : QObject(0), m_pSocket(0), m_iSocketDesc(socketDesc)
{
QThread *pThread = thread();
pThread = new QThread();
connect(pThread, SIGNAL(started()), this, SLOT(Started()));
connect(pThread, SIGNAL(finished()), this, SLOT(Stopped()));
moveToThread(pThread);
}
void Start(){ thread()->start(); }
void Stop()
{
thread()->quit();
if(thread() != QThread::currentThread())
thread()->wait();
}protected slots:
void printSocketDisconnected() { qDebug() << QString("Socket(%1) disconnected!").arg(m_pSocket->objectName()); }
void Started()
{
m_pSocket = new QTcpSocket(this);
m_pSocket->setObjectName(QString::number(m_iSocketDesc));
connect(m_pSocket, SIGNAL(disconnected()), this, SLOT(printSocketDisconnected()));if(!(m_pSocket->setSocketDescriptor(m_iSocketDesc) && m_pSocket->waitForConnected())) { thread()->quit(); return; } } void Stopped() { qDebug() << "Deleting socket"; delete m_pSocket; }
private:
QTcpSocket *m_pSocket;
int m_iSocketDesc;
};class ServerImpl : public QTcpServer
{
Q_OBJECT
public:
ServerImpl(QObject *pParent) : QTcpServer(pParent), m_pHandler(0)
{
qDebug() << "Listening to " << "127.0.0.1" << " on port:" << "3030";
listen(QHostAddress("127.0.0.1"), 3030);
}
~ServerImpl() { m_pHandler->Stop(); delete m_pHandler; }
protected:
void incomingConnection(int socketDescriptor)
{
qDebug() << "Incomming connection desc:" << socketDescriptor;
m_pHandler = new SocketHandler(socketDescriptor);
m_pHandler->Start();
}
private:
SocketHandler *m_pHandler;
};
TheServer::TheServer(QObject *pParent) : QObject(pParent), m_pServerImpl(0)
{
QThread *pThread = thread();
pThread = new QThread();
connect(pThread, SIGNAL(started()), this, SLOT(Started()));
connect(pThread, SIGNAL(finished()), this, SLOT(Stopped()));
moveToThread(pThread);
}
void TheServer::Start() { thread()->start(); }
void TheServer::Stop()
{
thread()->quit();
if(thread() != QThread::currentThread())
thread()->wait();
}
void TheServer::Started()
{
qDebug() << "The server started!";
m_pServerImpl = new ServerImpl(this);
}
void TheServer::Stopped()
{
qDebug() << "The server is stopping...";
m_pServerImpl->close();
delete m_pServerImpl;
}
#include "theserver.moc"
@@
// ghostclient.h
#ifndef GHOSTCLIENT_H
#define GHOSTCLIENT_H
#include <QObject>
#include <QTcpSocket>
#include <QTimer>
class ghostClient : public QObject
{
Q_OBJECT
public:
ghostClient(QObject *parent = 0);
~ghostClient() {}
protected slots:
void reconnect();
void connectSocket();
void displayState(QAbstractSocket::SocketState socketState);
private:
QTcpSocket *m_pTcpSocket;
QTimer *m_pReconnectTimer;
};
#endif // GHOSTCLIENT_H
@@
// ghostclient.cpp
#include "ghostclient.h"
#include <QDebug>
static QString stateToString(const QAbstractSocket::SocketState state)
{
switch(state)
{
case QAbstractSocket::UnconnectedState:
return "UnconnectedState";
case QAbstractSocket::HostLookupState:
return "HostLookupState";
case QAbstractSocket::ConnectingState:
return "ConnectingState";
case QAbstractSocket::ConnectedState:
return "ConnectedState";
case QAbstractSocket::BoundState:
return "BoundState";
case QAbstractSocket::ClosingState:
return "ClosingState";
case QAbstractSocket::ListeningState:
default:
return "Undefined";
}
}
ghostClient::ghostClient(QObject *parent) : QObject(parent),
m_pTcpSocket(new QTcpSocket(this)),
m_pReconnectTimer(new QTimer(this))
{
connect(m_pTcpSocket, SIGNAL(disconnected()), this, SLOT(reconnect()));
connect(m_pTcpSocket, SIGNAL(stateChanged(QAbstractSocket::SocketState)), this, SLOT(displayState(QAbstractSocket::SocketState)));
connect(m_pReconnectTimer, SIGNAL(timeout()), this, SLOT(connectSocket()));
m_pReconnectTimer->setInterval(500);
m_pReconnectTimer->start();
}
void ghostClient::displayState(QAbstractSocket::SocketState socketState)
{
qDebug() << "The state is:" << stateToString(socketState);
}
void ghostClient::reconnect()
{
if(!m_pReconnectTimer->isActive())
m_pReconnectTimer->start();
}
void ghostClient::connectSocket()
{
m_pTcpSocket->connectToHost("127.0.0.1", 3030);
m_pTcpSocket->waitForConnected(500);if(m_pTcpSocket->state() == QAbstractSocket::ConnectedState)
m_pReconnectTimer->stop();
}
@ -
first of all, i highly recommend you take a vision from Fortune examples for your code as i see your code isn't clean enough
http://qt-project.org/doc/qt-4.8/network-fortuneclient.html
http://qt-project.org/doc/qt-4.8/network-fortuneserver.htmlthen, for your problem it's completely normal. you use a timer to repeatedly call to connect to the host. QAbstractSocket::stateChanged is quite enough to recall your reconnect() function if socketState was in UnconnectedState mode.