[solved] "Connection Refused" error when TcpServer is moved into the mainwindow class

  • Hi,
    I started my threaded server application by first setting up the server in the main class, and setting up the listen port. The listen port launches a thread to handle incoming data. Everything worked perfectly.

    I decided to move the server into the mainwindowui class, since the server needs to be set up only if the user decides to start a training session. Qt side everything sets up correctly but my embedded client complaints "Error Connecting: Connection Refused".

    Is it verboten to launch a thread from the UI? Is that the issue?

    If that is correct and I want to wait to launch the server, do I need to setup a signal-slot connection? Signal says to set up listener, server sets up listener?

    My next step is launching another window where the incoming TCP Data is plotted. How do I connect the socket thread to the plot window?

    @#include <QMainWindow>
    #include <QtWidgets>
    #include "cprServer.h"
    namespace Ui {
    class MainWindow;

    class MainWindow : public QMainWindow

    explicit MainWindow(QWidget *parent = 0);

    private slots:
    void on_quitButton_clicked();

    void on_pbFiles_clicked();

    void setUpListener();
    Ui::MainWindow *ui;
    cprServer server;

    #ifndef CPRSERVER_H
    #define CPRSERVER_H

    #include <QStringList>
    #include <QTcpServer>

    class cprServer : public QTcpServer

    cprServer(QObject *parent = 0);

    void incomingConnection(qintptr socketDescriptor);

    QStringList datahandler;

    #endif // CPRSERVER_H
    #include <QThread>
    #include <QTcpSocket>

    class listenWriteThread : public QThread

    listenWriteThread(int socketDescriptor, const QString &fortune, QObject *parent);

    void run();

    public slots:
    void analyzeThis();
    void error(QTcpSocket::SocketError socketError);

    QTcpSocket* tcpSocket;
    int socketDescriptor;
    QString text;

    I have only added the header files because the functions work, it is just this set up of window calling server that is not working.

  • ConnectionRefused means that your server socket is not started at all. Please check that. Hope your setupListener is called and initialized before you send the request for connection. In summary serverSocket should be ready before any incoming request come. If you give your complete sample I can suggest you what is the issue.

    Also there is no restriction from where you create thread. You can create from wherever you want.

  • Hi,
    This is my MainWindow Code:
    @#include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "cprserver.h"
    MainWindow::MainWindow(QWidget *parent) :
    ui(new Ui::MainWindow)

    //connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));


    delete ui;

    void MainWindow::on_quitButton_clicked()

    void MainWindow::on_pbFiles_clicked()
    // setUpListener();
    const QString IP = "";
    const ushort port = 8100;
    QHostAddress host;
    cprServer server;
    if (!server.listen(host,port)) {
    QMessageBox::critical(this, tr("CPR Server"),
    tr("Unable to start the server: %1.")
    //qDebug() << "Unable to start server" << server.errorString();

    QString ipAddress=(server.serverAddress()).toString();
    qDebug () << "Server started at " << ipAddress ;


    I print the Server Started message to ensure the server was started. And this does happen every time I click on the pbFiles button.

    My server and thread classes:
    @#include "cprserver.h"

    #include "cprserver.h"
    #include "listenwritethread.h"

    #include <stdlib.h>

    cprServer::cprServer(QObject *parent)
    : QTcpServer(parent)

    void cprServer::incomingConnection(qintptr socketDescriptor)
    QString fortune = "One Fine Day!";
    listenWriteThread *thread = new listenWriteThread(socketDescriptor, fortune, this);
    //connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
    #include "listenwritethread.h"

    #include <QtNetwork>

    listenWriteThread::listenWriteThread(int socketDescriptor, const QString &fortune, QObject *parent)
    : QThread(parent), socketDescriptor(socketDescriptor), text(fortune)
    tcpSocket = new QTcpSocket;


    void listenWriteThread::run()
    //qDebug()<<"Inside RUN!";
    /if (!tcpSocket->setSocketDescriptor(socketDescriptor)) {
    emit error(tcpSocket->error());
    else qDebug()<<"Ready to Roll";
    //qDebug()<<"Connection Up!";

    void listenWriteThread::analyzeThis()
    //QTcpSOcket tcpSocket;
    //qDebug()<<"In Analyze!";
    QByteArray data = tcpSocket->readAll();
    QString sData=QString(data);
    QStringList dataList = sData.split(':');
    if ((dataList.size() >= 2) && dataList[0]=="Disp")
    bool ok;
    int val = dataList[3].toInt(&ok,10);
    qDebug() << " Compression Depth " << val;
    qDebug() << "Data Error ! " << data;

    When I ran these classes as part of main, I succesfully connected and received data. The errors started only after I moved server to mainwindow class.


  • cprServer server;

    THis is local stack variable. This will delete once the function is complete. CHange the same to heap variable

    cprServer *server = new     cprServer

    Then work on this.

  • Oh thank you thank you thank you so much!

    I had already made cprServer a private variable in the windows class, but, when I cut and paste code from the main.cpp file I repeated the cprServer declaration and inadvertently made the server a local variable.

    I never considered this error because I only saw cprServer server declared in the mainwindow class. I was absolutely blind to the local declaration in the slot function!

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.