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. Basic Problem with TCP connection
Forum Updated to NodeBB v4.3 + New Features

Basic Problem with TCP connection

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 2 Posters 1.4k Views 1 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.
  • JulianJ Offline
    JulianJ Offline
    Julian
    wrote on last edited by kshegunov
    #1

    Hello, I'm doing a TCP connection between two PC on the same lan (one with 192.168.0.1 and the other with 192.168.0.2) but as I want to make it generic because I'm doing it for school so I use a lineEdit to get the pc's IP. The code is:

    MainWindow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    #include <QTcpServer>
    #include <QTcpSocket>
    #include <QHostAddress>
    
    namespace Ui {
    class MainWindow;
    }
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
    
    private slots:
        void on_pushButton_clicked();
        void new_connection();
        void read();
    private:
        Ui::MainWindow *ui;
    
        QHostAddress *direcction;
        QTcpServer *server;
        QTcpSocket *client;
    };
    
    #endif // MAINWINDOW_H
    

    Like you can see I use one QTcpServer and one QTcpSocket on the Server aplication which runs on one of the two pc, in the other I just make a QTcpSocket.

    MainWindow.cpp

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        server = new QTcpServer(this);
        server ->setMaxPendingConnections(1);
    
        client = new QTcpSocket();
        connect(server, SIGNAL(newConnection()),this, SLOT(new_connection()));
    
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::on_pushButton_clicked()
    {
        direcction=new QHostAddress(ui->IPinput->text().toInt());  //IPinput is a lineEdit
        server->listen(*direcction, 0);
    
    }
    
    void MainWindow::new_connection()
    {
        connect(client, SIGNAL(readyRead()), this, SLOT(read()));
    }
    
    void MainWindow::read()
    {
        QByteArray buffer;
        buffer.resize(cliente->bytesAvailable());
        client->read(buffer.data(), buffer.size());
    
        ui->textEdit->append(QString(buffer));
    
    }
    

    What do you thinks? Why it do not work?

    The client application is

    MainWindow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    #include <QtNetwork>
    #include <QTcpSocket>
    #include <QHostAddress>
    
    
    namespace Ui {
    class MainWindow;
    }
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
    
    private slots:
        void on_makeconnection_clicked();
    
        void on_pushButton_clicked();
    
        void read();
    private:
        Ui::MainWindow *ui;
    
        QHostAddress *direcction;
        QTcpSocket *server;
        QString IP;
    
    };
    
    #endif // MAINWINDOW_H
    

    and Mainwindow.cpp

    
    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::on_hacerConexion_clicked()
    {
        IP= ui->IPinput->text();
        direcction=new QHostAddress(IP);
        server = new QTcpSocket(this);
    
        server->connectToHost(IP,0, QIODevice::ReadWrite);
        connect(server, SIGNAL(readyRead()),this, SLOT(read()));
    
    
    }
    
    void MainWindow::on_pushButton_clicked()
    {
        QString data;
        data= ui->DataToSend->text();
    
        server->write(ui->DataToSend->text().toLatin1().data(), ui->DataToSend->text().size());
        ui->DataToSend->clear();
    }
    
    void MainWindow::read()
    {
        QByteArray buffer;
        buffer.resize(servidor->bytesAvailable());
        servidor->read(buffer.data(), buffer.size());
    
        ui->label->setText(QString(buffer));
    
    }
    

    [Edit: Added code tags ~kshegunov]

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      There might be several things. The first one being your QHostAddress that is invalid. You're converting the content of IPinput to an int but don't even check that you get a valid value.

      Also note that you are leaking memory. You never delete direcction. There's no real need to allocate it on the heap.

      I'd recommend taking a look at Qt's networking examples before going further.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • JulianJ Offline
        JulianJ Offline
        Julian
        wrote on last edited by
        #3

        thanks!. So for example on the line:

        client->connectToHost("10.0.0.3", 0, QIODevice::WriteOnly);

        the QString line "10.0.0.3" is the server's ip but it's an local ip. I means it's the ip which the home's router dhcp server assigned to the local network. It's okay the sentence?

        because what I do on the server it's:

        connect (servidor,SIGNAL(newConnection()),this,SLOT(new_connection()));

        and

        void MainWindow::new_connection()
        {
        ui->state->setText("there is connection");
        connect(cliente, SIGNAL(readyRead()), this, SLOT(read()));
        }

        but on the label state I never get the message "there is connection".

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          You should add error checking to your code. You're not doing any so you're missing all information from them that could give you hints at what is not working or going wrong.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • JulianJ Offline
            JulianJ Offline
            Julian
            wrote on last edited by
            #5

            thanks. but what do you means with error checking? there is a special class for it? or it's a QTcpServer's method

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Please, take the time to look at the class's documentation. You have e.g. the QTcpSocke::error signal.

              On a side note, naming your QTcpSocket object server is quite confusing since there's also the QTcpServer class.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              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