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

Is QTcpSocket example working



  • I have made client-server simulator in C. They are connected with tcp and it works. I want to build HMI with Qt and I used chapter 15 Tripplanner-Tripserver as an example but I can not make it run even locally. Can somebody confirm the example is correct or what shall be corrected.

    You will find examples in:
    [C++ GUI Programming with Qt4, 2nd Edition] http://www.informit.com/store/c-plus-plus-gui-programming-with-qt4-9780132354165

    ->[Download]

    I am running 64bit Ubuntu.


  • Lifetime Qt Champion

    @jalomann How is anybody supposed to know what should be corrected if you do not tell us what is not working?
    Does it build? If not what error messages do you get? If it builds what happens on runtime?



  • Ou. Server-Client usually used to communicate over network.
    In this example client is used to ask train timetable between
    German towns. Server is supposed to create a random trip and
    timetable and send it over tcp connection back to client which
    in turn shows it in widget. In this case the address is set
    as localhost, so it should run on single computer.

    I see a lot of questions on forum about QTcpSocket.
    So the question is: Does QTcpSocket work at all? Did somebody
    use for something usefull so far?



  • Hai...
    no worry...
    QTcpSocket works fine...



  • Hi,

    As @jsulm said, can u provide the code, so the users in the forum can come to know.
    Regarding the above mentioned Topic.

    Thanks,


  • Lifetime Qt Champion

    Hi
    For other samples, this worked very well for me
    http://www.bogotobogo.com/cplusplus/sockets_server_client_QT.php


  • Lifetime Qt Champion

    @jalomann localhost should work just fine.
    What exactly is the issue?



  • The code is found in the tarball in the link. I didn't want to paste the whole code here because it is in nice package. It compiles but it doesn't make the connection. I have debugged so for far that the constructor gets called but no connection. The progress bar is shown but it hangs there.


  • Qt Champions 2017

    @jalomann said in Is QTcpSocket example working:

    Does QTcpSocket work at all?

    Yes, since I've started using Qt - about 10 years ago. My suspicion it was working long before that too ...

    Did somebody use for something usefull so far?

    Thousands upon thousands of people I'd assume.

    @jalomann said in Is QTcpSocket example working:

    I didn't want to paste the whole code here because it is in nice package.

    Paste only the relevant part - where the connection is supposed to be established, then we can ask for more information if it's needed.



  • This is what happens: http://jalomann.fi/tripserver.mp4

    It is a very good book to learn Qt. I thought somebody would have been interested.


  • Qt Champions 2017

    Hi,
    We got what the problem is, but I repeat:
    Please post the relevant code!

    Also from your clip I gathered you don't use a debugger, install and use a debugger, that's what it was invented for - to track down programming errors.



  • tripserver.pro

    TEMPLATE      = app
    QT           += network widgets
    HEADERS       = clientsocket.h \
                    tripserver.h
    SOURCES       = clientsocket.cpp \
                    main.cpp \
                    tripserver.cpp
    
    

    tripserver.h

    #ifndef TRIPSERVER_H
    #define TRIPSERVER_H
    
    #include <QTcpServer>
    
    class TripServer : public QTcpServer
    {
        Q_OBJECT
    
    public:
        TripServer(QObject *parent = 0);
    
    private:
        void incomingConnection(int socketId);
    };
    
    #endif
    
    

    tripserver.cpp

    #include <QtCore>
    
    #include "clientsocket.h"
    #include "tripserver.h"
    
    TripServer::TripServer(QObject *parent)
        : QTcpServer(parent)
    {
    }
    
    void TripServer::incomingConnection(int socketId)
    {
        ClientSocket *socket = new ClientSocket(this);
        socket->setSocketDescriptor(socketId);
    }
    
    

    clientsocket.h

    #ifndef CLIENTSOCKET_H
    #define CLIENTSOCKET_H
    
    #include <QTcpSocket>
    
    class QDate;
    class QTime;
    
    class ClientSocket : public QTcpSocket
    {
        Q_OBJECT
    
    public:
        ClientSocket(QObject *parent = 0);
    
    private slots:
        void readClient();
    
    private:
        void generateRandomTrip(const QString &from, const QString &to,
                                const QDate &date, const QTime &time);
    
        quint16 nextBlockSize;
    };
    
    #endif
    

    clientsocket.cpp

    #include <QtNetwork>
    
    #include "clientsocket.h"
    
    ClientSocket::ClientSocket(QObject *parent)
        : QTcpSocket(parent)
    {
        connect(this, SIGNAL(readyRead()), this, SLOT(readClient()));
        connect(this, SIGNAL(disconnected()), this, SLOT(deleteLater()));
    
        nextBlockSize = 0;
    }
    
    void ClientSocket::readClient()
    {
        QDataStream in(this);
        in.setVersion(QDataStream::Qt_4_3);
    
        if (nextBlockSize == 0) {
            if (bytesAvailable() < sizeof(quint16))
                return;
            in >> nextBlockSize;
        }
    
        if (bytesAvailable() < nextBlockSize)
            return;
    
        quint8 requestType;
        QString from;
        QString to;
        QDate date;
        QTime time;
        quint8 flag;
    
        in >> requestType;
        if (requestType == 'S') {
            in >> from >> to >> date >> time >> flag;
    
            std::srand(from.length() * 3600 + to.length() * 60
                       + time.hour());
            int numTrips = std::rand() % 8;
            for (int i = 0; i < numTrips; ++i)
                generateRandomTrip(from, to, date, time);
    
            QDataStream out(this);
            out << quint16(0xFFFF);
        }
    
        close();
    }
    
    void ClientSocket::generateRandomTrip(const QString & /* from */,
            const QString & /* to */, const QDate &date, const QTime &time)
    {
        QByteArray block;
        QDataStream out(&block, QIODevice::WriteOnly);
        out.setVersion(QDataStream::Qt_4_3);
        quint16 duration = std::rand() % 200;
        out << quint16(0) << date << time << duration << quint8(1)
            << QString("InterCity");
        out.device()->seek(0);
        out << quint16(block.size() - sizeof(quint16));
        write(block);
    }
    
    

    main.cpp

    #include <QtWidgets>
    #include <iostream>
    
    #include "tripserver.h"
    
    int main(int argc, char *argv[])
    {
        QApplication app(argc, argv);
        TripServer server;
        if (!server.listen(QHostAddress::Any, 6178)) {
            std::cerr << "Failed to bind to port" << std::endl;
            return 1;
        }
    
        QPushButton quitButton(QObject::tr("&Quit"));
        quitButton.setWindowTitle(QObject::tr("Trip Server"));
        QObject::connect(&quitButton, SIGNAL(clicked()),
                         &app, SLOT(quit()));
        quitButton.show();
        return app.exec();
    }


  • tripplanner.h

    #ifndef TRIPPLANNER_H
    #define TRIPPLANNER_H
    
    #include <QDialog>
    #include <QTcpSocket>
    
    #include "ui_tripplanner.h"
    
    class QPushButton;
    
    class TripPlanner : public QDialog, private Ui::TripPlanner
    {
        Q_OBJECT
    
    public:
        TripPlanner(QWidget *parent = 0);
    
    private slots:
        void connectToServer();
        void sendRequest();
        void updateTableWidget();
        void stopSearch();
        void connectionClosedByServer();
        void error();
    
    private:
        void closeConnection();
    
        QPushButton *searchButton;
        QPushButton *stopButton;
        QTcpSocket tcpSocket;
        quint16 nextBlockSize;
    };
    
    #endif
    
    

    tripplanner.cpp

    #include <QtWidgets>
    #include <QtNetwork>
    
    #include "tripplanner.h"
    
    TripPlanner::TripPlanner(QWidget *parent)
        : QDialog(parent)
    {
        setupUi(this);
    
        searchButton = buttonBox->addButton(tr("&Search"),
                                            QDialogButtonBox::ActionRole);
        stopButton = buttonBox->addButton(tr("S&top"),
                                          QDialogButtonBox::ActionRole);
        stopButton->setEnabled(false);
        buttonBox->button(QDialogButtonBox::Close)->setText(tr("&Quit"));
    
        QDateTime dateTime = QDateTime::currentDateTime();
        dateEdit->setDate(dateTime.date());
        timeEdit->setTime(QTime(dateTime.time().hour(), 0));
    
        progressBar->hide();
        progressBar->setSizePolicy(QSizePolicy::Preferred,
                                   QSizePolicy::Ignored);
    
        tableWidget->verticalHeader()->hide();
        tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
    
        connect(searchButton, SIGNAL(clicked()),
                this, SLOT(connectToServer()));
        connect(stopButton, SIGNAL(clicked()), this, SLOT(stopSearch()));
        connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
    
        connect(&tcpSocket, SIGNAL(connected()), this, SLOT(sendRequest()));
        connect(&tcpSocket, SIGNAL(disconnected()),
                this, SLOT(connectionClosedByServer()));
        connect(&tcpSocket, SIGNAL(readyRead()),
                this, SLOT(updateTableWidget()));
        connect(&tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)),
                this, SLOT(error()));
    }
    
    void TripPlanner::connectToServer()
    {
    #if 1
        tcpSocket.connectToHost(QHostAddress::LocalHost, 6178);
    #else
        tcpSocket.connectToHost("tripserver.zugbahn.de", 6178);
    #endif
    
        tableWidget->setRowCount(0);
        searchButton->setEnabled(false);
        stopButton->setEnabled(true);
        statusLabel->setText(tr("Connecting to server..."));
        progressBar->show();
    
        nextBlockSize = 0;
    }
    
    void TripPlanner::sendRequest()
    {
        QByteArray block;
        QDataStream out(&block, QIODevice::WriteOnly);
        out.setVersion(QDataStream::Qt_4_3);
        out << quint16(0) << quint8('S') << fromComboBox->currentText()
            << toComboBox->currentText() << dateEdit->date()
            << timeEdit->time();
    
        if (departureRadioButton->isChecked()) {
            out << quint8('D');
        } else {
            out << quint8('A');
        }
        out.device()->seek(0);
        out << quint16(block.size() - sizeof(quint16));
        tcpSocket.write(block);
    
        statusLabel->setText(tr("Sending request..."));
    }
    
    void TripPlanner::updateTableWidget()
    {
        QDataStream in(&tcpSocket);
        in.setVersion(QDataStream::Qt_4_3);
    
        forever {
            int row = tableWidget->rowCount();
    
            if (nextBlockSize == 0) {
                if (tcpSocket.bytesAvailable() < sizeof(quint16))
                    break;
                in >> nextBlockSize;
            }
    
            if (nextBlockSize == 0xFFFF) {
                closeConnection();
                statusLabel->setText(tr("Found %1 trip(s)").arg(row));
                break;
            }
    
            if (tcpSocket.bytesAvailable() < nextBlockSize)
                break;
    
            QDate date;
            QTime departureTime;
            QTime arrivalTime;
            quint16 duration;
            quint8 changes;
            QString trainType;
    
            in >> date >> departureTime >> duration >> changes >> trainType;
            arrivalTime = departureTime.addSecs(duration * 60);
    
            tableWidget->setRowCount(row + 1);
    
            QStringList fields;
            fields << date.toString(Qt::LocalDate)
                   << departureTime.toString(tr("hh:mm"))
                   << arrivalTime.toString(tr("hh:mm"))
                   << tr("%1 hr %2 min").arg(duration / 60)
                                        .arg(duration % 60)
                   << QString::number(changes)
                   << trainType;
            for (int i = 0; i < fields.count(); ++i)
                tableWidget->setItem(row, i,
                                     new QTableWidgetItem(fields[i]));
            nextBlockSize = 0;
        }
    }
    
    void TripPlanner::stopSearch()
    {
        statusLabel->setText(tr("Search stopped"));
        closeConnection();
    }
    
    void TripPlanner::connectionClosedByServer()
    {
        if (nextBlockSize != 0xFFFF)
            statusLabel->setText(tr("Error: Connection closed by server"));
        closeConnection();
    }
    
    void TripPlanner::error()
    {
        statusLabel->setText(tcpSocket.errorString());
        closeConnection();
    }
    
    void TripPlanner::closeConnection()
    {
        tcpSocket.close();
        searchButton->setEnabled(true);
        stopButton->setEnabled(false);
        progressBar->hide();
    }
    
    

    tripplanner.pro

    TEMPLATE      = app
    QT           += network widgets
    HEADERS       = tripplanner.h
    SOURCES       = main.cpp \
                    tripplanner.cpp
    FORMS         = tripplanner.ui
    
    

    main.cpp

    #include <QApplication>
    
    #include "tripplanner.h"
    
    int main(int argc, char *argv[])
    {
        QApplication app(argc, argv);
        TripPlanner tripPlanner;
        tripPlanner.show();
        return app.exec();
    }
    
    

    tripplanner.ui

    <ui version="4.0" >
     <class>TripPlanner</class>
     <widget class="QDialog" name="TripPlanner" >
      <property name="geometry" >
       <rect>
        <x>0</x>
        <y>0</y>
        <width>382</width>
        <height>369</height>
       </rect>
      </property>
      <property name="windowTitle" >
       <string>Trip Planner</string>
      </property>
      <layout class="QGridLayout" >
       <item row="0" column="0" >
        <widget class="QGroupBox" name="tripInfoGroupBox" >
         <property name="title" >
          <string>Trip Information</string>
         </property>
         <layout class="QGridLayout" >
          <item row="3" column="0" >
           <widget class="QLabel" name="timeLabel" >
            <property name="text" >
             <string>Appro&amp;ximate Time:</string>
            </property>
            <property name="buddy" >
             <cstring>timeEdit</cstring>
            </property>
           </widget>
          </item>
          <item row="2" column="1" >
           <widget class="QDateTimeEdit" name="dateEdit" >
            <property name="displayFormat" >
             <string>yyyy-MM-dd</string>
            </property>
           </widget>
          </item>
          <item row="3" column="1" >
           <widget class="QDateTimeEdit" name="timeEdit" >
            <property name="displayFormat" >
             <string>hh:mm:ss</string>
            </property>
           </widget>
          </item>
          <item row="2" column="0" >
           <widget class="QLabel" name="dateLabel" >
            <property name="text" >
             <string>&amp;Date:</string>
            </property>
            <property name="buddy" >
             <cstring>dateEdit</cstring>
            </property>
           </widget>
          </item>
          <item row="1" column="0" >
           <widget class="QLabel" name="toLabel" >
            <property name="text" >
             <string>&amp;To:</string>
            </property>
            <property name="buddy" >
             <cstring>toComboBox</cstring>
            </property>
           </widget>
          </item>
          <item row="4" column="0" colspan="2" >
           <layout class="QHBoxLayout" >
            <item>
             <widget class="QRadioButton" name="departureRadioButton" >
              <property name="text" >
               <string>D&amp;eparture</string>
              </property>
              <property name="shortcut" >
               <string>Alt+E</string>
              </property>
              <property name="checked" >
               <bool>true</bool>
              </property>
             </widget>
            </item>
            <item>
             <widget class="QRadioButton" name="arrivalRadioButton" >
              <property name="text" >
               <string>&amp;Arrival</string>
              </property>
              <property name="shortcut" >
               <string>Alt+A</string>
              </property>
             </widget>
            </item>
           </layout>
          </item>
          <item row="0" column="1" >
           <widget class="QComboBox" name="fromComboBox" >
            <property name="currentIndex" >
             <number>0</number>
            </property>
            <item>
             <property name="text" >
              <string>Berlin</string>
             </property>
            </item>
            <item>
             <property name="text" >
              <string>Bonn</string>
             </property>
            </item>
            <item>
             <property name="text" >
              <string>Bremen</string>
             </property>
            </item>
            <item>
             <property name="text" >
              <string>Dresden</string>
             </property>
            </item>
            <item>
             <property name="text" >
              <string>Düsseldorf</string>
             </property>
            </item>
            <item>
             <property name="text" >
              <string>Dortmund</string>
             </property>
            </item>
            <item>
             <property name="text" >
              <string>Frankfurt am Main</string>
             </property>
            </item>
            <item>
             <property name="text" >
              <string>Hannover</string>
             </property>
            </item>
            <item>
             <property name="text" >
              <string>Hamburg</string>
             </property>
            </item>
            <item>
             <property name="text" >
              <string>München</string>
             </property>
            </item>
            <item>
             <property name="text" >
              <string>Nürnberg</string>
             </property>
            </item>
            <item>
             <property name="text" >
              <string>Rostock</string>
             </property>
            </item>
            <item>
             <property name="text" >
              <string>Stuttgart</string>
             </property>
            </item>
           </widget>
          </item>
          <item row="1" column="1" >
           <widget class="QComboBox" name="toComboBox" >
            <property name="currentIndex" >
             <number>0</number>
            </property>
            <item>
             <property name="text" >
              <string>Berlin</string>
             </property>
            </item>
            <item>
             <property name="text" >
              <string>Bonn</string>
             </property>
            </item>
            <item>
             <property name="text" >
              <string>Bremen</string>
             </property>
            </item>
            <item>
             <property name="text" >
              <string>Dresden</string>
             </property>
            </item>
            <item>
             <property name="text" >
              <string>Düsseldorf</string>
             </property>
            </item>
            <item>
             <property name="text" >
              <string>Dortmund</string>
             </property>
            </item>
            <item>
             <property name="text" >
              <string>Frankfurt am Main</string>
             </property>
            </item>
            <item>
             <property name="text" >
              <string>Hannover</string>
             </property>
            </item>
            <item>
             <property name="text" >
              <string>Hamburg</string>
             </property>
            </item>
            <item>
             <property name="text" >
              <string>München</string>
             </property>
            </item>
            <item>
             <property name="text" >
              <string>Nürnberg</string>
             </property>
            </item>
            <item>
             <property name="text" >
              <string>Rostock</string>
             </property>
            </item>
            <item>
             <property name="text" >
              <string>Stuttgart</string>
             </property>
            </item>
           </widget>
          </item>
          <item row="0" column="0" >
           <widget class="QLabel" name="fromLabel" >
            <property name="text" >
             <string>&amp;From:</string>
            </property>
            <property name="buddy" >
             <cstring>fromComboBox</cstring>
            </property>
           </widget>
          </item>
         </layout>
        </widget>
       </item>
       <item rowspan="3" row="0" column="1" >
        <widget class="QDialogButtonBox" name="buttonBox" >
         <property name="orientation" >
          <enum>Qt::Vertical</enum>
         </property>
         <property name="standardButtons" >
          <set>QDialogButtonBox::Close</set>
         </property>
        </widget>
       </item>
       <item row="1" column="0" >
        <widget class="QTableWidget" name="tableWidget" >
         <property name="columnCount" >
          <number>6</number>
         </property>
         <column>
          <property name="text" >
           <string>Date</string>
          </property>
         </column>
         <column>
          <property name="text" >
           <string>Departure</string>
          </property>
         </column>
         <column>
          <property name="text" >
           <string>Arrival</string>
          </property>
         </column>
         <column>
          <property name="text" >
           <string>Duration</string>
          </property>
         </column>
         <column>
          <property name="text" >
           <string>Changes</string>
          </property>
         </column>
         <column>
          <property name="text" >
           <string>Train type</string>
          </property>
         </column>
        </widget>
       </item>
       <item row="2" column="0" >
        <layout class="QHBoxLayout" >
         <item>
          <widget class="QLabel" name="statusLabel" >
           <property name="sizePolicy" >
            <sizepolicy vsizetype="Preferred" hsizetype="Expanding" >
             <horstretch>0</horstretch>
             <verstretch>0</verstretch>
            </sizepolicy>
           </property>
           <property name="text" >
            <string>Ready</string>
           </property>
          </widget>
         </item>
         <item>
          <widget class="QProgressBar" name="progressBar" >
           <property name="minimum" >
            <number>0</number>
           </property>
           <property name="maximum" >
            <number>100</number>
           </property>
           <property name="orientation" >
            <enum>Qt::Horizontal</enum>
           </property>
          </widget>
         </item>
        </layout>
       </item>
      </layout>
     </widget>
     <resources/>
     <connections/>
    </ui>
    
    


  • I downloaded compiled and executed the example and it works for me. You need to make some changes to make it work in Qt5 (mainly adding the widgets module), remove the std::srand useless part.

    You also need to adjust incomingConnection method adding the line addPendingConnection(socket); to comply with http://doc.qt.io/qt-5/qtcpserver.html#incomingConnection

    Note: If another socket is created in the reimplementation of this method, it needs to be added to the Pending Connections mechanism by calling addPendingConnection().

    Make sure you start the server first and then the client


  • Qt Champions 2017

    TL;DR
    I applaud @VRonin's dedication, though.



  • Also, unrelated to Qt, depending on the paranoia level of your system's admin you might need to ask for adjusting the Mandatory Access Control / firewall settings.


  • Qt Champions 2017

    Forgot to add a minor detail to @VRonin's answer. If you're running a 64 bit compiler, this function isn't an override but an overload so it will never be called from the base class:

    void TripServer::incomingConnection(int socketId)
    

    qintptr is different from int. Here's the documentiation on incommingConnection().




  • Qt Champions 2017

    @VRonin

    munch, munch, mnm ...



  • @Wieland +1 for this. I remembered to check firewall.


Log in to reply