[Interest] QUdpSocket and Binding

Jean-Nicolas Artaud jeannicolasartaud at gmail.com
Mon Nov 3 13:57:13 CET 2014


Ok, I was wrong in my approach of the problem.
I had to use the MulticastInterface to solve my trouble: for UDP
processing, QHostAddress::AnyIPv4 only to bind.

The correct code is (maybe for the next one...) :

    QUdpSocket* socket = new QUdpSocket(this);


    // Use a regular expression to parse the adress

    QRegularExpression re( "^(\\d+.\\d+.\\d+.\\d+):(\\d+)$");

    QRegularExpressionMatch match = re.match(address);

    if ( !match.hasMatch() )

    {

        qb::warn( QString("Invalid address %1").arg(address) );

        return 0;

    }


    // Bind to the port

    int port = match.captured(2).toInt();

    if (!socket->bind( QHostAddress::AnyIPv4 , port,
QAbstractSocket::ShareAddress | QAbstractSocket::ReuseAddressHint ))

    {

        qb::warn( QString("Not possible to bind to %1:%2 :
%3").arg(_bindAddress).arg(port).arg(socket->errorString()) );

        return 0;

    }

	else

	{

		qb::inform( QString("Local address: %1, Local port:
%2.").arg(socket->localAddress().toString()).arg(socket->localPort())
);

	}


    // Join the multicast group

    QString ipAddress = match.captured(1);

    if (!socket->joinMulticastGroup( QHostAddress(ipAddress) ))

    {

        qb::warn( QString("Cannot join %1 :
%2").arg(ipAddress).arg(socket->errorString()) );

        return 0;

    }


    // Get the bind address

if ( _bindAddress.isEmpty() )

	{

		qb::inform("No binding activated. Choose default network interface.");

	}

	else

	{

        QHostAddress bindAddress( _bindAddress );

		qb::inform("Bind address: " + bindAddress.toString());

        QList<QNetworkInterface> interfaceList =
QNetworkInterface::allInterfaces();

        bool done = false;

        foreach( const QNetworkInterface& iface, interfaceList )

        {

            if ( iface.allAddresses().contains( bindAddress) )

            {

                qb::inform( QString( "%1 interface selected." ).arg(
bindAddress.toString() ));

                socket->setMulticastInterface( iface );

                done = true;

                break;

            }

        }

        if (!done)

        {

            qb::warn("Binding failed, check your network settings.");

        }


	}


    // IMPORTANT : set the maximum receive buffer size

    // Allow to reach high throughput (4Mb/s) without too many lost packets

    // Default value does not seem to be good enough

    setsockopt(socket->socketDescriptor(), SOL_SOCKET, SO_RCVBUF,
(const char*) &_socketBufferSize, sizeof(int));



    qb::inform( QString("Start listening to %1:%2").arg(ipAddress).arg(port) );


    connect(socket, SIGNAL(readyRead()), this, SLOT(readPendingDatagrams()));

    connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this,
SLOT(onSocketError(QAbstractSocket::SocketError)));


2014-10-31 22:07 GMT+01:00 Thiago Macieira <thiago.macieira at intel.com>:

> On Thursday 30 October 2014 15:43:23 Jean-Nicolas Artaud wrote:
> > I've got a trouble I don't understand : the bind on the QUdpSocket
> doesn't
> > conplains but I don't receive anything if I put my IP Address in
> > _bindAddress. So the binding on AnyIPv4 works but not for adresses.
> >
> > (the soft I'm working on is multi OS (Linux/Windows) I develop and test
> on
> > Linux and I can answer your questions as fast as possible, and even try
> > your suggestions).
>
> Run strace on your application and tell us what system calls it made to
> that
> address. We're looking for a socket(2) call, a bind(2) call and a
> setsockopt(2) call.
>
> --
> Thiago Macieira - thiago.macieira (AT) intel.com
>   Software Architect - Intel Open Source Technology Center
>
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/interest
>



-- 
Jean-Nicolas
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20141103/cd6eb142/attachment.html>


More information about the Interest mailing list