Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Two clients using QUdpSocket bound in localhost at same port on same machine : only first gets readyRead()
Forum Updated to NodeBB v4.3 + New Features

Two clients using QUdpSocket bound in localhost at same port on same machine : only first gets readyRead()

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 5.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jena43
    wrote on last edited by
    #1

    Hello everyone,

    I have a problem using "QUdpSocket":http://qt-project.org/doc/qt-5/qudpsocket.html.

    I have a server (not written with Qt) sending data on localhost at port 3333.
    I have a client Qt application which reads the data on localhost at port 3333.
    My code works fine if I run only one instance of my client, but doesn't work for another.
    All the apps (server + client n°1 + ... + client n°N) must run on the same machine.

    Here it is :
    @
    // in the constructor
    _socket = new QUdpSocket(this);
    qDebug()<<"UDP Socket stored at "<<&_socket<<"trying to bind";
    if (_socket->bind(QHostAddress::LocalHost, 3333, QUdpSocket::ShareAddress | QUdpSocket::ReuseAddressHint))
    {
    qDebug()<<"UDP Socket bound. trying to connect signals to app slots";
    connect(_socket, SIGNAL(readyRead()), this, SLOT(readyRead()));
    connect(_socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(errorSocket()));
    qDebug()<<"UDP Socket connected to slots";
    }
    else
    {
    qDebug()<<"error binding the Udp Socket :\n"<<_socket->errorString();
    }
    @

    @
    void TuioEventsListener::errorSocket()
    {
    qDebug()<<"Udp Socket error :\n"<<_socket->errorString();
    }
    @

    @
    void TuioEventsListener::readyRead()
    {
    qDebug()<<"data received";

    QByteArray buffer;
    buffer.resize(_socket->pendingDatagramSize());
    
    QHostAddress sender;
    quint16 senderPort;
    
    _socket->readDatagram(buffer.data(), buffer.size(), &sender, &senderPort);
    // Here I'm proccessing data
    

    }
    @

    Output client n°1 :
    @
    UDP Socket stored at 0x1e58ad8 trying to bind
    UDP Socket bound. trying to connect signals to app slots
    UDP Socket connected to slots
    data received
    data received
    ....
    @

    Output client n°2 :
    @
    UDP Socket stored at 0x1d78ad8 trying to bind
    UDP Socket bound. trying to connect signals to app slots
    UDP Socket connected to slots
    @

    When I close the first instance of the client, the second one receives the readyRead() signal.

    Like "clogwog":http://qt-project.org/mebmer/9158 in this "possible duplicate question":http://qt-project.org/forums/viewthread/7723/ , I think that the readDatagram() could be the cause. If I understood the problem, the first instance of the client has already read the datagram, which sets QUdpSocket::hasPendingDatagram to false ; so the second instance doesn't receives the readyRead() signal.

    What should I do ? Any idea / suggestion is welcome.

    Using Qt5.3.1 on Win7-64b with mingw32 compiler

    1 Reply Last reply
    0
    • A Offline
      A Offline
      ambershark
      wrote on last edited by
      #2

      This "post":http://stackoverflow.com/questions/2772324/can-two-different-udp-socket-in-a-system-bind-same-port may answer your questions.

      My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

      1 Reply Last reply
      0
      • jazzycamelJ Offline
        jazzycamelJ Offline
        jazzycamel
        wrote on last edited by
        #3

        I'm not sure what you're trying to do will work even without Qt. Only one network packet is created by your server when the message is sent and this will enter only one of the receiving sockets packet queues. Sharing the address only means the sockets have the same address/port, not that they will all receive all data. You need to look at broadcasting or multicasting or having whichever client receives the packet share it with the others via another IPC method (shared memory for example).

        For the avoidance of doubt:

        1. All my code samples (C++ or Python) are tested before posting
        2. As of 23/03/20, my Python code is formatted to PEP-8 standards using black from the PSF (https://github.com/psf/black)
        1 Reply Last reply
        0
        • J Offline
          J Offline
          Jena43
          wrote on last edited by
          #4

          Thanks for your replies. I'm currently looking at "QSharedMemory":http://qt-project.org/doc/qt-5/qsharedmemory.html and the "Shared Memory Example":http://qt-project.org/doc/qt-5/qtcore-sharedmemory-example.html

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved