Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved simple tcp socket program fails when QPushButton click!

    General and Desktop
    2
    3
    433
    Loading More Posts
    • 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.
    • Mandar Khire
      Mandar Khire last edited by

      Hi,
      I am learning simple tcp socket program in qt so I use program which I found in INTERNET.
      When I run it as it is, then I can see socket is created & message transfered properly.
      But when I try to use it with GUI ie. I create one QWidget which has one QPushButton & when this button click my aim is to open socket & transfer message. But I failed. I doesnt know where I am wrong.
      For reference code is as below:-
      Headers

      1. client.h
      #ifndef CLIENT_H
      #define CLIENT_H
      // client.h
      #include <QtNetwork>
      #include <QObject>
      #include <QString>
      #include <QTcpSocket>
      
      class Client: public QObject
      {
          Q_OBJECT
      public:
          Client(QObject* parent = 0);
          ~Client();
          void start(QString address, quint16 port);
      public slots:
          void startTransfer();
      private:
          QTcpSocket client;
      };
      #endif // CLIENT_H
      
      
      1. MainWindow.h
      #ifndef MAINWINDOW_H
      #define MAINWINDOW_H
      
      #include <QMainWindow>
      #include <QObject>
      #include "client.h"
      #include "server.h"
      
      namespace Ui {
      class MainWindow;
      }
      
      class MainWindow : public QMainWindow
      {
          Q_OBJECT
      
      public:
          explicit MainWindow(QWidget *parent = 0);
          ~MainWindow();
      
      private slots:
          void on_pushButton_clicked();
      
      private:
          Ui::MainWindow *ui;
      
      
      };
      
      #endif // MAINWINDOW_H
      
      
      1. server.h
      #ifndef SERVER_H
      #define SERVER_H
      #include <QtNetwork>
      #include <QObject>
      #include <QTcpServer>
      #include <QTcpSocket>
      
      
      class Server: public QObject
      {
          Q_OBJECT
      public:
          Server(QObject  * parent = 0);
          ~Server();
      public slots:
          void acceptConnection();
          void startRead();
      private:
          QTcpServer server;
          QTcpSocket* tcpSocket;
          QString recievedmessage;
      };
      #endif // SERVER_H
      
      

      Sources

      1. client.cc
      #include "client.h"
      #include <QHostAddress>
      
      
      Client::Client(QObject* parent): QObject(parent)
      {
          connect(&client, SIGNAL(connected()),this, SLOT(startTransfer()));
      }
      
      Client::~Client()
      {
          client.close();
      }
      
      void Client::start(QString address, quint16 port)
      {
          QHostAddress addr(address);
          client.connectToHost(addr, port);
      }
      
      void Client::startTransfer()
      {
          QString message = "hello World Server";
           qDebug() << "client transfer message = "<< message;
          int message_length = message.length();
          client.write(message.toStdString().c_str(),message_length);
          //  client.write("Hello World Mandar12345", 23);
      
      }
      
      1. server.cc
      #include "server.h"
      #include <iostream>
      using namespace std;
      
      Server::Server(QObject *parent): QObject(parent)
      {
          connect(&server, SIGNAL(newConnection()),
                  this, SLOT(acceptConnection()));
          server.listen(QHostAddress::Any, 8888);  //8888
      }
      
      Server::~Server()
      {
          //    server.close();
      }
      
      void Server::acceptConnection()
      {
          tcpSocket = server.nextPendingConnection();
      
          connect(tcpSocket, SIGNAL(readyRead()),
                  this, SLOT(startRead()));
      
      }
      
      void Server::startRead()
      {
          char buffer[1024] = {0};
          //  QTcpSocket* socket = qobject_cast< QTcpSocket* >(sender());
          //  tcpSocket = qobject_cast< QTcpSocket* >(sender());
          //  qDebug() << tcpSocket->readAll();
          tcpSocket->read(buffer, tcpSocket->bytesAvailable());
          recievedmessage = "message = " + QString::fromStdString(buffer);
          cout << buffer << endl;
          qDebug() << "recievedmessage = "<< recievedmessage;
          tcpSocket->close();
      }
      
      1. main.cpp
      #include "client.h"
      #include "server.h"
      #include "mainwindow.h"
      #include <QApplication>
      
      int main(int argc, char** argv)
      {
          QApplication app(argc, argv);
      
          //    Client client;
          //    client.start("127.0.0.1", 8888); 
          //    Server server;
          MainWindow w;
          w.show();
          return app.exec();
      }
      
      1. MainWindow.cpp
      #include "mainwindow.h"
      #include "ui_mainwindow.h"
      #include "client.h"
      #include "server.h"
      #include <iostream>
      
      MainWindow::MainWindow(QWidget *parent) :
          QMainWindow(parent),
          ui(new Ui::MainWindow)
      {
          ui->setupUi(this);
      
      }
      
      MainWindow::~MainWindow()
      {
          delete ui;
      }
      
      void MainWindow::on_pushButton_clicked()
      {
          Client client;
          client.start("127.0.0.1", 8888);
          Server server;
      }
      
      

      Need a guidance!

      1 Reply Last reply Reply Quote 0
      • mrjj
        mrjj Lifetime Qt Champion last edited by

        Hi
        Its a classic error where your variable runs out of scope and get deleted.
        You must move the variable to mainwindow.h as a member so it will live
        as long as mainwindow.

        void MainWindow::on_pushButton_clicked()
        {
        Client client; <<< local variable
        client.start("127.0.0.1", 8888);
        Server server; <<< local variable
        } //here server+client is deleted.

        1 Reply Last reply Reply Quote 3
        • Mandar Khire
          Mandar Khire last edited by

          Thanks mrjj,
          Now I understand why variable should kept in header file.
          Thanks.

          1 Reply Last reply Reply Quote 1
          • First post
            Last post