Just started to learn the programming of networks, sockets, and so on. I downloaded a simple server from this article . In the on_starting_clicked() slot, replaced the string

 tcpServer->listen(QHostAddress::Any, 33333) 

on

 tcpServer->listen(QHostAddress::LocalHost, 33333) 

and wrote a simple program that should connect to this server and send exactly 1 message. Her code is:

 int main(int argc, char *argv[]) { QCoreApplication z(argc, argv); QTcpSocket a; a.open(QIODevice::WriteOnly); a.bind(QHostAddress::LocalHost, 33333); a.write(QByteArray("rferfergregerger")); return z.exec(); } 

The problem is that the program does not connect to the server and does not send anything to it. Question: how to make the program send messages to the server? What am I doing wrong and how should I do it? What does QHostAddress::Any generally mean? How do other computers connect to the server? Is it possible to send structures through sockets and, if so, how to do it using QTcpSocket?

  • And what is your server? Do you write it too? How did you determine that the message is not being sent? QHostAddress :: Any is a broadcast ip-address (255.255.255.255), a packet with such a destination address is delivered to all network nodes. And QHostAddress :: LocalHost is the address 127.0.0.1. If you are trying to catch packets in Wireshark, then you will not see packets with such a destination address, you cannot catch them by simple means. And if you see in the server output that no packets are coming, then you need to look at the server code. - maestro
  • The server was not written by me, but by the author of the article on Habré, the link to which I left in the main post. When connecting, the server in the text field should display a message that a new client has connected, and when it receives a packet, it should output it in the same text field. When I try to connect to the server from my program, neither the first nor the second happens. - Ivan Smollenko
  • But no, wrong about QHostAddress :: Any . And on the issue - attach another output ipconfig / all. - maestro
  • Given that Qt is a cross-platform framework, then you can also use the pltform under which you work with the task to specify? - Alex.B

1 answer 1

To connect to the server, use the connectToHost() method:

 int main(int argc, char *argv[]) { QCoreApplication app(argc, argv); QScopedPointer<QTcpSocket> socket(new QTcpSocket()); socket->connectToHost(QHostAddress::LocalHost, 33333); if(socket->waitForConnected(5000) == false) return -1; socket->write("tra-ta-ta"); return app.exec(); } 

Note the warning in the Qt help for the QAbstractSocket::waitForConnected() method:

This function may fail randomly on Windows. Consider using the event loop for your computer will run on Windows.

In Windows, to determine if a socket is connected to the server, it is safer to use the QAbstractSocket::connected() signal:

 int main(int argc, char *argv[]) { QCoreApplication app(argc, argv); QSharedPointer<QTcpSocket> socket(new QTcpSocket()); socket->connectToHost(QHostAddress::LocalHost, 33333); connect(socket.data(), &QTcpSocket::connected, [socket]() { socket->write("tra-ta-ta"); }); return app.exec(); }