O'Reilly logo

Game Programming using Qt 5 Beginner's Guide - Second Edition by Lorenz Haas, Witold Wysota, Pavel Strakhov

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Time for action – Sending a text via UDP

As an example, let's assume that we have two sockets of the QUdpSocket type. We will call the first one socketA and the other socketB. Both are bound to the localhost, socketA to the 52000 port and socketB to the 52001 port. So, if we want to send the string Hello! from socketA to socketB, we have to write in the application that is holding socketA:

socketA->writeDatagram(QByteArray("Hello!"),
                       QHostAddress("127.0.0.1"), 52001);

The class that holds socketB must have the socket's readyRead() signal connected to a slot. This slot will then be called because of our writeDatagram() call, assuming that the datagram is not lost! In the slot, we read the datagram and the sender's address and port number with: ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required