Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. 'Ghost' QTcpSockets show up in QTcpServer
Forum Update on Monday, May 27th 2025

'Ghost' QTcpSockets show up in QTcpServer

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 1.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    UlfJohnsson
    wrote on last edited by
    #1

    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 :S

    Code 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_OBJECT

    public:
    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_OBJECT

    public:
    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();
    }
    @

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mohsen
      wrote on last edited by
      #2

      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.html

      then, 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.

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved