QTcpSocket: Cannot Connect in a LAN
-
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); }
-
@Sajjad-Ali
Your code does aconnectToHost()
immediately followed by asocket->write(data)
. Either put the write in a slot onconnected()
signal or callwaitForConnected()
--- 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 aboutwaitForConnected()
in that situation. Also docs claim taht method may not work well under Windows. You might want delaysendData()
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.
-
@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.