Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Is QTcpSocket example working
Qt 6.11 is out! See what's new in the release blog

Is QTcpSocket example working

Scheduled Pinned Locked Moved Unsolved General and Desktop
20 Posts 8 Posters 5.0k Views 5 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • mrjjM Offline
    mrjjM Offline
    mrjj
    Lifetime Qt Champion
    wrote on last edited by
    #6

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

    1 Reply Last reply
    2
    • J jalomann

      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?

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #7

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

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • J Offline
        J Offline
        jalomann
        wrote on last edited by
        #8

        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.

        1 Reply Last reply
        0
        • J jalomann

          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?

          kshegunovK Offline
          kshegunovK Offline
          kshegunov
          Moderators
          wrote on last edited by kshegunov
          #9

          @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.

          Read and abide by the Qt Code of Conduct

          J 1 Reply Last reply
          2
          • kshegunovK kshegunov

            @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.

            J Offline
            J Offline
            jalomann
            wrote on last edited by
            #10

            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.

            kshegunovK 1 Reply Last reply
            0
            • J jalomann

              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.

              kshegunovK Offline
              kshegunovK Offline
              kshegunov
              Moderators
              wrote on last edited by
              #11

              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.

              Read and abide by the Qt Code of Conduct

              J 1 Reply Last reply
              1
              • kshegunovK kshegunov

                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.

                J Offline
                J Offline
                jalomann
                wrote on last edited by
                #12

                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();
                }
                J 1 Reply Last reply
                0
                • J jalomann

                  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();
                  }
                  J Offline
                  J Offline
                  jalomann
                  wrote on last edited by
                  #13

                  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>
                  
                  
                  kshegunovK 1 Reply Last reply
                  0
                  • VRoninV Offline
                    VRoninV Offline
                    VRonin
                    wrote on last edited by VRonin
                    #14

                    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

                    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                    ~Napoleon Bonaparte

                    On a crusade to banish setIndexWidget() from the holy land of Qt

                    1 Reply Last reply
                    3
                    • J jalomann

                      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>
                      
                      
                      kshegunovK Offline
                      kshegunovK Offline
                      kshegunov
                      Moderators
                      wrote on last edited by
                      #15

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

                      Read and abide by the Qt Code of Conduct

                      1 Reply Last reply
                      1
                      • ? Offline
                        ? Offline
                        A Former User
                        wrote on last edited by
                        #16

                        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.

                        J 1 Reply Last reply
                        3
                        • kshegunovK Offline
                          kshegunovK Offline
                          kshegunov
                          Moderators
                          wrote on last edited by
                          #17

                          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().

                          Read and abide by the Qt Code of Conduct

                          VRoninV 1 Reply Last reply
                          3
                          • kshegunovK kshegunov

                            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().

                            VRoninV Offline
                            VRoninV Offline
                            VRonin
                            wrote on last edited by VRonin
                            #18

                            @kshegunov
                            Give that man a cookie!

                            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                            ~Napoleon Bonaparte

                            On a crusade to banish setIndexWidget() from the holy land of Qt

                            kshegunovK 1 Reply Last reply
                            4
                            • VRoninV VRonin

                              @kshegunov
                              Give that man a cookie!

                              kshegunovK Offline
                              kshegunovK Offline
                              kshegunov
                              Moderators
                              wrote on last edited by
                              #19

                              @VRonin

                              munch, munch, mnm ...

                              Read and abide by the Qt Code of Conduct

                              1 Reply Last reply
                              3
                              • ? A Former User

                                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.

                                J Offline
                                J Offline
                                jalomann
                                wrote on last edited by
                                #20

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

                                1 Reply Last reply
                                0

                                • Login

                                • Login or register to search.
                                • First post
                                  Last post
                                0
                                • Categories
                                • Recent
                                • Tags
                                • Popular
                                • Users
                                • Groups
                                • Search
                                • Get Qt Extensions
                                • Unsolved