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. Socket: Unable to send a Message
Forum Updated to NodeBB v4.3 + New Features

Socket: Unable to send a Message

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 7.8k 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.
  • S Offline
    S Offline
    Seraph
    wrote on last edited by
    #1

    Hi there,

    It's me again with socket issues. :P

    I have a QQueue<QByteArray> dataQueue full of QByteArrays with the exact size of 512 Bytes. The overall size of all ByteArrays is 200MB. (Yes I know... It's just for excessive testing of reliability of my infrastructure, it does not matter if all bytes are transfered correctly)
    Now I want to send the queue via QUdpSocket::writeDatagram() in a while loop.

    The udp socket sends to a multi cast address.
    The receiver joined this multi cast address.

    @
    QQueue<QByteArray> dataQueue;
    QUdpSocket *socket;

    //...

    void send()
    {
    QByteArray b;
    while(dataQueue.size() > 0)
    {
    b = dataQueue.dequeue();

    if(socket->writeDatagram(b.data(), b.size(), QHostAddress("239.255.43.21"), 1564) < 0)
    std::cout<<"Socket error: "<<socket->errorString().toStdString()<<std::endl;
    }
    }

    //...

    @

    OK... If i call send() it dequeues around 1800(not const.) QByteArrays and sends them correctly. But then i get -1 as return of writeDatagram and the output says: "Socket error: Unable to send a Message"

    Now the funny thing again: If i slow the sending down a bit (like with a console output in every loop step) all 200MB will be transfered correctly!!! So... It has to be the socket below the QUdpSocket right? Maybe it's buffer is full before it can send everything?
    Is there a way to wait for the socket to be ready again in a connection less state???

    And now a second question: It only returns -1 when any other socket is listening on the network... Even if it is "wire shark"!!!!

    Could anybody explain to me, why a socket should behave like that? has it smth to do with Multicasting? Because UDP is connection less and should not really care about whether other network members listen or not or receive or not?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      messi
      wrote on last edited by
      #2

      Hi Seraph,

      have you checked the qt documentation?

      QUdpSocket is inherited from QAbstractSocket which is inherited from QIODevice.

      Checkout the manual of QUdpSocket::writeDatagram there you will find two warnings.
      In one it says that you have to use write(). Follow this link it will guide you to QIODevice.
      There you will find various function how to check for any pending data to write.

      Like:
      qint64 QAbstractSocket::bytesToWrite
      qint64 QIODevice::bytesToWrite ()

      void QIODevice::bytesWritten ( qint64 bytes )
      bool QIODevice::waitForBytesWritten ( int msecs )

      With those functions you should be able to sove your problem.

      cheers
      messi

      1 Reply Last reply
      0
      • S Offline
        S Offline
        Seraph
        wrote on last edited by
        #3

        Hi messi,

        Thanks for the reply, i will check it.

        But AFAIK is write and waitForBytesWritten only for sockets in connected state!?
        I do not have a connected state.

        Greetings

        1 Reply Last reply
        0
        • M Offline
          M Offline
          messi
          wrote on last edited by
          #4

          No it should work for both.

          Here is some code which I found on the web:

          http://code.ohloh.net/search?s=QUdpSocket::waitForBytesWritten&browser=Default

          you could realize the same program with sendto()/recvfrom() from <sys/socket.h>

          Cheers
          messi

          1 Reply Last reply
          0
          • S Offline
            S Offline
            Seraph
            wrote on last edited by
            #5

            You mean those lines in this Udp sing thing?

            @rBytes = this->socket->write(buf,maxl); //only for connected state? Where is the destination ip/port?
            this->socket->flush();
            this->socket->waitForBytesWritten();@

            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