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