Results 1 to 6 of 6

Thread: QUdp and waitForReadyRead

  1. #1
    Join Date
    Nov 2007
    Posts
    291
    Thanks
    85
    Thanked 1 Time in 1 Post

    Default QUdp and waitForReadyRead

    hi
    Using connectToHost() with QUdp socket works fine for writing data with write() function.The code below works fine.
    Qt Code:
    1. QUdpSocket socket;
    2. socket.connectToHost(QHostAddress::LocalHost,9930);
    3. int i=5;
    4. socket.write((char*)&i,sizeof(int));
    To copy to clipboard, switch view to plain text mode 

    But using connectToHost() for reading does not work.The below code doesnot work.
    Qt Code:
    1. QUdpSocket socket;
    2. socket.connectToHost(QHostAddress::LocalHost,9930);
    3. if(socket.waitForReadyRead(2000)) //never enters inside this if
    4. qDebug()<<QString(socket.readAll());
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QUdp and waitForReadyRead

    First of all readyRead is emitted when something is to be read from the socket, not when a connection is established. Second of all UDP is a connectionless protocol - there is no point-to-point connection between the hosts. With UDP you can "connect" to a host to mark a default destination for datagrams to be sent, nothing more, not data gets sent there.

  3. #3
    Join Date
    Nov 2007
    Posts
    291
    Thanks
    85
    Thanked 1 Time in 1 Post

    Default Re: QUdp and waitForReadyRead

    I am sure that data is received at the port(9930) to which i am connecting(verified with other program).
    From Qt docs
    if you want to use the standard QIODevice functions read(), readLine(), write(), etc., you must first connect the socket directly to a peer by calling connectToHost().

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QUdp and waitForReadyRead

    Yes, that's correct. But you don't have to use read() or write() with a UDP socket, which doesn't make much sense anyway as you get a "stream" of data in a datagram oriented flow. With QUdpSocket you usually use QUdpSocket::readDatagram() and QUdpSocket::writeDatagram(). As I said connect only marks a default source/destination for data (you can't set a remote host with read() or write() hence they have to use the "default" which is set using connect).

  5. The following user says thank you to wysota for this useful post:

    babu198649 (22nd December 2008)

  6. #5
    Join Date
    Nov 2007
    Posts
    291
    Thanks
    85
    Thanked 1 Time in 1 Post

    Default Re: QUdp and waitForReadyRead

    Thank you
    I have an application which uses TCP ,I want to test the same application with UDP.So i thought that if i use the connectToHost() function ,then only the QTcp socket type has to be changed to QUdp socket type(all the rest of the code remains same).

  7. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QUdp and waitForReadyRead

    No, TCP and UDP protocols have nothing in common apart the fact they both concern the transport layer of the OSI model.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.