Set a local port to a QUdpSocket



  • Hello,
    I would set the local port for QUdpSocket by QAbstractSocket::bind but the QAbstractSocket::connectToHost resets the local port.
    Is it a bug of Qt ?
    A there an other way to set the local port ?
    @
    // local port = 2000
    // destination port = 3000
    QUdpSocket s;
    s.bind(2000);
    qDebug() << "Local port before connectToHost() = " << s.localPort();
    s.connectToHost(QHostAddress("87.250.251.3"), 3000);
    qDebug() << "Local port after connectToHost() = " << s.localPort();
    @



  • Hello,
    You don't need to call 'connectToHost' when use QUdpSocket.
    With 'bind' you already specify to listen on local UDP socket 2000 for data from all addresses.
    See "Broadcast Receiver Example" in documentation.



  • Thanks, it's ok now and more simple.

    I have a similar problem with a QTcpSocket
    see "Set a local port to a QTcpSocket":http://qt-project.org/forums/viewthread/33175/


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.