Qt World Summit: Register Today!

QUdpSocket does not work on Windows Phone 8.1 ?

  • I cannot make QUdpSocket working on WP8.1
    The code works fine on Linux, Android & Windows Desktop but not on WP8.1 ...
    I always get "UnknownSocketError" (bind result always false).
    Tried different combinaisons of addresses, etc.

    Same problem on the emulator and on a real phone, bind does not succeed !

    Does anybody have some experience with UDP/TCP on WP8.1 - Do I need to set a permission ? Where ?
    MultipurposeUDPSocket::MultipurposeUDPSocket(QTextBrowser *tbLog, int portUDP, QObject *parent) : QObject(parent)
    this->tbLog = tbLog;
    this->portUDP = portUDP;
    udpSocket = new QUdpSocket();
    connect(udpSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(slotSocketError(QAbstractSocket::SocketError)));
    QHostAddress address;
    address.setAddress(""); //");
    bool resbind = udpSocket->bind(address, portUDP, QAbstractSocket::DefaultForPlatform);
    if ( resbind == false )
    tbLog->append("CANNOT BIND");
    connect(udpSocket, SIGNAL(readyRead()), this, SLOT(processPendingDatagrams()));
    void MultipurposeUDPSocket::slotSocketError(QAbstractSocket::SocketError err)
    switch ( err )
    case QAbstractSocket::ConnectionRefusedError: tbLog->append("ConnectionRefused"); break;
    case QAbstractSocket::RemoteHostClosedError: tbLog->append("RemoteHostClosedError"); break;
    case QAbstractSocket::HostNotFoundError: tbLog->append("HostNotFoundError"); break;
    case QAbstractSocket::SocketAccessError: tbLog->append("SocketAccessError"); break;
    case QAbstractSocket::SocketResourceError: tbLog->append("SocketResourceError"); break;
    case QAbstractSocket::SocketTimeoutError: tbLog->append("SocketTimeoutError"); break;
    case QAbstractSocket::DatagramTooLargeError: break; tbLog->append("DatagramTooLargeError");
    case QAbstractSocket::NetworkError: tbLog->append("NetworkError"); break;
    case QAbstractSocket::AddressInUseError: tbLog->append("AddressInUseError"); break;
    case QAbstractSocket::SocketAddressNotAvailableError: tbLog->append("SocketAddressNotAvailableError"); break;
    case QAbstractSocket::UnsupportedSocketOperationError: tbLog->append("UnsupportedSocketOperationError"); break;
    case QAbstractSocket::ProxyAuthenticationRequiredError: tbLog->append("ProxyAuthenticationRequiredError"); break;
    case QAbstractSocket::SslHandshakeFailedError: tbLog->append("SslHandshakeFailedError"); break;
    case QAbstractSocket::UnfinishedSocketOperationError: tbLog->append("UnfinishedSocketOperationError"); break;
    case QAbstractSocket::ProxyConnectionRefusedError: tbLog->append("ProxyConnectionRefusedError"); break;
    case QAbstractSocket::ProxyConnectionClosedError: tbLog->append("ProxyConnectionClosedError"); break;
    case QAbstractSocket::ProxyConnectionTimeoutError: tbLog->append("ProxyConnectionTimeoutError"); break;
    case QAbstractSocket::ProxyNotFoundError: tbLog->append("ProxyNotFoundError"); break;
    case QAbstractSocket::ProxyProtocolError: tbLog->append("ProxyProtocolError"); break;
    case QAbstractSocket::OperationError: tbLog->append("OperationError"); break;
    case QAbstractSocket::SslInternalError: tbLog->append("SslInternalError"); break;
    case QAbstractSocket::SslInvalidUserDataError: tbLog->append("SslInvalidUserDataError"); break;
    case QAbstractSocket::TemporaryError: tbLog->append("TemporaryError"); break;
    case QAbstractSocket::UnknownSocketError: tbLog->append("UnknownSocketError"); break; // -1 An unidentified error

  • You should use Code tags to help reading your example code. Just add at-sign before and after, see:

    // code:
    bool function() {
    return true;

  • I'm about just guessing here, but.

    Have you checked you have localhost on you Windows-machine? Sometimes it can have only ip6-address, and then that won't work. You could just use the bind-function that does not take QHostAdress as parameter. That uses QHostAddress::Any.

    Have you checked the return value for error() and errorString() from your udpSocket?

  • Lifetime Qt Champion


    I don't have any experience with Windows Phone 8.1 but Just a quick idea: did you check that the bearer plugins are deployed with your application ?

  • Hello,
    The problem is that when you look into the '.../winphone_arm/plugins' folder, there is no 'bearer' subfolder (qt-opensource-windows-x86-winrt-5.x package install) ?! Whereas in '.../android-armv7/plugins' folder, the subfolder 'bearer' is there with 'libqandroidbearer.so'. (qt-opensource-windows-x86-android-5.x package install)
    So, I suppose there's no bearer.dll plugin for winphone ?
    For Android I did not include the plugin and it is working without it.
    By the way what is the best way to register this plugin in the app ?

    • Add QTPLUGIN += qandroidbearer in .pro
    • Add Q_IMPORT_PLUGIN(qandroidbearer) in main.cpp ?

    To answer Vivelu, errorString() returns also "Unknown error" with just udpSocket->bind();

  • from my experience with WP and C sockets (winsock)
    sometimes it isn't routed right way
    It looks like you are connected to wifi but
    OS route all connection to GSM module :)

    I remember that symbian has similar behavior and they had some sort of api how to select proper network device

    at least WP 8.0 has this behavior

    I hope that Qt Http class use windows http stack as it is also wired but wp http stack is working right :)

  • Partially solved !
    I can now receive datagrams on a real device (not working in the emulator) by adding:
    <Capability Name="internetClientServer"/>
    <Capability Name="privateNetworkClientServer"/>
    after the </Applications> tag in appxManifest.xml file. (just "internetClientServer" should be enough).

    bind() to UDP port is now OK.

    BUT I can still not anything emit from the device ?!.
    QHostAddress address;
    address.setAddress(""); //"); //"); //");
    qint64 res = udpSocket->writeDatagram(datagram.data(), datagram.size(), address, portUDP);
    tbLog->append("Written "+QString::number(res));
    seems to run OK as I receive back the correct number of bytes written and no error - but the datagram is not received/seen by any of my devices: linux & windows desktop neither by android phones on my local net (192.168.0.x)
    As I wrote, the code is running perfectly on Linux & Android !

    I've tried all possible of IP address combinaison (UDP broadcast is not supported on WP8.1) and I can not understand why, even, the local address is not working !

  • Are you sure that the machine where you send the UDP packets to receives them? In my case "writeDatagram" returns the correct number of bytes but the data is never received on the other machine.
    As the UDP protocol is stateless, "writeDatagram" cannot know if the bytes where received by the other machine.

    I think that the QUdpSocket is not working on WP 8.1.

  • There seems to be a bug in the QUdpSocket implementation for WinRT/Phone. I have checked the QUdpSocket files source code without finding the bug.

    But when I implement the UDP socket (bypassing the QUdpSocket) by myself based on this example [1] then everything works fine.

    [1] https://code.msdn.microsoft.com/windowsapps/DatagramSocket-sample-76a7d82b/

  • I just noticed that there is already a bug report describing exactly our problem: https://bugreports.qt.io/browse/QTBUG-44051

  • Hello Lynic,
    Thanks a lot for your analysis ! Maybe will be corrected in release 5.5.x
    (?) - In the meantime, I'll try also to bypass QUdp as you suggested with the example.
    Now trying to get the Bluetooth working which seems also buggy/incomplete on the WinRT platform, but it is an other story...

  • Hi all,

    I found this thread while playing around with UDP sockets and searching for info. It seems that the QTBUG-44051 is now solved and indeed I can correctly send a broadcast message via UDP. However, received data does seem to be totally wrong for some reason.

    I've the following code to search for a target device in the LAN. As the OP's code, mine works perfectly fine on all platforms (Win, Mac, Linux, iOS and Android) expect WinPhone:

    udpSocket = new QUdpSocket(this);
        foreach (QNetworkInterface iff, ifs)
            if(iff.isValid() && !(iff.flags() & QNetworkInterface::IsLoopBack) &&
                    (iff.flags() & QNetworkInterface::CanBroadcast) &&
                    (iff.flags() & QNetworkInterface::IsRunning) && (iff.addressEntries().size() > 0))
                foreach(QNetworkAddressEntry a, iff.addressEntries())
                    if(a.ip().protocol() == QAbstractSocket::IPv4Protocol)
                        udpSocket->writeDatagram(data, length, a.broadcast(), BROADCAST_PORT);

    I had to comment out some of the checks - maybe because bearer management is not implemented in WinPhone. That's fine and allowed the data sending. We have also checked that the data is correctly received by the target device via debugger.

    As for what regard data reception, my UDP socket receives both the message itself (since it is a broadcast one) and the reply from the target device. The number of received bytes is correct in both cases: I send 4 bytes and I should receive 15 bytes from the target device. HOWEVER, the received content has the WRONG value of:

    \0x00cdcdcd    // original message sent by my code


    \0x00cdcdcdcdcdcdcdcdcdcdcdcdcdcd    // answer from the target device

    Am I doing something wrong, the missing bearer management affects the UDP broadcast behaviour or have I found a (quite strange) bug in Winphone qt network code?

    Any help is really appreciated.
    Thanks in advance.

  • To everyone searching for an answer: according to developers, the WinPhone implementation of the network interface lacks some functionalities. I've opened a bug report which can be followed here.

Log in to reply