QTCP run 2 client on main thread, for 2 server, separate QAbstractSocket signal
-
Sorry at all but quite new in soket, so before discower the weel, i prefer ask if is possible run 2 different client on same mainwindows or same thared .... server is different and work on different ip adress .... but now when start one client other close because disconnected signal .... when my code is write for persistent connection ..... seems QAbstractSocket signal was valid for one client and for other too at same time .... so think problem was separate QAbstractSocket signal about error and state for better management of void.
ANy suggestion? is possible to reach these things 2 client os same thread?
-
Sorry at all but quite new in soket, so before discower the weel, i prefer ask if is possible run 2 different client on same mainwindows or same thared .... server is different and work on different ip adress .... but now when start one client other close because disconnected signal .... when my code is write for persistent connection ..... seems QAbstractSocket signal was valid for one client and for other too at same time .... so think problem was separate QAbstractSocket signal about error and state for better management of void.
ANy suggestion? is possible to reach these things 2 client os same thread?
@gfxx said in QTCP run 2 client on main thread, for 2 server, separate QAbstractSocket signal:
i prefer ask if is possible run 2 different client on same mainwindows or same thared
Yes, it is.
"but now when start one client other close because disconnected signal" - please show your code. Also, does the server support more than one connection at the same time?
-
@gfxx said in QTCP run 2 client on main thread, for 2 server, separate QAbstractSocket signal:
i prefer ask if is possible run 2 different client on same mainwindows or same thared
Yes, it is.
"but now when start one client other close because disconnected signal" - please show your code. Also, does the server support more than one connection at the same time?
@jsulm quite simple .... server is done by qt example .... and client is these:
/* on mainwindows.h */ private: QTcpSocket *tcpSocket; QDataStream in; QTcpSocket *tcpSocket1; QDataStream in1; /* on mainwindows.h end */ /* on mainwindows */ tcpSocket = new QTcpSocket(this); in.setDevice(tcpSocket); in.setVersion(QDataStream::Qt_5_15); tcpSocket->connectToHost("19x.16x.50.xxx", xx050); //(ipHost, ipPort.toShort()); //ipPort.toShort()); if (tcpSocket->waitForConnected()){ qDebug("Connected!"); } connect(tcpSocket,&QAbstractSocket::disconnected,[]()->void{qDebug("Disconnected");}); connect(tcpSocket, &QIODevice::readyRead, this, &MainWindow::readFortune); connect(tcpSocket, &QAbstractSocket::stateChanged, this, &MainWindow::sokState); connect(tcpSocket, &QAbstractSocket::errorOccurred, this, &MainWindow::displayError); /* end */ void MainWindow::startSokconnection1(){ /* a timer singleshot call startSokconnection1 after 4 sec of tcpSocket was in connected state */ tcpSocket1 = new QTcpSocket(); in1.setDevice(tcpSocket1); in1.setVersion(QDataStream::Qt_5_15); tcpSocket1->connectToHost("19x.16x.51.xxx", xx051); //(ipHost, ipPort.toShort()); //ipPort.toShort()); if (tcpSocket1->waitForConnected()){ qDebug("Connected -1-"); } connect(tcpSocket1,&QAbstractSocket::disconnected,[]()->void{qDebug("Disconnected -1-");}); connect(tcpSocket1, &QIODevice::readyRead, this, &MainWindow::readFortune1); connect(tcpSocket1, &QAbstractSocket::stateChanged, this, &MainWindow::sokState1); connect(tcpSocket1, &QAbstractSocket::errorOccurred, this, &MainWindow::displayError1); }when start tcpSocket1 tcpSocket go to Unconnected state ...... readFortune1 is never call actually .... readFortune is working when tcpSocket is connected ...
-
@jsulm quite simple .... server is done by qt example .... and client is these:
/* on mainwindows.h */ private: QTcpSocket *tcpSocket; QDataStream in; QTcpSocket *tcpSocket1; QDataStream in1; /* on mainwindows.h end */ /* on mainwindows */ tcpSocket = new QTcpSocket(this); in.setDevice(tcpSocket); in.setVersion(QDataStream::Qt_5_15); tcpSocket->connectToHost("19x.16x.50.xxx", xx050); //(ipHost, ipPort.toShort()); //ipPort.toShort()); if (tcpSocket->waitForConnected()){ qDebug("Connected!"); } connect(tcpSocket,&QAbstractSocket::disconnected,[]()->void{qDebug("Disconnected");}); connect(tcpSocket, &QIODevice::readyRead, this, &MainWindow::readFortune); connect(tcpSocket, &QAbstractSocket::stateChanged, this, &MainWindow::sokState); connect(tcpSocket, &QAbstractSocket::errorOccurred, this, &MainWindow::displayError); /* end */ void MainWindow::startSokconnection1(){ /* a timer singleshot call startSokconnection1 after 4 sec of tcpSocket was in connected state */ tcpSocket1 = new QTcpSocket(); in1.setDevice(tcpSocket1); in1.setVersion(QDataStream::Qt_5_15); tcpSocket1->connectToHost("19x.16x.51.xxx", xx051); //(ipHost, ipPort.toShort()); //ipPort.toShort()); if (tcpSocket1->waitForConnected()){ qDebug("Connected -1-"); } connect(tcpSocket1,&QAbstractSocket::disconnected,[]()->void{qDebug("Disconnected -1-");}); connect(tcpSocket1, &QIODevice::readyRead, this, &MainWindow::readFortune1); connect(tcpSocket1, &QAbstractSocket::stateChanged, this, &MainWindow::sokState1); connect(tcpSocket1, &QAbstractSocket::errorOccurred, this, &MainWindow::displayError1); }when start tcpSocket1 tcpSocket go to Unconnected state ...... readFortune1 is never call actually .... readFortune is working when tcpSocket is connected ...
@gfxx
If you are under Linux where you have the telnet command, or if you have something similar under Windows, from separate shell/terminal instances set off these two commands:telnet 19x.16x.50.xxx xx050 telnet 19x.16x.51.xxx xx051Don't worry that that you cannot "interact" with each one. Hopefully after the first one the telnet stays connected. Does it get disconnected after the second one? If so this is server behaviour, for whatever reason, nothing to do with Qt client. I note that in addition to the port numbers being different your IP addresses are also different....
EDIT This was posted before @jsulm's reply below, which doubtless will turn out to be the actual issue....
-
@jsulm quite simple .... server is done by qt example .... and client is these:
/* on mainwindows.h */ private: QTcpSocket *tcpSocket; QDataStream in; QTcpSocket *tcpSocket1; QDataStream in1; /* on mainwindows.h end */ /* on mainwindows */ tcpSocket = new QTcpSocket(this); in.setDevice(tcpSocket); in.setVersion(QDataStream::Qt_5_15); tcpSocket->connectToHost("19x.16x.50.xxx", xx050); //(ipHost, ipPort.toShort()); //ipPort.toShort()); if (tcpSocket->waitForConnected()){ qDebug("Connected!"); } connect(tcpSocket,&QAbstractSocket::disconnected,[]()->void{qDebug("Disconnected");}); connect(tcpSocket, &QIODevice::readyRead, this, &MainWindow::readFortune); connect(tcpSocket, &QAbstractSocket::stateChanged, this, &MainWindow::sokState); connect(tcpSocket, &QAbstractSocket::errorOccurred, this, &MainWindow::displayError); /* end */ void MainWindow::startSokconnection1(){ /* a timer singleshot call startSokconnection1 after 4 sec of tcpSocket was in connected state */ tcpSocket1 = new QTcpSocket(); in1.setDevice(tcpSocket1); in1.setVersion(QDataStream::Qt_5_15); tcpSocket1->connectToHost("19x.16x.51.xxx", xx051); //(ipHost, ipPort.toShort()); //ipPort.toShort()); if (tcpSocket1->waitForConnected()){ qDebug("Connected -1-"); } connect(tcpSocket1,&QAbstractSocket::disconnected,[]()->void{qDebug("Disconnected -1-");}); connect(tcpSocket1, &QIODevice::readyRead, this, &MainWindow::readFortune1); connect(tcpSocket1, &QAbstractSocket::stateChanged, this, &MainWindow::sokState1); connect(tcpSocket1, &QAbstractSocket::errorOccurred, this, &MainWindow::displayError1); }when start tcpSocket1 tcpSocket go to Unconnected state ...... readFortune1 is never call actually .... readFortune is working when tcpSocket is connected ...
@gfxx said in QTCP run 2 client on main thread, for 2 server, separate QAbstractSocket signal:
in1.setDevice(tcpSocket);
This is wrong
-
@gfxx said in QTCP run 2 client on main thread, for 2 server, separate QAbstractSocket signal:
in1.setDevice(tcpSocket);
This is wrong
-
@gfxx
If you are under Linux where you have the telnet command, or if you have something similar under Windows, from separate shell/terminal instances set off these two commands:telnet 19x.16x.50.xxx xx050 telnet 19x.16x.51.xxx xx051Don't worry that that you cannot "interact" with each one. Hopefully after the first one the telnet stays connected. Does it get disconnected after the second one? If so this is server behaviour, for whatever reason, nothing to do with Qt client. I note that in addition to the port numbers being different your IP addresses are also different....
EDIT This was posted before @jsulm's reply below, which doubtless will turn out to be the actual issue....
@JonB telnet command work ....
my situation about server .... and my app
-
SERVER 1 192.168.50.xxx -> TCP server (pc1)
-
SERVER 2 192.168.51.xxx -> TCP server 1 (pc2)
-
MYapp -> 2 client (pc3)
-
MYapp client1 on mainwindows previously report an error was:
/* on mainwindows */ tcpSocket = new QTcpSocket(this); // <----- in.setDevice(tcpSocket); in.setVersion(QDataStream::Qt_5_15);- MYapp client2 in in call whithout (this) ......
I correct my previous code in previous post ..... but telnet work and there are not disconnection from server .... but I work in 2 different terminal not in same mainwindows app ....
-
-
@JonB telnet command work ....
my situation about server .... and my app
-
SERVER 1 192.168.50.xxx -> TCP server (pc1)
-
SERVER 2 192.168.51.xxx -> TCP server 1 (pc2)
-
MYapp -> 2 client (pc3)
-
MYapp client1 on mainwindows previously report an error was:
/* on mainwindows */ tcpSocket = new QTcpSocket(this); // <----- in.setDevice(tcpSocket); in.setVersion(QDataStream::Qt_5_15);- MYapp client2 in in call whithout (this) ......
I correct my previous code in previous post ..... but telnet work and there are not disconnection from server .... but I work in 2 different terminal not in same mainwindows app ....
@gfxx There must be still something wrong with your code. Using two QTcpSocket instances to connect to two different servers at the same time works for sure. Can you post a minimal compilable application which shows this behaviour?
-
-
@gfxx There must be still something wrong with your code. Using two QTcpSocket instances to connect to two different servers at the same time works for sure. Can you post a minimal compilable application which shows this behaviour?
@jsulm for first thanks a lot about these confirmation .... I not sure about ....
second because not sure I simplified my real question posted here:
https://forum.qt.io/topic/141619/qtcp-client-at-same-time-of-qtopcua-client /* I know I'm a bad person at all */Actually I try start i real simply working compilable example .... think in one hour
-
@jsulm for first thanks a lot about these confirmation .... I not sure about ....
second because not sure I simplified my real question posted here:
https://forum.qt.io/topic/141619/qtcp-client-at-same-time-of-qtopcua-client /* I know I'm a bad person at all */Actually I try start i real simply working compilable example .... think in one hour
ok .... i made a new app with soket only because problem is in that part ...... these not work at all ....
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QtWidgets> #include <QtNetwork/QNetworkAccessManager> #include <QtNetwork> #include <QKeyEvent> #include <QInputDialog> #include <QDate> #include <QDateTime> #include <QTcpSocket> #include <QAbstractSocket> #include <QDataStream> QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } QT_END_NAMESPACE class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); public slots: void startSokconnection(); private: Ui::MainWindow *ui; QTcpSocket *tcpSocket; QDataStream in; };#include "mainwindow.h" #include "ui_mainwindow.h" bool firstb = true; MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); tcpSocket = new QTcpSocket(); in.setDevice(tcpSocket); in.setVersion(QDataStream::Qt_5_15); connect(ui->pb, SIGNAL(released()), this, SLOT(startSokconnection())); connect(tcpSocket,&QAbstractSocket::disconnected,[]()->void{qDebug("Disconnected");}); connect(tcpSocket,&QAbstractSocket::connected,[]()->void{qDebug("CONNECTED");}); } MainWindow::~MainWindow() { delete ui; } void MainWindow::startSokconnection(){ tcpSocket->abort(); tcpSocket->connectToHost("192.168.50.xxx", 50xxx); //(ipHost, ipPort.toShort()); //ipPort.toShort()); if (tcpSocket->waitForConnected()){ qDebug("Connected!"); firstb = false; } }no error at all but nothing connect or disconnect messages .... and server seems not see client
What I loose?
-
ok .... i made a new app with soket only because problem is in that part ...... these not work at all ....
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QtWidgets> #include <QtNetwork/QNetworkAccessManager> #include <QtNetwork> #include <QKeyEvent> #include <QInputDialog> #include <QDate> #include <QDateTime> #include <QTcpSocket> #include <QAbstractSocket> #include <QDataStream> QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } QT_END_NAMESPACE class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); public slots: void startSokconnection(); private: Ui::MainWindow *ui; QTcpSocket *tcpSocket; QDataStream in; };#include "mainwindow.h" #include "ui_mainwindow.h" bool firstb = true; MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); tcpSocket = new QTcpSocket(); in.setDevice(tcpSocket); in.setVersion(QDataStream::Qt_5_15); connect(ui->pb, SIGNAL(released()), this, SLOT(startSokconnection())); connect(tcpSocket,&QAbstractSocket::disconnected,[]()->void{qDebug("Disconnected");}); connect(tcpSocket,&QAbstractSocket::connected,[]()->void{qDebug("CONNECTED");}); } MainWindow::~MainWindow() { delete ui; } void MainWindow::startSokconnection(){ tcpSocket->abort(); tcpSocket->connectToHost("192.168.50.xxx", 50xxx); //(ipHost, ipPort.toShort()); //ipPort.toShort()); if (tcpSocket->waitForConnected()){ qDebug("Connected!"); firstb = false; } }no error at all but nothing connect or disconnect messages .... and server seems not see client
What I loose?
@gfxx said in QTCP run 2 client on main thread, for 2 server, separate QAbstractSocket signal:
if (tcpSocket->waitForConnected()){ qDebug("Connected!"); firstb = false; }because no msec time into waitForConnected macro .... abstractSocket switch immediately on unconnected state .... old part of my app containing a relais to call immediately reconnection id unconnected stare is reached .... so I see all that problem ....
Insert 3000ms and see al wheels .... thanks to all for support.
-
ok .... i made a new app with soket only because problem is in that part ...... these not work at all ....
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QtWidgets> #include <QtNetwork/QNetworkAccessManager> #include <QtNetwork> #include <QKeyEvent> #include <QInputDialog> #include <QDate> #include <QDateTime> #include <QTcpSocket> #include <QAbstractSocket> #include <QDataStream> QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } QT_END_NAMESPACE class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); public slots: void startSokconnection(); private: Ui::MainWindow *ui; QTcpSocket *tcpSocket; QDataStream in; };#include "mainwindow.h" #include "ui_mainwindow.h" bool firstb = true; MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); tcpSocket = new QTcpSocket(); in.setDevice(tcpSocket); in.setVersion(QDataStream::Qt_5_15); connect(ui->pb, SIGNAL(released()), this, SLOT(startSokconnection())); connect(tcpSocket,&QAbstractSocket::disconnected,[]()->void{qDebug("Disconnected");}); connect(tcpSocket,&QAbstractSocket::connected,[]()->void{qDebug("CONNECTED");}); } MainWindow::~MainWindow() { delete ui; } void MainWindow::startSokconnection(){ tcpSocket->abort(); tcpSocket->connectToHost("192.168.50.xxx", 50xxx); //(ipHost, ipPort.toShort()); //ipPort.toShort()); if (tcpSocket->waitForConnected()){ qDebug("Connected!"); firstb = false; } }no error at all but nothing connect or disconnect messages .... and server seems not see client
What I loose?
@gfxx said in QTCP run 2 client on main thread, for 2 server, separate QAbstractSocket signal:
connect(ui->pb, SIGNAL(released()), this, SLOT(startSokconnection()));
What is ui->pb? And did you make sure startSokconnection is called?
-
@gfxx said in QTCP run 2 client on main thread, for 2 server, separate QAbstractSocket signal:
connect(ui->pb, SIGNAL(released()), this, SLOT(startSokconnection()));
What is ui->pb? And did you make sure startSokconnection is called?