Pass QCoreApplication in parameter - c++

I'm tring to build a client for a web service. My goal is to send a request to my server every second. I used this library to help me : QHttp
I create a timer that I link with a signal to my QCoreApplication app, and send my request when the timer reach 1 second.
Here's how I do it :
main.cpp
#include "request.h"
int main(int argc, char** argv) {
QCoreApplication app(argc, argv);
Request* request = new Request();
request->sendRequestPeriodically(1000, app);
return app.exec();
}
request.h
//lots of include before
class Request
{
Q_OBJECT
public:
Request();
void sendRequestPeriodically (int time, QCoreApplication app);
public slots:
void sendRequest (QCoreApplication app);
};
request.cpp
#include "request.h"
void Request::sendRequest (QCoreApplication app){
using namespace qhttp::client;
QHttpClient client(&app);
QUrl server("http://127.0.0.1:8080/?Clearance");
client.request(qhttp::EHTTP_GET, server, [](QHttpResponse* res) {
// response handler, called when the incoming HTTP headers are ready
// gather HTTP response data (HTTP body)
res->collectData();
// when all data in HTTP response have been read:
res->onEnd([res]() {
// print the XML body of the response
qDebug("\nreceived %d bytes of http body:\n%s\n",
res->collectedData().size(),
res->collectedData().constData()
);
// done! now quit the application
//qApp->quit();
});
});
// set a timeout for the http connection
client.setConnectingTimeOut(10000, []{
qDebug("connecting to HTTP server timed out!");
qApp->quit();
});
}
void Request::sendRequestPeriodically(int time, QCoreApplication app){
QTimer *timer = new QTimer(this);
QObject::connect(timer, SIGNAL(timeout()), this, SLOT(sendRequest(app)));
timer->start(time); //time specified in ms
}
Request::Request()
{
}
I got these errors :
C:\Qt\5.7\mingw53_32\include\QtCore\qcoreapplication.h:211: erreur : 'QCoreApplication::QCoreApplication(const QCoreApplication&)' is private
Q_DISABLE_COPY(QCoreApplication)
C:\Users\ebelloei\Documents\qhttp\example\client-aircraft\main.cpp:7: erreur : use of deleted function 'QCoreApplication::QCoreApplication(const QCoreApplication&)'
I'm new to Qt, but I assume this comes from the fact I can't passe my QCoreApplication in parameters, is this right ?

First of all, if you need to access the global application instance, you shouldn't be passing it around. Use the qApp macro, or QCoreApplication::instance().
But that's besides the point: the QHttpClient client instance is a local variable, its lifetime managed by the compiler. There's no point to giving it a parent. The client gets destroyed right as sendRequest exits: your sendRequest is effectively a no-op because of that.
You also have errors in your connect statements: you can't pass argument values using the old style connect syntax:
// Wrong: the connection fails and does nothing.
connect(timer, SIGNAL(timeout()), this, SLOT(sendRequest(foo)));
// Qt 5
connect(timer, &QTimer::timeout, this, [=]{ sendRequest(foo); });
// Qt 4
this->foo = foo;
connect(timer, SIGNAL(timeout()), this, SLOT(sendRequest()));
// sendRequest would then use this->foo
Ideally, you should redesign your code to use QNetworkAccessManager. If not, then you must keep the QHttpClient instance alive within main:
int main(int argc, char** argv) {
QCoreApplication app(argc, argv);
qhttp::client::QHttpClient client;
auto request = new Request(&client);
request->sendRequestPeriodically(1000);
return app.exec();
}
And then:
class Request : public QObject
{
Q_OBJECT
QTimer m_timer{this};
qhttp::client::QHttpClient *m_client;
public:
explicit Request(qhttp::client::QHttpClient *client);
void sendRequestPeriodically(int time);
void sendRequest();
};
Request::Request(qhttp::client::QHttpClient *client) :
QObject{client},
m_client{client}
{
QObject::connect(&m_timer, &QTimer::timeout, this, &Request::sendRequest);
}
void Request::sendRequestPeriodically(int time) {
timer->start(time);
}
void Request::sendRequest() {
QUrl server("http://127.0.0.1:8080/?Clearance");
m_client->request(qhttp::EHTTP_GET, server, [](QHttpResponse* res) {
...
});
}

You can't pass a copy QCoreApplication object via its copy constructor, you have to pass a pointer to QCoreApplication.
All of "QCoreApplication app" should be replaced by "QCoreApplication *app" and calls to those functions must include a pointer to app not a copy of it.
request.h
//lots of include before
class Request
{
Q_OBJECT
public:
Request();
void sendRequestPeriodically (int time, QCoreApplication *app);
public slots:
void sendRequest (QCoreApplication *app);
};
request.cpp
#include "request.h"
void Request::sendRequest (QCoreApplication *app){
using namespace qhttp::client;
QHttpClient client(app);
QUrl server("http://127.0.0.1:8080/?Clearance");
client.request(qhttp::EHTTP_GET, server, [](QHttpResponse* res) {
// response handler, called when the incoming HTTP headers are ready
// gather HTTP response data (HTTP body)
res->collectData();
// when all data in HTTP response have been read:
res->onEnd([res]() {
// print the XML body of the response
qDebug("\nreceived %d bytes of http body:\n%s\n",
res->collectedData().size(),
res->collectedData().constData()
);
// done! now quit the application
//qApp->quit();
});
});
// set a timeout for the http connection
client.setConnectingTimeOut(10000, []{
qDebug("connecting to HTTP server timed out!");
qApp->quit();
});
}
void Request::sendRequestPeriodically(int time, QCoreApplication app){
QTimer *timer = new QTimer(this);
QObject::connect(timer, SIGNAL(timeout()), this, SLOT(sendRequest(app)));
timer->start(time); //time specified in ms
}
Request::Request()
{
}
main.cpp
#include "request.h"
int main(int argc, char** argv) {
QCoreApplication app(argc, argv);
Request* request = new Request();
request->sendRequestPeriodically(1000, &app);
return app.exec();
}
Edit
As KubaOber said, there are more issues in your code, read his answer

Related

Unable to connect using QML Remote Object with Error: "connectionToSource is null"

I am trying to use Qt Remote Objects for creating client server application.
But encountered an issue that says "connectionToSource is null". How do I set the source connection ?
I am creating the server using the .rep file. The code I had used is as follows:
Data Model
// Rep File (DataModel.rep)
class DataModel {
PROP(double data)
}
Server Code
// DataBroadcaster class (DataBroadcaster.h)
// DataBroadcaster class inherits DataModelSimpleSource.h (generated from .rep file)
#include "rep_DataModel_Source.h"
class DataBroadcaster : public DataModelSimpleSource
{
Q_OBJECT
public:
DataBroadcaster(QObject *parent = nullptr);
~DataBroadcaster();
void Initialize();
private:
int QScopedPointer<QRemoteObjectHost> remoteHost;
}
// DataBroadcaster.cpp
DataBroadcaster::DataBroadcaster(QObject *parent): DataModelSimpleSource(parent){}
DataBroadcaster::~DataBroadcaster(){}
void DataBroadcaster::Initialize()
{
remoteHost.reset(new QRemoteObjectHost(QUrl(QStringLiteral("local:mydata"))));
remoteHost->enableRemoting(this, "DataModel");
}
// Server code main function
#include <QCoreApplication>
#include "DataBroadcaster.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
DataBroadcaster server;
server.Initialize();
double count = 0.0;
while(true)
{
server.pushData(count + 0.1);
}
return a.exec();
}
Client Code
// DataModelDataReplica is generated from .rep file used to generate source
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include "rep_DataModel_replica.h"
int main(int argc, char *argv[])
{
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
// registered as qml type in main
qmlRegisterType<DataModelReplica>("CustomData", 1, 0, "MyData");
const QUrl url(QStringLiteral("qrc:/main.qml"));
QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
&app, [url](QObject *obj, const QUrl &objUrl) {
if (!obj && url == objUrl)
QCoreApplication::exit(-1);
}, Qt::QueuedConnection);
engine.load(url);
return app.exec();
}
QML Code
// DisplayPage.qml Added to QML file
import CustomData 1.0
MyData {
id: dataClient
node: Node {
registerUrl: "local:mydata"
}
Button {
id: btn
text: "Server Data"
onClicked: {
// displaying some data from source
btn.text = dataClient.data
}
}
}
Execution:
Server code is running
Client code is running but when trying to get data I get following error message in debugger
**"qt.remoteobjects: connectionToSource is null"**
I am unable to figure out what am I missing.
If anyone has any idea about how to resolve or where to look for please suggest.
I was able to find the problem. In the Server Code main function, the while loop is used which never exits because of which the "exec()" function was never called and Qt event loop never started because of which the Qt Remote object didn't share data. So I changed the following code using the Signal and Slot mechanism to create a non-blocking function. Following is the code change that I made.
// Server Code main function
#include <QCoreApplication>
#include "DataBroadcaster.h"
#include "ServerController.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
DataBroadcaster server;
server.Initialize();
ServerController controller(&server);
return a.exec();
}
// Server Controller
#include <QTimer>
#include "DataBroadcaster.h"
class ServerController : public QObject
{
Q_OBJECT
public:
explicit ServerController(QObject *parent = nullptr, DataBroadcast *server = nullptr);
~ServerController();
public Q_SLOTS:
void SendData();
private:
QTimer timer;
double count = 0.0;
DataBroadcast *m_server = nullptr;
}
ServerController::ServerController(QObject *parent, DataBroadcast *server): QObject(parent), m_server(server)
{
connect(&timer, SIGNAL(timeout()), this, SLOT(SendData()));
timer.start();
}
ServerController::~ServerController()
{
timer.stop();
}
ServerController::SendData()
{
count += 0.1;
if(m_server != nullptr)
{
m_server->pushData(count);
}
}

Qt - MainWindow freezes when running

I'm trying to get large data from a database but when running the main window freezes.
I am working under windows and according to this link Windows automatically set the program to a hanging state state after 5 seconds.
Is there a way to prevent freezing?
Here is the code:
void MainWindow::on_getDataButtonClicked()
{
ui->centralWidget->setEnabled(false);
QApplication::setOverrideCursor(Qt::WaitCursor);
try
{
Client client(user, password);
std::future<map<string, map<string, string> > > fut =
std::async(std::launch::async, &Client::get_data, &client);
// While not all data has been retrieved, set message to the status bar.
while (fut.wait_for(std::chrono::seconds(0)) != std::future_status::ready)
{
ui->statusBar->showMessage("Getting data.");
std::this_thread::sleep_for(std::chrono::milliseconds(500));
ui->statusBar->showMessage("Getting data..");
std::this_thread::sleep_for(std::chrono::milliseconds(500));
ui->statusBar->showMessage("Getting data...");
std::this_thread::sleep_for(std::chrono::milliseconds(500));
}
map<string, map<string, string> > exported_strings = std::move(fut.get());
ui->statusBar->showMessage("%All data has been retrieved!");
}
catch (std::string& s)
{
QMessageBox::critical(this, "Error", QString::fromStdString(s));
}
catch (std::exception& e)
{
QMessageBox::critical(this, "Error", QString(e.what()));
}
catch (...)
{
QMessageBox::critical(this, "Error", "An unknown error has occurred.");
}
ui->centralWidget->setEnabled(true);
QApplication::restoreOverrideCursor();
}
On a side note, the main window does not freezes when debugging.
There's no point to doing asynchronous work if you're waiting for it and blocking the GUI thread anyway in the while loop. You need to get rid of the while loop.
You could use QtConcurrent::run instead of std::async, and use QFutureWatcher to get notified asynchronously, without blocking, when the async task has finished.
// https://github.com/KubaO/stackoverflown/tree/master/questions/async-sane-39396761
#include <QtWidgets>
#include <QtConcurrent>
#include <map>
#include <string>
struct Client {
using result_type = std::map<std::string, std::map<std::string, std::string>>;
result_type get_data() {
QThread::sleep(5); // pretend to do some work
return result_type();
}
};
class MainWindow : public QMainWindow {
Q_OBJECT
Client::result_type exported_strings;
QWidget centralWidget;
QVBoxLayout layout{&centralWidget};
QPushButton getDataButton{"Get Data"};
QStatusBar statusBar;
QTimer statusTimer;
QString statusMessage;
void setBusyStatus(const QString & status) {
centralWidget.setEnabled(false);
QApplication::setOverrideCursor(Qt::WaitCursor);
statusMessage = status;
statusTimer.start(0);
}
void setNormalStatus(const QString & status) {
centralWidget.setEnabled(true);
QApplication::restoreOverrideCursor();
statusBar.showMessage(status);
statusTimer.stop();
}
Q_SLOT void on_getDataButtonClicked();
public:
MainWindow() {
setStatusBar(&statusBar);
setCentralWidget(&centralWidget);
layout.addWidget(&getDataButton);
int n = 0;
connect(&statusTimer, &QTimer::timeout, [=]() mutable {
statusBar.showMessage(QStringLiteral("%1%2").arg(statusMessage).arg(QString{n+1, QChar{'.'}}));
n = (n+1)%3;
statusTimer.start(500);
});
connect(&getDataButton, &QPushButton::clicked, this, &MainWindow::on_getDataButtonClicked);
}
};
void MainWindow::on_getDataButtonClicked()
{
auto future = QtConcurrent::run([=]{
Client client;
return client.get_data();
});
auto watcher = new QFutureWatcher<Client::result_type>{this};
connect(watcher, &QFutureWatcher<Client::result_type>::finished, this, [=]{
exported_strings = std::move(watcher->result());
watcher->deleteLater();
setNormalStatus("All data has been retrieved!");
});
watcher->setFuture(future);
setBusyStatus("Getting data");
}
int main(int argc, char ** argv) {
QApplication app{argc, argv};
MainWindow w;
w.show();
return app.exec();
}
#include "main.moc"
Alternatively, you could emit a signal from the async code, letting you retain the use of std::async if you prefer that:
#include <QtWidgets>
#include <future>
#include <map>
#include <string>
struct Client {
using result_type = std::map<std::string, std::map<std::string, std::string>>;
result_type get_data() {
QThread::sleep(5); // pretend to do some work
return result_type();
}
};
class MainWindow : public QMainWindow {
Q_OBJECT
Client::result_type exported_strings;
QWidget centralWidget;
QVBoxLayout layout{&centralWidget};
QPushButton getDataButton{"Get Data"};
QStatusBar statusBar;
QTimer statusTimer;
QString statusMessage;
std::future<Client::result_type> resultFuture;
void setBusyStatus(const QString & status) {
centralWidget.setEnabled(false);
QApplication::setOverrideCursor(Qt::WaitCursor);
statusMessage = status;
statusTimer.start(0);
}
void setNormalStatus(const QString & status) {
centralWidget.setEnabled(true);
QApplication::restoreOverrideCursor();
statusBar.showMessage(status);
statusTimer.stop();
}
Q_SLOT void on_getDataButtonClicked();
Q_SIGNAL void hasResult();
public:
MainWindow() {
setStatusBar(&statusBar);
setCentralWidget(&centralWidget);
layout.addWidget(&getDataButton);
int n = 0;
connect(&statusTimer, &QTimer::timeout, [=]() mutable {
statusBar.showMessage(QStringLiteral("%1%2").arg(statusMessage).arg(QString{n+1, QChar{'.'}}));
n = (n+1)%3;
statusTimer.start(500);
});
connect(&getDataButton, &QPushButton::clicked, this, &MainWindow::on_getDataButtonClicked);
}
};
void MainWindow::on_getDataButtonClicked()
{
connect(this, &MainWindow::hasResult, this, [this](){
exported_strings = std::move(resultFuture.get());
setNormalStatus("All data has been retrieved!");
}, Qt::UniqueConnection);
resultFuture = std::async(std::launch::async, [this]{
Client client;
auto result = client.get_data();
emit hasResult();
return result;
});
setBusyStatus("Getting data");
}
int main(int argc, char ** argv) {
QApplication app{argc, argv};
MainWindow w;
w.show();
return app.exec();
}
#include "main.moc"
If everything is happening on the same thread and some operation is slow, then you won't get back to the event-loop in time for the OS to not declare you as stuck.
The solution(s) are to either;
1) use multiple processes.
2) use multiple threads.
3) divide the work on your one thread into chunks that are all guaranteed to be small enough that you can get back to servicing the eventloop in a timely fashion.
To avoid multithreading, here's what you should do:
Cut your code (virtually, just in thought) into chunks
Run your program and study these chunks, and see which of them causes the freezing (takes longest to execute)
Once you know what part causes the freezing, use QApplication::processEvents(); to force the MainWindow to become responsive.
Example: Let me illustrate with an example. Say you have this function
void MainWindow::on_getDataButtonClicked()
{
//do some easy stuff
for(long i = 0; i < 100000; i++)
{
//do some stuff
//do some other stuff
}
//do some different stuff
}
Now while executing this nasty function, your window will definitely freeze until the whole function is finished. The "easy stuff" is not the problem, because it's fast. But the big loop is the problem. So all you have to do is tell Qt to reprocess the events that get the main window to become responsive again; Like this:
void MainWindow::on_getDataButtonClicked()
{
//do some easy stuff
for(long i = 0; i < 100000; i++)
{
QApplication::processEvents();
//do some stuff
//do some other stuff
}
QApplication::processEvents();
//do some different stuff
}
How many times should you call processEvents();? Call it whenever you want the window to respond again. If in doubt, just put processEvents() like everywhere! Infest every line with that. It's not the best practice, but then you can remove them one by one and see where your program starts freezing again.
One additional piece of advice: Don't throw std::string exceptions. This is a very bad idea. Learn how to throw classes inherited from std::exception. You can still hold your string inside the std::exception object. For example, you can do: throw std::exception("This is very very bad");. There are many other classes you can throw. Find them here.

Qt - get data and forward them on serial connection

I am trying to develop a simple Qt application.
After I press a "START" button, the application should continuosly retrieves data from a device (using third party libraries) and forward them as soon as possible on a serial connection.
The (ugly) software I used till now was a console application that ran in a sequential way and got data frame as soon as they are made available by the host, using the following cycle:
while(1)
{
[...]
while( MyClient.GetFrame().Result != Result::Success )
{
Sleep( 200 );
std::cout << ".";
}
[... pack and send on serial]
}
I was wondering which is the more convinient way to implement this in Qt, in order to keep the GUI responsive but also with the minimum possible latency between getFrame and the serial write function.
Should I use a timer triggered SLOT? QtConcurrent namespace? QRunnable?
Which are the main advantages and disadvantages of each of these approaches?
Thanks
for your help!
Since the existing library forces you to poll for data, the only thing to do is to run it on a timer. It's your choice as to if the object that does this job will run in the main thread, or in a worker thread. There's no need to use Qt Concurrent nor QRunnable - using a QObject makes life somewhat simpler since you can easily provide feedback to the GUI.
For example, making some assumptions about your client's API:
class Worker : public QObject {
Client m_client;
QSerialPort m_port;
QBasicTimer m_timer;
void processFrame() {
if (m_client.GetFrame().Result != Result::Success) return;
QByteArray frame = QByteArray::fromRawData(
m_client.GetFrame().Data, m_client.GetFrame().Size);
... process the frame
if (m_port.write(frame) != frame.size()) {
... process the error
}
}
void timerEvent(QTimerEvent * ev) {
if (ev->timerId() == m_timer.timerId()) processFrame();
}
public:
Worker(QObject * parent = 0) : QObject(parent) {}
Q_SLOT bool open(const QString & name) {
m_port.close();
m_port.setPortName(name);
if (!m_port.open(name)) return false;
if (!m_port.setBaudRate(9600)) return false;
if (!m_port.setDataBits(QSerialPort::Data8)) return false;
... other settings go here
return true;
}
Q_SLOT void start() { m_timer.start(200, this); }
Q_SLOT void stop() { m_timer.stop(); }
...
}
/// A thread that's always safe to destruct
class Thread : public QThread {
using QThread::run; // lock the default implementation
public:
Thread(QObject * parent = 0) : QThread(parent) {}
~Thread() { quit(); wait(); }
};
int main(int argc, char ** argv) {
QApplication app(argc, argv);
Worker worker;
Thread thread; // worker must be declared before thread!
if (true) {
// this code is optional, disabling it should not change things
// too much unless the client implementation blocks too much
thread.start();
worker.moveToThread(&thread);
}
QPushButton button("Start");
QObject::connect(&button, &QPushButton::clicked, [&worker]{
// Those are cross-thread calls, they can't be done directly
QMetaObject::invoke(&worker, "open", Q_ARG(QString, "COM1");
QMetaObject::invoke(&worker, "start");
});
button.show();
return app.exec(argc, argv);
}

Thread sending http request using Qt

I am trying to create a thread (HttpWorker) that when required wakes up and sends a http request. I would like this to be done in a single thread. I am using Qt for the implementation.
The way I thought i would do it is to have a class MyHttpWorker, move it to another thread, connect the slots/signals etc. Then on thread start I would use QNetworkAccessManager to call get requests. I would use QWaitCondition to pause the thread after the request has been sent and I would resume this thread whenever I need to send another one.
However, when I pause the httpworker thread, the FinishedSlot is not called at all. If I use the class to simply call one http request, it executes with no problem. So the problem is connected to QWaitCondition (or just freezing the threads in general).
I could simply create and destroy one worker and thread for each request I have, but I require to send lot of http requests, so I think this method would be way too consuming (creating threads and destroying them over and over).
I appreciate any help I can get.
Here is my code:
MyHttpWorker.h
#include <QNetworkReply>
#include <QDebug>
#include <QObject>
#include <QNetworkAccessManager>
#include <QThread>
#include <QWaitCondition>
#include <QMutex>
class MyHttpWorker : public QObject
{
Q_OBJECT
QNetworkAccessManager* nam;
QMutex syncPause;
QWaitCondition pauseCond;
public:
explicit MyHttpWorker(QObject *parent = 0);
void MyWake();
public slots:
void SetTheThread(QThread* thread);
void MyStart();
void finishedSlot(QNetworkReply* reply);
};
MyHttpWorker.cpp
MyHttpWorker::MyHttpWorker(QObject *parent) :
QObject(parent)
{
nam = new QNetworkAccessManager(this);
QObject::connect(nam, SIGNAL(finished(QNetworkReply*)), this, SLOT(finishedSlot(QNetworkReply*)));
}
void MyHttpWorker::finishedSlot(QNetworkReply* reply)
{
qDebug() << "Finished"; //This slot is never even reached, when i used QWaitCond...
if (reply->error() == QNetworkReply::NoError)
{
QByteArray bytes = reply->readAll();
QString string(bytes);
qDebug() << string;
}else
{
qDebug() << reply->errorString();
}
reply->deleteLater();
}
void MyHttpWorker::SetTheThread(QThread* thread){
QObject::connect(thread,SIGNAL(started()),this,SLOT(MyStart()));
}
void MyHttpWorker::MyWake(){
pauseCond.wakeAll();
}
void MyHttpWorker::MyStart(){
qDebug() << "Start" ;
while(true){
syncPause.lock();
qDebug() << "thread waiting...";
pauseCond.wait(&syncPause);
qDebug() << "thread resumed.";
syncPause.unlock();
//sending the actual request here
QNetworkRequest myRequest;
myRequest.setUrl(QUrl("http://www.google.com"));
nam->get(myRequest);
}
}
main.cpp
#include <QCoreApplication>
#include <QThread>
#include <QDebug>
#include <myhttpworker.h>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
//create the worker, thread and launch it... (worker is waiting by default)
MyHttpWorker* worker = new MyHttpWorker;
QThread* httpThread = new QThread;
worker->SetTheThread(httpThread);
worker->moveToThread(httpThread);
httpThread->start();
//try and send 5 requests ...
for(int i=0;i<5;i++){
qDebug() << "Unpausing";
QThread::currentThread()->msleep(1000);
worker->MyWake();
}
return a.exec();
}
don't create an infinite loop but let the even loop handle it:
void MyHttpWorker::MyWake()
{
QMetaObject::invokeMethod(this,"doSend");
//send to the event loop
}
// new slot
void MyHttpWorker::doSend(){
//sending the actual request here
QNetworkRequest myRequest;
myRequest.setUrl(QUrl("http://www.google.com"));
nam->get(myRequest);
}
//and remove the myStart and all that synchronisation
then when you want to stop it just send a quit to the thread. I suggest you also connect the finished signal of the thread to the deleteLater slot of MyHttpWorker

Experiencing timeouts after connecting to 20 different servers using QTcpSocket

I've been experiencing a weird problem using QTcpSocket, I've searched the web but can't find anybody else with the same problem.
I have two bare-bones applications, one client and one server, if I run them both on my local machine the client successfully makes all 50 connections to the server.
If I run the server on another computer connected to the network (let's say: 10.1.1.1) it will again connect every time without a problem.
Now, if I change the server's main.cpp to initialise multiple servers under different ports (8001 to 8050), and change the client to make one connection to each server. With both running locally it still works fine.
int main(int argc, char** argv) {
QCoreApplication app(argc, argv);
for (int i = 8001; i <= 8050; ++i) {
Server server(i);
return app.exec();
}
}
But, if I put the server on 10.1.1.1 and try it again then I connect to the first 20 without a problem, but then it hangs for a while (anywhere upwards of 5 seconds) before connecting to the next 20, and so on until all are connected.
As a final test, I put an instance of the server on another machine (10.1.1.2) and created 15 instances of a server on each machine and tried to connect to both I experienced the same problem. All 15 of the first machine connected fine, as did the next 5 of the 2nd machine before it hung until eventually connecting to the last 10.
This is on Qt version 4.7.2. And I've experienced this problem on Fedora 17, Windows 7 but not Scientific Linux 6.
The code for the client/server is included below, I removed all the includes to save some vertical space:
Client
client.h
class Client: public QObject {
Q_OBJECT
public:
Client(QObject* parent = 0);
~Client();
void start(QString address, quint16 port);
public slots:
void startTransfer();
void disconnect() { qDebug("disconnect"); }
private:
QTcpSocket client;
};
client.cpp
Client::Client(QObject* parent): QObject(parent) {
connect(&client, SIGNAL(connected()), this, SLOT(startTransfer()));
connect(&client, SIGNAL(disconnected()), this, SLOT(disconnect()));
}
Client::~Client() {
client.close();
}
void Client::start(QString address, quint16 port) {
QHostAddress addr(address);
qDebug(QString("connecting to %1:%2").arg(address, QString::number(port)).toLocal8Bit().constData());
client.connectToHost(addr, port);
}
void Client::startTransfer() {
qDebug("connected");
client.write("Hello, world", 13);
}
main.cpp
int main(int argc, char** argv) {
QCoreApplication app(argc, argv);
for (int i = 0; i < 50; i++) {
Client *client = new Client;
client->start("192.168.0.1", 8888);
}
return app.exec();
}
Server
server.h
class Server: public QObject {
Q_OBJECT
public:
Server(int port = 8888, QObject * parent = 0);
~Server();
public slots:
void acceptConnection();
void startRead();
private:
QTcpServer server;
QList<QTcpSocket *> clients;
};
server.cpp
Server::Server(int port, QObject* parent): QObject(parent) {
qDebug(qPrintable("new server instance on port " + QString::number(port)));
connect(&server, SIGNAL(newConnection()), this, SLOT(acceptConnection()));
server.listen(QHostAddress::Any, port);
}
Server::~Server() {
server.close();
}
void Server::acceptConnection() {
QTcpSocket *client = server.nextPendingConnection();
clients.append(client);
connect(client, SIGNAL(readyRead()), this, SLOT(startRead()));
}
void Server::startRead() {
QTcpSocket *client = dynamic_cast<QTcpSocket *>(sender());
char buffer[1024] = {0};
client->read(buffer, client->bytesAvailable());
QString response = QString("%2 on server %3").arg(buffer, QString::number(server.serverPort()));
std::cout << qPrintable(response) << std::endl;
client->close();
}
main.cpp
int main(int argc, char** argv) {
QCoreApplication app(argc, argv);
Server server;
return app.exec();
}