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. QTcpSocket: Cannot Connect in a LAN
QtWS25 Last Chance

QTcpSocket: Cannot Connect in a LAN

Scheduled Pinned Locked Moved Unsolved General and Desktop
qtnetworkqtcpsockettcp
3 Posts 3 Posters 298 Views
  • 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.
  • S Offline
    S Offline
    Sajjad Ali
    wrote on 13 May 2024, 12:48 last edited by
    #1

    Hello,
    I have 2 PCs.
    PC1 has IP Address 169.254.211.27
    So, I am trying to send data from PC2 to PC1 through QTcpSocket
    But it has some problem in establishing connection.
    I have also tried waitForConnection. but the still cannot see hello message I sent.
    The code in PC1:
    main.cpp

    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    #include "receiver.h"
    
    int main(int argc, char *argv[])
    {
        QGuiApplication app(argc, argv);
        Receiver a;
        QQmlApplicationEngine engine;
        const QUrl url(u"qrc:/6-TCPLearn/main.qml"_qs);
        QObject::connect(
            &engine,
            &QQmlApplicationEngine::objectCreated,
            &app,
            [url](QObject *obj, const QUrl &objUrl) {
                if (!obj && url == objUrl)
                    QCoreApplication::exit(-1);
            },
            Qt::QueuedConnection);
        engine.load(url);
    
        return app.exec();
    }
    
    

    receiver.h

    #ifndef RECEIVER_H
    #define RECEIVER_H
    
    #include <QObject>
    #include <QTcpSocket>
    #include <QHostAddress>
    
    class Receiver : public QObject
    {
        Q_OBJECT
    public:
        explicit Receiver(QObject *parent = nullptr);
        void readData();
    public slots:
    signals:
    private:
        QTcpSocket *socket;
    };
    
    #endif // RECEIVER_H
    
    

    receiver.cpp

    #include "receiver.h"
    
    Receiver::Receiver(QObject *parent)
        : QObject{parent}
    {
        socket = new QTcpSocket(this);
        qInfo() << socket->state();
        socket->connectToHost("127.0.0.1", 1234);
        qInfo() << socket->state();
        connect(socket, &QTcpSocket::readyRead, this, &Receiver::readData);
    }
    
    void Receiver::readData()
    {
        QByteArray data = socket->readAll();
        qInfo() << "Receiver: " << data;
    }
    
    

    The Code in PC2:
    main.cpp

    
    #include <QCoreApplication>
    #include "sendtcp.h"
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
        SendTCP socket;
        socket.sendData();
        return a.exec();
    }
    

    sendtcp.h

    #ifndef SENDTCP_H
    #define SENDTCP_H
    
    #include <QObject>
    #include <QtNetwork/QTcpSocket>
    class SendTCP : public QObject
    {
        Q_OBJECT
    public:
        explicit SendTCP(QObject *parent = nullptr);
        void sendData();
    signals:
    private:
        QTcpSocket *socket;
    };
    
    #endif // SENDTCP_H
    

    sendtcp.cpp

    #include "sendtcp.h"
    
    SendTCP::SendTCP(QObject *parent)
        : QObject{parent}
    {
        socket = new QTcpSocket(this);
        socket->connectToHost("169.254.211.27",1234);
    }
    
    void SendTCP::sendData()
    {
        QByteArray data;
        data = "Hello";
        socket->write(data);
    }
    
    J C 2 Replies Last reply 13 May 2024, 12:57
    0
    • S Sajjad Ali
      13 May 2024, 12:48

      Hello,
      I have 2 PCs.
      PC1 has IP Address 169.254.211.27
      So, I am trying to send data from PC2 to PC1 through QTcpSocket
      But it has some problem in establishing connection.
      I have also tried waitForConnection. but the still cannot see hello message I sent.
      The code in PC1:
      main.cpp

      #include <QGuiApplication>
      #include <QQmlApplicationEngine>
      #include "receiver.h"
      
      int main(int argc, char *argv[])
      {
          QGuiApplication app(argc, argv);
          Receiver a;
          QQmlApplicationEngine engine;
          const QUrl url(u"qrc:/6-TCPLearn/main.qml"_qs);
          QObject::connect(
              &engine,
              &QQmlApplicationEngine::objectCreated,
              &app,
              [url](QObject *obj, const QUrl &objUrl) {
                  if (!obj && url == objUrl)
                      QCoreApplication::exit(-1);
              },
              Qt::QueuedConnection);
          engine.load(url);
      
          return app.exec();
      }
      
      

      receiver.h

      #ifndef RECEIVER_H
      #define RECEIVER_H
      
      #include <QObject>
      #include <QTcpSocket>
      #include <QHostAddress>
      
      class Receiver : public QObject
      {
          Q_OBJECT
      public:
          explicit Receiver(QObject *parent = nullptr);
          void readData();
      public slots:
      signals:
      private:
          QTcpSocket *socket;
      };
      
      #endif // RECEIVER_H
      
      

      receiver.cpp

      #include "receiver.h"
      
      Receiver::Receiver(QObject *parent)
          : QObject{parent}
      {
          socket = new QTcpSocket(this);
          qInfo() << socket->state();
          socket->connectToHost("127.0.0.1", 1234);
          qInfo() << socket->state();
          connect(socket, &QTcpSocket::readyRead, this, &Receiver::readData);
      }
      
      void Receiver::readData()
      {
          QByteArray data = socket->readAll();
          qInfo() << "Receiver: " << data;
      }
      
      

      The Code in PC2:
      main.cpp

      
      #include <QCoreApplication>
      #include "sendtcp.h"
      int main(int argc, char *argv[])
      {
          QCoreApplication a(argc, argv);
          SendTCP socket;
          socket.sendData();
          return a.exec();
      }
      

      sendtcp.h

      #ifndef SENDTCP_H
      #define SENDTCP_H
      
      #include <QObject>
      #include <QtNetwork/QTcpSocket>
      class SendTCP : public QObject
      {
          Q_OBJECT
      public:
          explicit SendTCP(QObject *parent = nullptr);
          void sendData();
      signals:
      private:
          QTcpSocket *socket;
      };
      
      #endif // SENDTCP_H
      

      sendtcp.cpp

      #include "sendtcp.h"
      
      SendTCP::SendTCP(QObject *parent)
          : QObject{parent}
      {
          socket = new QTcpSocket(this);
          socket->connectToHost("169.254.211.27",1234);
      }
      
      void SendTCP::sendData()
      {
          QByteArray data;
          data = "Hello";
          socket->write(data);
      }
      
      J Offline
      J Offline
      JonB
      wrote on 13 May 2024, 12:57 last edited by JonB
      #2

      @Sajjad-Ali
      Your code does a connectToHost() immediately followed by a socket->write(data). Either put the write in a slot on connected() signal or call waitForConnected() --- your code does neither.

      I don't think you will get connected() signal given that you call this before starting the Qt event loop (a.exec()). I don't know about waitForConnected() in that situation. Also docs claim taht method may not work well under Windows. You might want delay sendData() until that loop has started.

      If it does not work connect a slot to void QAbstractSocket::errorOccurred(QAbstractSocket::SocketError socketError) and show result. You should always do this when writing code for production, and even more so while developing.

      1 Reply Last reply
      3
      • S Sajjad Ali
        13 May 2024, 12:48

        Hello,
        I have 2 PCs.
        PC1 has IP Address 169.254.211.27
        So, I am trying to send data from PC2 to PC1 through QTcpSocket
        But it has some problem in establishing connection.
        I have also tried waitForConnection. but the still cannot see hello message I sent.
        The code in PC1:
        main.cpp

        #include <QGuiApplication>
        #include <QQmlApplicationEngine>
        #include "receiver.h"
        
        int main(int argc, char *argv[])
        {
            QGuiApplication app(argc, argv);
            Receiver a;
            QQmlApplicationEngine engine;
            const QUrl url(u"qrc:/6-TCPLearn/main.qml"_qs);
            QObject::connect(
                &engine,
                &QQmlApplicationEngine::objectCreated,
                &app,
                [url](QObject *obj, const QUrl &objUrl) {
                    if (!obj && url == objUrl)
                        QCoreApplication::exit(-1);
                },
                Qt::QueuedConnection);
            engine.load(url);
        
            return app.exec();
        }
        
        

        receiver.h

        #ifndef RECEIVER_H
        #define RECEIVER_H
        
        #include <QObject>
        #include <QTcpSocket>
        #include <QHostAddress>
        
        class Receiver : public QObject
        {
            Q_OBJECT
        public:
            explicit Receiver(QObject *parent = nullptr);
            void readData();
        public slots:
        signals:
        private:
            QTcpSocket *socket;
        };
        
        #endif // RECEIVER_H
        
        

        receiver.cpp

        #include "receiver.h"
        
        Receiver::Receiver(QObject *parent)
            : QObject{parent}
        {
            socket = new QTcpSocket(this);
            qInfo() << socket->state();
            socket->connectToHost("127.0.0.1", 1234);
            qInfo() << socket->state();
            connect(socket, &QTcpSocket::readyRead, this, &Receiver::readData);
        }
        
        void Receiver::readData()
        {
            QByteArray data = socket->readAll();
            qInfo() << "Receiver: " << data;
        }
        
        

        The Code in PC2:
        main.cpp

        
        #include <QCoreApplication>
        #include "sendtcp.h"
        int main(int argc, char *argv[])
        {
            QCoreApplication a(argc, argv);
            SendTCP socket;
            socket.sendData();
            return a.exec();
        }
        

        sendtcp.h

        #ifndef SENDTCP_H
        #define SENDTCP_H
        
        #include <QObject>
        #include <QtNetwork/QTcpSocket>
        class SendTCP : public QObject
        {
            Q_OBJECT
        public:
            explicit SendTCP(QObject *parent = nullptr);
            void sendData();
        signals:
        private:
            QTcpSocket *socket;
        };
        
        #endif // SENDTCP_H
        

        sendtcp.cpp

        #include "sendtcp.h"
        
        SendTCP::SendTCP(QObject *parent)
            : QObject{parent}
        {
            socket = new QTcpSocket(this);
            socket->connectToHost("169.254.211.27",1234);
        }
        
        void SendTCP::sendData()
        {
            QByteArray data;
            data = "Hello";
            socket->write(data);
        }
        
        C Offline
        C Offline
        ChrisW67
        wrote on 14 May 2024, 06:36 last edited by
        #3

        @Sajjad-Ali Quite apart from @JonB's observations:

        • Your receiver is not listening. You need to look at QTcpServer
        • You need to ensure that the receiving end does not have a system firewall blocking whatever port you are listening on.
        1 Reply Last reply
        3

        3/3

        14 May 2024, 06:36

        • Login

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