Here is code ilustrating the problem:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QTcpSocket>
#include <QHostAddress>
namespace Ui {
class MainWindow;
}
{
Q_OBJECT
public:
explicit MainWindow
(QWidget *parent
= 0);
~MainWindow();
private:
Ui::MainWindow *ui;
static const int numOfSockets = 50;
int numOfProccessing;
public slots:
void onBtnStart();
};
#endif // MAINWINDOW_H
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QTcpSocket>
#include <QHostAddress>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
static const int numOfSockets = 50;
QTcpSocket sockets[numOfSockets];
QHostAddress hostAddress;
int numOfProccessing;
public slots:
void onBtnStart();
void onError(QAbstractSocket::SocketError);
};
#endif // MAINWINDOW_H
To copy to clipboard, switch view to plain text mode
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->btnStart, SIGNAL(pressed()), this, SLOT(onBtnStart()));
for(int i = 0; i < MainWindow::numOfSockets; i++)
{
}
ui
->btnStart
->setText
(QString("Start(%1)").
arg(MainWindow
::numOfSockets));
hostAddress.setAddress("192.168.0.1");
}
MainWindow::~MainWindow()
{
for(int i = 0; i < MainWindow::numOfSockets; i++)
{
if(sockets[i].isOpen())
{
sockets[i].close();
}
}
delete ui;
}
void MainWindow::onBtnStart()
{
numOfProccessing = 0;
ui->btnStart->setEnabled(false);
for(int i = 0; i < MainWindow::numOfSockets; i++)
{
hostAddress.setAddress(hostAddress.toIPv4Address() + 1);
sockets[i].connectToHost(hostAddress, 2376);
qDebug
() <<
QString("connect to host %1").
arg(hostAddress.
toString());
numOfProccessing++;
}
}
{
ui->btnStart->setEnabled(--numOfProccessing == 0);
qDebug
() <<
QString("MainWindow::onError %1").
arg(e
);
}[ATTACH]6016[/ATTACH]
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->btnStart, SIGNAL(pressed()), this, SLOT(onBtnStart()));
for(int i = 0; i < MainWindow::numOfSockets; i++)
{
connect(&sockets[i], SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(onError(QAbstractSocket::SocketError)));
}
ui->btnStart->setText(QString("Start(%1)").arg(MainWindow::numOfSockets));
hostAddress.setAddress("192.168.0.1");
}
MainWindow::~MainWindow()
{
for(int i = 0; i < MainWindow::numOfSockets; i++)
{
disconnect(&sockets[i], SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(onError(QAbstractSocket::SocketError)));
if(sockets[i].isOpen())
{
sockets[i].close();
}
}
delete ui;
}
void MainWindow::onBtnStart()
{
numOfProccessing = 0;
ui->btnStart->setEnabled(false);
for(int i = 0; i < MainWindow::numOfSockets; i++)
{
hostAddress.setAddress(hostAddress.toIPv4Address() + 1);
sockets[i].connectToHost(hostAddress, 2376);
qDebug() << QString("connect to host %1").arg(hostAddress.toString());
numOfProccessing++;
}
}
void MainWindow::onError(QAbstractSocket::SocketError e)
{
ui->btnStart->setEnabled(--numOfProccessing == 0);
qDebug() << QString("MainWindow::onError %1").arg(e);
}[ATTACH]6016[/ATTACH]
To copy to clipboard, switch view to plain text mode
Added after 1 47 minutes:
It seems that these sockets exist even after app was closed. I used Wireshark to check if there is any traffic after sockets were deleted and it was. Can anybody tell me how to permanently and immediately close these sockets? QTcpSocket::abort() does not the trick.
Bookmarks