Socket: Unable to send a Message



  • 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?



  • 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



  • 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



  • 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



  • 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();@


Log in to reply
 

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