开发者

Rebind using new QUdpSocket

开发者 https://www.devze.com 2023-02-04 01:33 出处:网络
I have a network application which uses UDP broadcasts for device discovery, but only accepts one connection at a time. So, when a new TCP connection is made, I delete the QUdpSocket that was used for

I have a network application which uses UDP broadcasts for device discovery, but only accepts one connection at a time. So, when a new TCP connection is made, I delete the QUdpSocket that was used for discovery.

However, when the remote device is disconnected, I want to create a new QUdpSocket and start listening again:

    // Set up a UDP server to respond to any "discovery" messages:
    udpServer = new QUdpSocket(this);
    if (udpServer -> bind(QHostAddress::Any, DISCOVERY_PORT))
        connect(udpServer, SIGNAL(readyRead()),
                this,      SLOT(beDiscovered()));
    else
    {
        fprintf(stderr, "UDP port not bound successfully: %d, ", udpServer ->error());
        fprintf(stderr, udpServer ->errorString().toLocal8Bit());
        fprintf(stderr, "\r\n");
        fflush(stderr);
#ifdef WIN32
        _commit(_fileno(stderr));
#else
        fsync(_fileno(stderr));
#endif
    }

The re-bind fails, however, with code 8, "The boun开发者_如何学JAVAd address is already in use".

So, how can I make sure that when the 'old' QUdpSocket was deleted, it fully releases the address(es) it was bound to?

Alternatievly, should I be binding with QUdpSocket::ShareAddress or QUdpSocket::ReuseAddressHint? This doesn't seem right, as neither really describe the behaviour I want, namely an exclusive binding for my QUdpSocket during its lifetime, and in any case QUdpSocket::ShareAddress is supposed to be the default on Windows.

Thanks, Stephen.


...so in other words the question has answered itself!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号