0

I have some sender processes, each of them running the same sender program, in short, these sender write to the same port in localhost like:

udpSocket->writeDatagram(datagram, QHostAddress::Broadcast, 45454);

The receiver processes, each of them also running the same receiver program, bind to the same port, like:

udpSocket->bind(45454, QUdpSocket::ReuseAddressHint); // windows system

connect(udpSocket, SIGNAL(readyRead()),
        this, SLOT(processPendingDatagrams()));

// ...

void Receiver::processPendingDatagrams()
{
    QByteArray datagram;
    while (udpSocket->hasPendingDatagrams()) {
        datagram.resize(int(udpSocket->pendingDatagramSize()));
        udpSocket->readDatagram(datagram.data(), datagram.size());
        statusLabel->setText(tr("Received datagram: \"%1\"")
            .arg(datagram.constData()));
    }
}

All these processes run in localhost.

I did do some experiment on my machine, with two senders and three receivers, it seems that the receiver can receive data success.

But can we make any assumption about the behavior/data when multi-process write to the same port in localhost using QUdpSocket? If not, what might happen(for example, missing some data packet or wrong data received)?

user8510613
  • 1,242
  • 9
  • 27
  • You use the terms "process" and "processes". What do those terms mean to you? Do you really have multiple operating system processes? Multiple threads? Or multiple functions named something like "process"? Those three are very different things, and only one of them should be called "process". – Some programmer dude Apr 27 '19 at 08:42
  • @Someprogrammerdude The first one, i really have multiple operating system processes. – user8510613 Apr 27 '19 at 08:46
  • Then there's no problem. Each process will have its own unique socket, bound locally to different ports (unless you explicitly bind them?). And unless the packets are dropped, the receiver will receive those packets in arbitrary order, but they will be distinct and separate packets. – Some programmer dude Apr 27 '19 at 08:48
  • @Someprogrammerdude I explicitly bind them(the first line that describe the receiver program above), all of them bind to port 45454. I wonder if there is some inner synchronize mechanism? – user8510613 Apr 27 '19 at 08:55
  • That's the *receiver* process binding to a port, that's what it should do. And as long as you don't run multiple receiver programs on the same host that's still not a problem. – Some programmer dude Apr 27 '19 at 08:58
  • @Someprogrammerdude I did run multiple receiver/sender programs, all write/listen to the same port on the same host. – user8510613 Apr 27 '19 at 09:01
  • You can't bind multiple UDP sockets to a single interface/port pair without the `SO_REUSEPORT` option set. – Some programmer dude Apr 27 '19 at 09:06
  • @Someprogrammerdude please see the line ```udpSocket->bind(port, QUdpSocket::ReuseAddressHint)```, and https://doc.qt.io/archives/qt-4.8/qudpsocket.html#BindFlag-enum, "..... On Windows, this is equivalent to the SO_REUSEADDR socket option. " – user8510613 Apr 27 '19 at 09:09
  • `SO_REUSEADDR` is different from `SO_REUSEPORT`. Unless Windows does it differently from everybody else (as usual). – Some programmer dude Apr 27 '19 at 09:16
  • 1
    @Someprogrammerdude i did some experiment on my machine, when there is only one sender and multiple receiver, it works well, all the receiver can receive data properly, i am wondering about the behavior when expanding to multiple sender. According to https://stackoverflow.com/questions/14388706/socket-options-so-reuseaddr-and-so-reuseport-how-do-they-differ-do-they-mean-t/14388707#14388707, Setting SO_REUSEADDR on a socket in Windows behaves like setting SO_REUSEPORT and SO_REUSEADDR on a socket in BSD – user8510613 Apr 27 '19 at 09:19
  • See https://medium.com/uckey/so-reuseport-addr-2-2-how-packets-forwarded-to-multiple-sockets-ce4b83cd0fd2, it should answer your question. :) – Some programmer dude Apr 27 '19 at 09:24
  • @Someprogrammerdude ehhh.... how? I've read the stackoverflow link, it seems that talk nothing about this situation, or did i miss something? – user8510613 Apr 27 '19 at 09:38
  • From the link I gave in my last comment (for `SO_REUSEPORT` *and* `SO_REUSEADDR` for UDP broadcast sockets): "All sockets received the packets". So all receivers will receive all packets. – Some programmer dude Apr 27 '19 at 09:41
  • @Someprogrammerdude i mean, will there be some collision when multiple process es write data to the same port, that might cause packet miss or wrong data, or is there any inner synchronize mechanism existed? – user8510613 Apr 27 '19 at 09:42
  • No collision for senders or receivers. – Some programmer dude Apr 27 '19 at 09:45
  • @Someprogrammerdude Could you explain it more deeply, how? – user8510613 Apr 27 '19 at 11:59

0 Answers0