Results 1 to 9 of 9

Thread: Problem with a UDP connection between two programs

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Dec 2008
    Location
    France
    Posts
    6
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Problem with a UDP connection between two programs

    I have two programs, A and B, who communicate by an UDP connection. They can beings on the same computer or two computers on a network.
    The A program sends a command to the B program, which processes this command and sends then an answer to the A program.
    Originally both programs were written in C# (WinXP). The A program was rewritten with Qt/C++, and it works well with the C# version of B (WinXP).
    But I don't manage to put on the UDP connection with a Qt/C++ version of B.
    Part of the A program
    Qt Code:
    1. class radioInterfaceThread : public QThread
    2. {
    3. Q_OBJECT
    4.  
    5. QUdpSocket *udpCommandSocket;
    6.  
    7. public:
    8. radioInterfaceThread(QObject *parent = 0);
    9. ~radioInterfaceThread();
    10. void setConnectUDP();
    11. protected:
    12. void run();
    13. private slots:
    14. void udpRxEvent();
    15. void sendCommand();
    16. };
    17.  
    18.  
    19. radioInterfaceThread::radioInterfaceThread(QObject *parent)
    20. : QThread(parent)
    21. , udpCommandSocket(0)
    22. {
    23. udpCommandSocket = new QUdpSocket;
    24. connect(udpCommandSocket, SIGNAL(readyRead()), this, SLOT(udpRxEvent()), Qt::DirectConnection);
    25. }
    26.  
    27. void radioInterfaceThread::run()
    28. {
    29. ....
    30. exec();
    31. ....
    32. }
    33.  
    34.  
    35. void radioInterfaceThread::setConnectTCP(QString addr, quint16 port)
    36. {
    37. udpCommandSocket->connectToHost(QHostAddress(addr), port, QAbstractSocket::ReadWrite);
    38. }
    39.  
    40.  
    41. void radioInterfaceThread::udpRxEvent()
    42. {
    43. qint64 bytes_available = 0;
    44.  
    45. while ((bytes_available = udpCommandSocket->bytesAvailable()) > 0)
    46. {
    47. QByteArray datagram;
    48. datagram.resize(bytes_available);
    49.  
    50. qint64 bytes_read = udpCommandSocket->readDatagram(datagram.data(), datagram.size());
    51. //... process the answer
    52. }
    53. }
    54.  
    55. // slot called to send Command
    56. void radioInterfaceThread::sendCommand(QString cmd)
    57. {
    58. qint64 bytes_sent = 0;
    59.  
    60. //...
    61. bytes_sent = udpCommandSocket->write(QString(cmd.trimmed() + "\r\n").toLatin1().constData());
    62. //...
    63. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problem with a UDP connection between two programs

    Iirc, you should not declare signals/slots in a QThread subclass. (Those won't be executed in the thread's context!)

    On the other hand, I fail to see what the thread is for at all: signals/slots are asynchronous by nature, so a regular QObject-subclass (instead of the thread should work!)

    Are you calling setConnectUDP() somewhere? Do you wait for the connection to be established before you start sending?

  3. #3
    Join Date
    Dec 2008
    Location
    France
    Posts
    6
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: Problem with a UDP connection between two programs

    Quote Originally Posted by caduel View Post
    Iirc, you should not declare signals/slots in a QThread subclass. (Those won't be executed in the thread's context!)

    On the other hand, I fail to see what the thread is for at all: signals/slots are asynchronous by nature, so a regular QObject-subclass (instead of the thread should work!)

    Are you calling setConnectUDP() somewhere? Do you wait for the connection to be established before you start sending?
    I have created a little A program with a GUI and a "Send" button. The setConnectUDP() call is made in the Constructor of the windows's GUI. A click on the "send" button call the "sendCommand" slot, so no problem of wait!

    Remember, this A program work fine with C# B program!

    I also tried with a version without thread, with the same result.

  4. #4
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problem with a UDP connection between two programs

    Maybe the problem is with your "B" program, then?
    (If you strip down the program(s)) and provide us compilable (ideallly minimal examples) we are better able to help without guessing what you are doing :-)

  5. #5
    Join Date
    Dec 2008
    Location
    France
    Posts
    6
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: Problem with a UDP connection between two programs

    The B program need an hardware board (it's the "Server" beetween the board and the GUI in a "Software Defined Radio" system)
    So, no simple to give you a B compilable version!
    But can be it's not necessary.
    What I want to realize is that an A program sends (by an UDP connection) a request to the B program (that it is simple!) but after, the B program returns an answer to the A program. There, I found nothing of complete and functional. In the Qt's documentation, it is written :
    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().
    nothing more, and without real example! All that I tried does not work

  6. #6
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problem with a UDP connection between two programs

    a) You should separate the hardware dependent code from the network/server layer.
    That way you can test the server part independently from the hardware part.

    b) Have you tried the code at QUdpSocket? Does your "read-slot" get called? Ever?

  7. #7
    Join Date
    Dec 2008
    Location
    France
    Posts
    6
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: Problem with a UDP connection between two programs

    OK, find attached files Test_A and Test_B in Test_A_B.zip
    The slot " udpRxEvent() " of the program A is never called.
    Attached Files Attached Files

  8. #8
    Join Date
    Dec 2008
    Location
    France
    Posts
    6
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: Problem with a UDP connection between two programs

    Here is the C# version of B program (without hardware dependent code)
    You can verify that the program A (Qt) works perfectly with this program B (C#)
    Attached Files Attached Files

  9. #9
    Join Date
    Dec 2008
    Location
    France
    Posts
    6
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: Problem with a UDP connection between two programs

    I finaly found solution all is on the run()
    Qt Code:
    1. QTextStream out( stdout );
    2.  
    3. QUdpSocket m_socket;
    4. QByteArray datagram;
    5. QHostAddress sender;
    6. quint16 senderPort;
    7. QString in_qstr;
    8. QString resp;
    9.  
    10. if (m_socket.bind(QHostAddress::Any, 55667) == -1) {
    11. qDebug("Could not bind udp command_server socket...");
    12. m_thread_go = false;
    13. }
    14. else
    15. m_thread_go = true;
    16.  
    17. while (m_thread_go) {
    18.  
    19. if (m_socket.waitForReadyRead(100)) {
    20. if (m_socket.hasPendingDatagrams()) {
    21. datagram.resize(m_socket.pendingDatagramSize());
    22. m_socket.readDatagram(datagram.data(), datagram.size(), &sender, &senderPort);
    23. in_qstr = datagram.data();
    24.  
    25. resp = ProcessCommand(in_qstr.toLower());
    26.  
    27. datagram.clear();
    28. datagram.append(resp);
    29.  
    30. if (m_socket.writeDatagram(datagram, sender, senderPort) == -1) {
    31. qDebug("dropped at write");
    32. m_socket.close();
    33. return;
    34. }
    35. if (resp == "_exit_") {
    36. m_socket.close();
    37. m_thread_go = false;
    38. emit exitCommand();
    39. }
    40. m_socket.flush();
    41. }
    42. }
    43. }
    44. m_socket.close();
    45. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. DB Connection Problem
    By ktmdwn in forum Qt Programming
    Replies: 13
    Last Post: 4th August 2010, 15:12
  2. SQL connection closure problem.
    By cbarmpar in forum Qt Programming
    Replies: 1
    Last Post: 8th September 2008, 08:42
  3. Client/Server Error: BadIDChoice
    By 3nc31 in forum Qt Programming
    Replies: 5
    Last Post: 27th November 2007, 10:22
  4. Problem about timer in multithread programs
    By vql in forum General Programming
    Replies: 4
    Last Post: 17th October 2007, 15:00
  5. [QMYSQL] connection problem
    By chaos_theory in forum Installation and Deployment
    Replies: 5
    Last Post: 2nd July 2007, 09:52

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
  •  
Qt is a trademark of The Qt Company.