Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Working TCP client not connecting after upgrading to Qt creator 4.14.0 (based on Qt 5.15.2)



  • I have a working project which has stopped working after upgrading to Qt 5.15.2. Previously I was using Qt creator 4.8.

    I do not get any error and the program executes normally with GUI coming up and remaining all executions working perfectly except the tcpsocket connection.

    The software is basically a tcp client which receives 1800 bytes every second from a embedded system connected over LAN.

    GUI with code snippet.png```
    code_text
    #include "mainwindow.h"
    #include "ui_mainwindow.h"

    #include <QtNetwork>
    //#include <QObject>

    unsigned int received_count=0;
    unsigned int old_received_count=0;
    unsigned int packet_drop_count=0;

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);

    QString titlestring ="DDCS Network Component Qualification" ;
    setWindowTitle(titlestring);
    
    ui->lineEdit->setText("hi");
    
    tcpSocket=new QTcpSocket(this);
    timer=new QTimer(this);
    
    connect(timer,SIGNAL(timeout()),this,SLOT(client_logic()));
    timer->start(1000);
    

    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    void MainWindow::write_to_file() // created for experimenting text file generation
    {
    QString tst="tst";
    QString tst1="123456789";
    QDateTime t1;
    QString t2;
    t2=t1.currentDateTime().toString("yyyy-MM-dd");

    //QFile file(tst1+".txt");
    QFile file(tst1+"_"+t2+".txt");
    
    if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
        return;
    
    QTextStream out(&file);
    out << "Date & Time of inspection: " << t1.currentDateTime().toString("yyyy-MM-dd HH:mm:ss") << "\n";
    out << "Subassembly Serial Number: " << tst1 << "\n";
    out << "Subassembly Length: " << tst << "\n";
    out << "Subassembly Weight: " << 123456785 << "\n";
    out << "Derived Flow Zone: " << 123456785 << "\n";
    out << "Visual Inspection Remarks: " << 123456785 << "\n";
    out << "Subassembly Straightness: " << 123456785 << "\n";
    out << "Average Flow Through DPTRs: " << 123456785 << "\n";
    out << "Enrichment Zone: " << 123456785 << "\n";
    out << "Subassembly Accept/Reject Status: " << 123456785 << "\n";
    

    }

    void MainWindow::client_logic()
    {
    //connect to socket
    if(tcpSocket->state() != QAbstractSocket::ConnectedState)
    {
    ui->lineEdit_11->setText("Not Connected");
    tcpSocket->connectToHost("10.24.3.106",3000);
    tcpSocket->waitForConnected(500);
    }
    else if(tcpSocket->state() == QAbstractSocket::ConnectedState)
    {
    ui->lineEdit_11->setText("Connected");
    }

    //---------------read data logic or logic for receiving data from server to client------------------------//

    qint64 available_bytes_length=0;
    qint64 no_of_bytes_read;
    char read_packet_char[8100]={0};
    
    if(tcpSocket->state() == QAbstractSocket::ConnectedState)
    {
        //read routine
        tcpSocket->waitForReadyRead(900);
        available_bytes_length=tcpSocket->bytesAvailable();
        ui->lineEdit_12->setText(QString::number(available_bytes_length));
        qDebug() << "available_bytes_length" << available_bytes_length;
    
        if (available_bytes_length!=0)
        {
            if (available_bytes_length<1800)
            {
                no_of_bytes_read = tcpSocket->read(read_packet_char,8100);
                qDebug() << "no_of_bytes_came 1st" << no_of_bytes_read;
    
                tcpSocket->waitForReadyRead(900);
    
                no_of_bytes_read = tcpSocket->read(&read_packet_char[no_of_bytes_read],8100);
                qDebug() << "no_of_bytes_came 2nd" << no_of_bytes_read;
            }
    
            if (available_bytes_length==1800)
            {
                no_of_bytes_read = tcpSocket->read(read_packet_char,8100);
                qDebug() << "no_of_bytes_read" << no_of_bytes_read;
            }
    
            if (available_bytes_length>1800)
            {
                no_of_bytes_read = tcpSocket->read(read_packet_char,8100);
                qDebug() << "no_of_bytes_came 1st" << no_of_bytes_read;
    
                tcpSocket->waitForReadyRead(900);
    
                no_of_bytes_read = tcpSocket->read(&read_packet_char[no_of_bytes_read],8100);
                qDebug() << "no_of_bytes_came 2nd" << no_of_bytes_read;
            }
        }
        //end of read routine
    

    // //start of write routine
    // char write_packet[3000];
    // for (int i =0;i<3000;i++)
    // {
    // write_packet[i]=(char)i;
    // }
    // qint64 no_of_bytes_written=0;
    // no_of_bytes_written = tcpSocket->write(&write_packet[0],1400);
    // qDebug() << "no_of_bytes_written" << no_of_bytes_written;
    // ui->lineEdit_13->setText(QString::number(no_of_bytes_written));
    // //end of write routine

    }
    
    ui->lineEdit->setText(QString::number(read_packet_char[0]));
    ui->lineEdit_2->setText(QString::number(read_packet_char[1]));
    ui->lineEdit_3->setText(QString::number(read_packet_char[2]));
    ui->lineEdit_4->setText(QString::number(read_packet_char[3]));
    ui->lineEdit_5->setText(QString::number(read_packet_char[4]));
    ui->lineEdit_6->setText(QString::number(read_packet_char[5]));
    ui->lineEdit_7->setText(QString::number(read_packet_char[6]));
    ui->lineEdit_8->setText(QString::number(read_packet_char[7]));
    ui->lineEdit_9->setText(QString::number(read_packet_char[8]));
    ui->lineEdit_10->setText(QString::number(read_packet_char[9]));
    ui->lineEdit_14->setText(QString::number((unsigned char)read_packet_char[1799]));
    
    received_count = (unsigned int) read_packet_char[1799];
    int received_count_difference;
    received_count_difference = received_count - old_received_count;
    ui->lineEdit_15->setText(QString::number(received_count_difference));
    old_received_count = received_count;
    
    if ((received_count_difference==1)||(received_count_difference==1))
    {
    
    }
    else
    {
    packet_drop_count++;
    }
    ui->lineEdit_16->setText(QString::number(packet_drop_count));
    

    //---------------end of read data logic or logic for receiving data from server to client------------------------//

    tcpSocket->flush();
    qDebug() << "Sock State" << tcpSocket->state();
    

    }



  • Hi jsulm

    thanks for reply.
    I am checking the connection status by printing the status in text box of my application.

    Anyways before reading this reply i debugged using :
    qDebug() << "Sock State" << tcpSocket->state();
    qDebug() << "Sock State1" << tcpSocket->error();
    qDebug() << "Sock State2" << tcpSocket->errorString();

    and followed this link:
    https://stackoverflow.com/questions/42121008/switching-from-qt-5-7-to-qt-5-8-the-proxy-type-is-invalid-for-this-operation

    and it worked out for me. My updated code I am attaching for reference:

    #include "mainwindow.h"
    #include "ui_mainwindow.h"

    #include <QtNetwork>
    //#include <QObject>

    unsigned int received_count=0;
    unsigned int old_received_count=0;
    unsigned int packet_drop_count=0;

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);

    QString titlestring ="DDCS Network Component Qualification" ;
    setWindowTitle(titlestring);
    
    ui->lineEdit->setText("hi");
    
    QNetworkProxy proxy;
    QNetworkProxy::setApplicationProxy(proxy);
    
    tcpSocket=new QTcpSocket(this);
    timer=new QTimer(this);
    
    connect(timer,SIGNAL(timeout()),this,SLOT(client_logic()));
    timer->start(1000);
    

    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    void MainWindow::write_to_file() // created for experimenting text file generation
    {
    QString tst="tst";
    QString tst1="123456789";
    QDateTime t1;
    QString t2;
    t2=t1.currentDateTime().toString("yyyy-MM-dd");

    //QFile file(tst1+".txt");
    QFile file(tst1+"_"+t2+".txt");
    
    if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
        return;
    
    QTextStream out(&file);
    out << "Date & Time of inspection: " << t1.currentDateTime().toString("yyyy-MM-dd HH:mm:ss") << "\n";
    out << "Subassembly Serial Number: " << tst1 << "\n";
    out << "Subassembly Length: " << tst << "\n";
    out << "Subassembly Weight: " << 123456785 << "\n";
    out << "Derived Flow Zone: " << 123456785 << "\n";
    out << "Visual Inspection Remarks: " << 123456785 << "\n";
    out << "Subassembly Straightness: " << 123456785 << "\n";
    out << "Average Flow Through DPTRs: " << 123456785 << "\n";
    out << "Enrichment Zone: " << 123456785 << "\n";
    out << "Subassembly Accept/Reject Status: " << 123456785 << "\n";
    

    }

    void MainWindow::client_logic()
    {
    //connect to socket
    if(tcpSocket->state() != QAbstractSocket::ConnectedState)
    {
    ui->lineEdit_11->setText("Not Connected");
    tcpSocket->connectToHost("10.24.3.106",3000);
    tcpSocket->waitForConnected(500);
    }
    else if(tcpSocket->state() == QAbstractSocket::ConnectedState)
    {
    ui->lineEdit_11->setText("Connected");
    }

    //---------------read data logic or logic for receiving data from server to client------------------------//

    qint64 available_bytes_length=0;
    qint64 no_of_bytes_read;
    char read_packet_char[8100]={0};
    
    if(tcpSocket->state() == QAbstractSocket::ConnectedState)
    {
        //read routine
        tcpSocket->waitForReadyRead(900);
        available_bytes_length=tcpSocket->bytesAvailable();
        ui->lineEdit_12->setText(QString::number(available_bytes_length));
        qDebug() << "available_bytes_length" << available_bytes_length;
    
        if (available_bytes_length!=0)
        {
            if (available_bytes_length<1800)
            {
                no_of_bytes_read = tcpSocket->read(read_packet_char,8100);
                qDebug() << "no_of_bytes_came 1st" << no_of_bytes_read;
    
                tcpSocket->waitForReadyRead(900);
    
                no_of_bytes_read = tcpSocket->read(&read_packet_char[no_of_bytes_read],8100);
                qDebug() << "no_of_bytes_came 2nd" << no_of_bytes_read;
            }
    
            if (available_bytes_length==1800)
            {
                no_of_bytes_read = tcpSocket->read(read_packet_char,8100);
                qDebug() << "no_of_bytes_read" << no_of_bytes_read;
            }
    
            if (available_bytes_length>1800)
            {
                no_of_bytes_read = tcpSocket->read(read_packet_char,8100);
                qDebug() << "no_of_bytes_came 1st" << no_of_bytes_read;
    
                tcpSocket->waitForReadyRead(900);
    
                no_of_bytes_read = tcpSocket->read(&read_packet_char[no_of_bytes_read],8100);
                qDebug() << "no_of_bytes_came 2nd" << no_of_bytes_read;
            }
        }
        //end of read routine
    

    // //start of write routine
    // char write_packet[3000];
    // for (int i =0;i<3000;i++)
    // {
    // write_packet[i]=(char)i;
    // }
    // qint64 no_of_bytes_written=0;
    // no_of_bytes_written = tcpSocket->write(&write_packet[0],1400);
    // qDebug() << "no_of_bytes_written" << no_of_bytes_written;
    // ui->lineEdit_13->setText(QString::number(no_of_bytes_written));
    // //end of write routine

    }
    
    ui->lineEdit->setText(QString::number(read_packet_char[0]));
    ui->lineEdit_2->setText(QString::number(read_packet_char[1]));
    ui->lineEdit_3->setText(QString::number(read_packet_char[2]));
    ui->lineEdit_4->setText(QString::number(read_packet_char[3]));
    ui->lineEdit_5->setText(QString::number(read_packet_char[4]));
    ui->lineEdit_6->setText(QString::number(read_packet_char[5]));
    ui->lineEdit_7->setText(QString::number(read_packet_char[6]));
    ui->lineEdit_8->setText(QString::number(read_packet_char[7]));
    ui->lineEdit_9->setText(QString::number(read_packet_char[8]));
    ui->lineEdit_10->setText(QString::number(read_packet_char[9]));
    ui->lineEdit_14->setText(QString::number((unsigned char)read_packet_char[1799]));
    
    received_count = (unsigned int) read_packet_char[1799];
    int received_count_difference;
    received_count_difference = received_count - old_received_count;
    ui->lineEdit_15->setText(QString::number(received_count_difference));
    old_received_count = received_count;
    
    if ((received_count_difference==1)||(received_count_difference==1))
    {
    
    }
    else
    {
    packet_drop_count++;
    }
    ui->lineEdit_16->setText(QString::number(packet_drop_count));
    

    //---------------end of read data logic or logic for receiving data from server to client------------------------//

    tcpSocket->flush();
    qDebug() << "Sock State" << tcpSocket->state();
    qDebug() << "Sock State1" << tcpSocket->error();
    qDebug() << "Sock State2" << tcpSocket->errorString();
    

    }


  • Lifetime Qt Champion

    @Aditya-Gour So, what did you do so far to find out why it does not work?
    I can't find any error handling in your code. For example you call connectToHost but you do not even check whether it succeeded! Please add at least error handling to your application before asking others to debug your app.
    https://doc.qt.io/qt-5/qabstractsocket.html#errorOccurred
    https://doc.qt.io/qt-5/qabstractsocket.html#error
    https://doc.qt.io/qt-5/qiodevice.html#errorString



  • Hi jsulm

    thanks for reply.
    I am checking the connection status by printing the status in text box of my application.

    Anyways before reading this reply i debugged using :
    qDebug() << "Sock State" << tcpSocket->state();
    qDebug() << "Sock State1" << tcpSocket->error();
    qDebug() << "Sock State2" << tcpSocket->errorString();

    and followed this link:
    https://stackoverflow.com/questions/42121008/switching-from-qt-5-7-to-qt-5-8-the-proxy-type-is-invalid-for-this-operation

    and it worked out for me. My updated code I am attaching for reference:

    #include "mainwindow.h"
    #include "ui_mainwindow.h"

    #include <QtNetwork>
    //#include <QObject>

    unsigned int received_count=0;
    unsigned int old_received_count=0;
    unsigned int packet_drop_count=0;

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);

    QString titlestring ="DDCS Network Component Qualification" ;
    setWindowTitle(titlestring);
    
    ui->lineEdit->setText("hi");
    
    QNetworkProxy proxy;
    QNetworkProxy::setApplicationProxy(proxy);
    
    tcpSocket=new QTcpSocket(this);
    timer=new QTimer(this);
    
    connect(timer,SIGNAL(timeout()),this,SLOT(client_logic()));
    timer->start(1000);
    

    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    void MainWindow::write_to_file() // created for experimenting text file generation
    {
    QString tst="tst";
    QString tst1="123456789";
    QDateTime t1;
    QString t2;
    t2=t1.currentDateTime().toString("yyyy-MM-dd");

    //QFile file(tst1+".txt");
    QFile file(tst1+"_"+t2+".txt");
    
    if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
        return;
    
    QTextStream out(&file);
    out << "Date & Time of inspection: " << t1.currentDateTime().toString("yyyy-MM-dd HH:mm:ss") << "\n";
    out << "Subassembly Serial Number: " << tst1 << "\n";
    out << "Subassembly Length: " << tst << "\n";
    out << "Subassembly Weight: " << 123456785 << "\n";
    out << "Derived Flow Zone: " << 123456785 << "\n";
    out << "Visual Inspection Remarks: " << 123456785 << "\n";
    out << "Subassembly Straightness: " << 123456785 << "\n";
    out << "Average Flow Through DPTRs: " << 123456785 << "\n";
    out << "Enrichment Zone: " << 123456785 << "\n";
    out << "Subassembly Accept/Reject Status: " << 123456785 << "\n";
    

    }

    void MainWindow::client_logic()
    {
    //connect to socket
    if(tcpSocket->state() != QAbstractSocket::ConnectedState)
    {
    ui->lineEdit_11->setText("Not Connected");
    tcpSocket->connectToHost("10.24.3.106",3000);
    tcpSocket->waitForConnected(500);
    }
    else if(tcpSocket->state() == QAbstractSocket::ConnectedState)
    {
    ui->lineEdit_11->setText("Connected");
    }

    //---------------read data logic or logic for receiving data from server to client------------------------//

    qint64 available_bytes_length=0;
    qint64 no_of_bytes_read;
    char read_packet_char[8100]={0};
    
    if(tcpSocket->state() == QAbstractSocket::ConnectedState)
    {
        //read routine
        tcpSocket->waitForReadyRead(900);
        available_bytes_length=tcpSocket->bytesAvailable();
        ui->lineEdit_12->setText(QString::number(available_bytes_length));
        qDebug() << "available_bytes_length" << available_bytes_length;
    
        if (available_bytes_length!=0)
        {
            if (available_bytes_length<1800)
            {
                no_of_bytes_read = tcpSocket->read(read_packet_char,8100);
                qDebug() << "no_of_bytes_came 1st" << no_of_bytes_read;
    
                tcpSocket->waitForReadyRead(900);
    
                no_of_bytes_read = tcpSocket->read(&read_packet_char[no_of_bytes_read],8100);
                qDebug() << "no_of_bytes_came 2nd" << no_of_bytes_read;
            }
    
            if (available_bytes_length==1800)
            {
                no_of_bytes_read = tcpSocket->read(read_packet_char,8100);
                qDebug() << "no_of_bytes_read" << no_of_bytes_read;
            }
    
            if (available_bytes_length>1800)
            {
                no_of_bytes_read = tcpSocket->read(read_packet_char,8100);
                qDebug() << "no_of_bytes_came 1st" << no_of_bytes_read;
    
                tcpSocket->waitForReadyRead(900);
    
                no_of_bytes_read = tcpSocket->read(&read_packet_char[no_of_bytes_read],8100);
                qDebug() << "no_of_bytes_came 2nd" << no_of_bytes_read;
            }
        }
        //end of read routine
    

    // //start of write routine
    // char write_packet[3000];
    // for (int i =0;i<3000;i++)
    // {
    // write_packet[i]=(char)i;
    // }
    // qint64 no_of_bytes_written=0;
    // no_of_bytes_written = tcpSocket->write(&write_packet[0],1400);
    // qDebug() << "no_of_bytes_written" << no_of_bytes_written;
    // ui->lineEdit_13->setText(QString::number(no_of_bytes_written));
    // //end of write routine

    }
    
    ui->lineEdit->setText(QString::number(read_packet_char[0]));
    ui->lineEdit_2->setText(QString::number(read_packet_char[1]));
    ui->lineEdit_3->setText(QString::number(read_packet_char[2]));
    ui->lineEdit_4->setText(QString::number(read_packet_char[3]));
    ui->lineEdit_5->setText(QString::number(read_packet_char[4]));
    ui->lineEdit_6->setText(QString::number(read_packet_char[5]));
    ui->lineEdit_7->setText(QString::number(read_packet_char[6]));
    ui->lineEdit_8->setText(QString::number(read_packet_char[7]));
    ui->lineEdit_9->setText(QString::number(read_packet_char[8]));
    ui->lineEdit_10->setText(QString::number(read_packet_char[9]));
    ui->lineEdit_14->setText(QString::number((unsigned char)read_packet_char[1799]));
    
    received_count = (unsigned int) read_packet_char[1799];
    int received_count_difference;
    received_count_difference = received_count - old_received_count;
    ui->lineEdit_15->setText(QString::number(received_count_difference));
    old_received_count = received_count;
    
    if ((received_count_difference==1)||(received_count_difference==1))
    {
    
    }
    else
    {
    packet_drop_count++;
    }
    ui->lineEdit_16->setText(QString::number(packet_drop_count));
    

    //---------------end of read data logic or logic for receiving data from server to client------------------------//

    tcpSocket->flush();
    qDebug() << "Sock State" << tcpSocket->state();
    qDebug() << "Sock State1" << tcpSocket->error();
    qDebug() << "Sock State2" << tcpSocket->errorString();
    

    }


  • Lifetime Qt Champion

    @Aditya-Gour said in Working TCP client not connecting after upgrading to Qt creator 4.14.0 (based on Qt 5.15.2):

    before reading this reply i debugged using :

    Where did you put these lines?

    "My updated code I am attaching for reference" - what is updated there?

    Please connect a slot to https://doc.qt.io/qt-5/qabstractsocket.html#errorOccurred before calling connectToHost and print https://doc.qt.io/qt-5/qiodevice.html#errorString there.