1

So, UDP server apparently just listens on some port and handles byte arrays which come with IP and PORT of the source:

// In this code, listener is QUdpSocket*
FileServer::FileServer(QObject *parent)
    : QObject(parent)
    , listener(new QUdpSocket(this))
{
    // bind to listening port
    listener->bind(QHostAddress::Any, 6660);

    connect(listener, &QUdpSocket::readyRead,
            this, &FileServer::readPendingDatagrams, Qt::QueuedConnection);
}

void FileServer::readPendingDatagrams()
{
    while (listener->hasPendingDatagrams()) {
        // This is how this is done in new QT
        //QNetworkDatagram datagram = listener->receiveDatagram();
        //processTheDatagram(datagram);
        // This is how it is done in old QT
        QByteArray datagram;
        datagram.resize(listener->pendingDatagramSize());
        QHostAddress sender;
        quint16 senderPort;

        listener->readDatagram(datagram.data(), datagram.size(),
                                &sender, &senderPort);
        processDatagram(datagram, sender, senderPort);
    }
}

Very nice. This actually works, as I verified using this python snippet:

import socket
host="127.0.0.1"
port=6660
udp_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
udp_sock.sendto(b'PING', (host, port))

The byte array even contained "PING". So I was about to write a client. I assume client will simply send data using QUdpSocket::sendDatagram. But how can he receive data?

First I thought I will call bind(SERVER_ADDRESS_HERE, 6660). But clearly they can't both listen on the same port.

So how do I write a client for the server above?

Tomáš Zato
  • 50,171
  • 52
  • 268
  • 778
  • you connect the readyRead signal of the sender to the corresponding read method – deW1 Apr 11 '17 at 22:35
  • And who choses the port? What's the point of `bind`? – Tomáš Zato Apr 11 '17 at 22:47
  • You're reinventing the wheel. Instead of a roll-your-own protocol, use an existing protocol to ensure that all the chunks are transferred, that the transmission is windowed, etc. If you truly need UDP (why?), implement TFTP on top of it. I think you'd need to convince us and yourself that your choice of UDP makes any sense. – Kuba hasn't forgotten Monica Apr 12 '17 at 13:24
  • @KubaOber this is an educational project, using UDP is the primary goal here. It's part of an university course in networking. – Tomáš Zato Apr 13 '17 at 10:50
  • UDP is literally a means of optionally delivering packets in random order. The **optionality** and **random order** are important. It's like giving a friend a book as a loose stack of pages: they can be shuffled or get lost. You will need to design a protocol that rides on top of UDP that allows you to use the UDP in spite of its characteristics. Even if you come up with a custom protocol, you should at least read [RFC 1350](https://tools.ietf.org/html/rfc1350) to see what's involved in the design of such protocols. Recall that TFTP runs on top of UDP. It's a very simple protocol. – Kuba hasn't forgotten Monica Apr 13 '17 at 14:13

1 Answers1

0

you can use the signal readyread() of a socket to execute a slot when you recive data:

//put this in the MainWindow costructor
connect(socket, SIGNAL(readyread()), SLOT(slot()));

Remember to make the bind to start reciving data from a port:

//this is the code to make the bind
socket->bind(port);

In the slot you can write this code that saves in a QString what you recive:

void MainWindow::slot(){

    QString data = "";
    while(socket->hasPendingDatagrams()) {

        QByteArray datagram;
        datagram.resize(socket->pendingDatagramSize());
        QHostAddress senderIP;
        quint64 senderPort;
        socket->readDatagram(datagram.data(), datagram.size(), &senderIP, &senderPort);

        //if you want to check if the sender was the server you can control senderIp

        data += QString(datagram);
    }
}
EmLe49
  • 71
  • 10
  • This could work but if you want a comunication with a server I suggest you to use TCP (in QT the library is QTcpSocket). – EmLe49 Apr 15 '17 at 17:13
  • How do I chose the port? How can two clients listen on the same machine on the same port? – Tomáš Zato Apr 15 '17 at 18:13
  • First answer : you have to know in which port your server send data. Second answer : I'm not sure, but I think that you can make the bind at the same port with more different sockets. – EmLe49 Apr 15 '17 at 19:28
  • correction: your machine can't use the same port for 2 different processes. Please, if my answer havs been useful conferm it – EmLe49 May 04 '17 at 17:27