Results 1 to 8 of 8

Thread: tftp with QUdpSocket - cannot do - have to use Q3SocketDevice

  1. #1
    Join Date
    Nov 2010
    Posts
    2
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default tftp with QUdpSocket - cannot do - have to use Q3SocketDevice

    I'm talking to a measurement instrument using tftp.

    The funny thing about tftp protocol is that you connect to a server to a specified port, and the server responses from a different, connection specific port (which the server decides), which you then use in further communications.

    With QUdpSocket you cannot get the new server port number. The documentation of peerPort() says 'Returns the port of the connected peer if the socket is in ConnectedState; otherwise returns 0.'

    If you connect a QUdpSocket to a server for tftp protocol you never receive any data (at least I didn't). readyRead() never triggers, using a timer an polling the socket never reads anything.

    If you don't connect and just send datagrams, you receive the replies, but as the socket is not in connected state, peerPort returns 0. (Why this limitation, if this could be lifted then QUdpSocket could be used???)

    So, I ended up using Q3SocketDevice, this works, my problem is now solved.

    But what happens when one day we get Qt5, which probably has Q4Support, but no Q3Support anymore... No tftp possible with Qt anymore ??

    Maybe this ConnectedState limitation in peerPort() should be lifted ?

  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: tftp with QUdpSocket - cannot do - have to use Q3SocketDevice

    If QUdpSocket doesn't work then you must have messed something up.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jul 2012
    Posts
    3
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: tftp with QUdpSocket - cannot do - have to use Q3SocketDevice

    Hy, I'm facing the same problem!

    Here is a test code which is sending a write request to a TFTP server.
    The server sends a response to this request, so I should receive the signal "readyRead()" from the QUdpSocket.
    But I never get it!

    Qt Code:
    1. basicTester::basicTester(QObject *parent) :
    2. QObject(parent)
    3. {
    4. dAddr = QHostAddress("192.168.111.84"); //tftp server address
    5. dPort = 69;
    6.  
    7. sock = new QUdpSocket(this);
    8. connect(sock, SIGNAL(readyRead()), this, SLOT(readData()));
    9. connect(sock, SIGNAL(stateChanged(QAbstractSocket::SocketState)),
    10. this, SLOT(stateChanged(QAbstractSocket::SocketState)));
    11.  
    12. sock->connectToHost(dAddr, dPort);
    13. }
    14.  
    15. void basicTester::stateChanged(QAbstractSocket::SocketState sockstate){
    16. qDebug() << sock->state();
    17. switch(sockstate){
    18. case QAbstractSocket::ConnectedState:
    19. qDebug() << "Socket is connected!";
    20. qDebug() << "sock peer : " << sock->peerAddress() << sock->peerPort();
    21. qDebug() << "sock local: " << sock->localAddress() << sock->localPort();
    22. send();
    23. break;
    24. }
    25. }
    26.  
    27. void basicTester::send() {
    28. QByteArray dgram;
    29. QString remote("myfile.txt");
    30. QString modestring ("octet");
    31. dgram.resize(( 2 + remote.length() + 1 + modestring.length() + 1 ));
    32.  
    33. //TFTP WRQ
    34. dgram[0] = 0x00;
    35. dgram[1] = 0x02;
    36. memcpy( dgram.data()+2, remote.toAscii(), remote.length() +1 );
    37. memcpy( dgram.data()+2+remote.length()+1, modestring.toAscii(), modestring.length() +1 );
    38.  
    39. qDebug() << "Writing dgram to " << dAddr << ":" << dPort;
    40. if(sock->write(dgram) == -1){
    41. qDebug() << "Error sending WRQ packet " << sock->errorString();
    42. }
    43. }
    44.  
    45. void basicTester::readData() {
    46. qDebug() << "basicTester::readData";
    47. }
    To copy to clipboard, switch view to plain text mode 

    I've tested with 3 different tftp server, and I always get the same result.
    Using wireshark I can see that the server is sending the response, but the QUdpSocket doesn't get it.

    I've made a basic UDP echo server (from Qt Example), I've replaced the tftp server by this echoServer, and there I get the "readyRead()" signal!

    The difference I can observe with wireshark is:
    • with TFTP server:
      -> Client sends request from source port 3538(or what ever is generated on connection) ; destination port 69
      -> Server sends response from source port 4096 ; destination port 3528
    • with UDP Echo Server:
      -> Client sends request from source port 3545(or what ever is generated on connection) ; destination port 69
      -> Server sends response from source port 69; destination port 3545


    So the problem seems to be that the tftp server responds with a different source port than the one we used to connect.

    Any clue on how to solve this?

  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: tftp with QUdpSocket - cannot do - have to use Q3SocketDevice

    The clue is to understand that UDP is a connectionless protocol. Calling connectToHost() does not establish any "connection" between the hosts. It only emulates a connection by associating a {host,port} pair with the socket and when a datagram is received from that pair, it is treated as part of a "connection" (and one that is sent is getting sent to that associated host and port). In your case the datagram doesn't match the {host,port} pair so readyRead() is not emitted. If you comment out your connectToHost() call and use readDatagram() and writeDatagram(), everything should work fine.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Jul 2012
    Posts
    3
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: tftp with QUdpSocket - cannot do - have to use Q3SocketDevice

    Thanks for the answer!

    I know that UDP is connectionLess, but using "connectToHost" is the only way I founded to get the client "local port".

    If I just call writeDatagram(), then I don't get my local port, and I need this port for the readDatagram()!

  6. #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: tftp with QUdpSocket - cannot do - have to use Q3SocketDevice

    Use QUdpSocket::bind() to bind your socket to a specified port.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Jul 2012
    Posts
    3
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: tftp with QUdpSocket - cannot do - have to use Q3SocketDevice

    perfect!
    When I tried bind() I still had the conenctToHost(), my bad!

    Problem solved, thank's a lot!

    the correct source code, if it can help someone else:
    Qt Code:
    1. basicTester::basicTester(QObject *parent) :
    2. QObject(parent)
    3. {
    4. dAddr = QHostAddress("192.168.111.84"); //tftp server address
    5. dPort = 69;
    6. sock = new QUdpSocket(this);
    7. connect(sock, SIGNAL(readyRead()), this, SLOT(readData()));
    8. sock->bind(2152); /// \TODO need generate random client TID, and check if port available (select another if not) ; see RFC1350
    9. }
    10.  
    11. void basicTester::send() {
    12. QByteArray dgram;
    13. QString remote("myfile.txt");
    14. QString modestring ("octet");
    15. dgram.resize(( 2 + remote.length() + 1 + modestring.length() + 1 ));
    16.  
    17. //TFTP WRQ
    18. dgram[0] = 0x00;
    19. dgram[1] = 0x02;
    20. memcpy( dgram.data()+2, remote.toAscii(), remote.length() +1 );
    21. memcpy( dgram.data()+2+remote.length()+1, modestring.toAscii(), modestring.length() +1 );
    22.  
    23. qDebug() << "Writing dgram to " << dAddr << ":" << dPort;
    24. if(sock->writeDatagram(dgram, dAddr, dPort) == -1){
    25. qDebug() << "Error sending WRQ packet " << sock->errorString();
    26. }
    27. qDebug() << "sock peer : " << sock->peerAddress() << sock->peerPort();
    28. qDebug() << "sock local: " << sock->localAddress() << sock->localPort();
    29. }
    30.  
    31. void basicTester::readData() {
    32. qDebug() << "basicTester::readData";
    33. while(sock->hasPendingDatagrams()){
    34. QByteArray datagram;
    35. datagram.resize(sock->pendingDatagramSize());
    36. QHostAddress sender;
    37. quint16 senderPort;
    38. sock->readDatagram(datagram.data(), datagram.size(), &sender, &senderPort);
    39. qDebug() << "tftp server response : " << datagram.toHex();
    40. }
    41. }
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Nov 2010
    Posts
    2
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: tftp with QUdpSocket - cannot do - have to use Q3SocketDevice

    Quote Originally Posted by yvan40 View Post
    perfect!
    Qt Code:
    1. sock->bind(2152); /// \TODO need generate random client TID, and check if port available (select another if not) ; see RFC1350
    To copy to clipboard, switch view to plain text mode 
    No need to worry about available ports if you let bind() handle it for you like this:
    Qt Code:
    1. sock->bind();
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Not getting readyRead from a QUdpSocket
    By evelBist in forum Newbie
    Replies: 3
    Last Post: 20th July 2010, 20:43
  2. Working with QUdpSocket
    By csvivek in forum Qt Programming
    Replies: 6
    Last Post: 30th October 2009, 02:38
  3. QUdpSocket and bytesAvailable
    By babu198649 in forum Newbie
    Replies: 0
    Last Post: 10th December 2008, 11:49
  4. UDP - QUdpSocket
    By denwelzie in forum Qt Programming
    Replies: 7
    Last Post: 29th April 2008, 10:02
  5. QUdpSocket hasPendingDatagrams()
    By mbjerkne in forum Qt Programming
    Replies: 3
    Last Post: 4th April 2006, 20:14

Tags for this Thread

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.