[solved] "Connection Refused" error when TcpServer is moved into the mainwindow class
-
wrote on 16 Jan 2014, 02:50 last edited by
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
{
Q_OBJECTpublic:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();private slots:
void on_quitButton_clicked();void on_pbFiles_clicked();
private:
void setUpListener();
Ui::MainWindow *ui;
cprServer server;
};#ifndef CPRSERVER_H
#define CPRSERVER_H#include <QStringList>
#include <QTcpServer>class cprServer : public QTcpServer
{
Q_OBJECTpublic:
cprServer(QObject *parent = 0);protected:
void incomingConnection(qintptr socketDescriptor);private:
QStringList datahandler;
};#endif // CPRSERVER_H
#ifndef LISTENWRITETHREAD_H
#define LISTENWRITETHREAD_H
#include <QThread>
#include <QTcpSocket>class listenWriteThread : public QThread
{
Q_OBJECTpublic:
listenWriteThread(int socketDescriptor, const QString &fortune, QObject *parent);void run();
public slots:
void analyzeThis();
signals:
void error(QTcpSocket::SocketError socketError);private:
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.
Thanks! -
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.
-
wrote on 16 Jan 2014, 04:39 last edited by
Hi,
This is my MainWindow Code:
@#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "cprserver.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);//connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
}
MainWindow::~MainWindow()
{
delete ui;
}void MainWindow::on_quitButton_clicked()
{
close();
}void MainWindow::on_pbFiles_clicked()
{
// setUpListener();
const QString IP = "127.0.0.1";
const ushort port = 8100;
QHostAddress host;
cprServer server;
host.setAddress(IP);
if (!server.listen(host,port)) {
QMessageBox::critical(this, tr("CPR Server"),
tr("Unable to start the server: %1.")
.arg(server.errorString()));
close();
//qDebug() << "Unable to start server" << server.errorString();} QString ipAddress=(server.serverAddress()).toString(); qDebug () << "Server started at " << ipAddress ; qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
}@
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()));
thread->start();
}
#include "listenwritethread.h"#include <QtNetwork>
listenWriteThread::listenWriteThread(int socketDescriptor, const QString &fortune, QObject *parent)
: QThread(parent), socketDescriptor(socketDescriptor), text(fortune)
{
tcpSocket = new QTcpSocket;
tcpSocket->setSocketDescriptor(socketDescriptor);}
void listenWriteThread::run()
{
//qDebug()<<"Inside RUN!";
/if (!tcpSocket->setSocketDescriptor(socketDescriptor)) {
emit error(tcpSocket->error());
return;
}
else qDebug()<<"Ready to Roll";/
//qDebug()<<"Connection Up!";
connect(tcpSocket,SIGNAL(readyRead()),this,SLOT(analyzeThis()));
}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;
}
else
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.
Thanks!
-
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.
-
wrote on 16 Jan 2014, 06:09 last edited by
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!
1/5