Hello everybody. I have been strugling with something since several weeks already, as I still havent understood entyrely the behaviour of UDP socket programming and threads.
I must create a server that reads the pending datagram from the client (it is a robot from KUKA). However I need to reply this package within 11 ms with a previously made string taken from an xml file. The package repplied must mirror the timstamp received in the message from the client. So far, so good (at least that is what I think). Using several tutorial, I finally manage to do that code. The problem arises when I try either to read the package received from the client or to write something different in the datagram to send to the client. Whenever I write a line of code that slows down the program a bit, the communication is broken and the robot disconnects as it did not have an answer within that time.

I have tried to use Qthread in different ways, but it seems that I havent understood it at all. What ever I do ends up in stopping the robot or crashing my program.

Here is what I have so far. I mean the "working implementation".

myudp.h
Qt Code:
  1. #ifndef MYUDP_H
  2. #define MYUDP_H
  3.  
  4. #include <QObject>
  5. #include <QUdpSocket>
  6. #include <QtXml>
  7. #include <QTextStream>
  8. #include <QDebug>
  9. #include <mythread.h>
  10.  
  11. class MyUDP : public QObject
  12. {
  13. Q_OBJECT
  14. QByteArray IPOCnumber;
  15. QString SendXML;
  16. QByteArray SendXML_b;
  17. QHostAddress sender;
  18. quint16 senderPort;
  19. QByteArray Buffer,dataReceived;
  20.  
  21. QMutex mtxSender,mtxReceiver;
  22.  
  23.  
  24. public:
  25. explicit MyUDP(QObject *parent = 0);
  26. void readFile(QString file);
  27. void CopyIPOC();
  28.  
  29.  
  30.  
  31.  
  32. signals:
  33. void started();
  34.  
  35. public slots:
  36. void readPendingDatagram();
  37. void SendUDP();
  38. void writeReceivedData();
  39. private:
  40. QUdpSocket *socket;
  41. };
  42.  
  43. #endif // MYUDP_H
To copy to clipboard, switch view to plain text mode 

my udp.cpp

Qt Code:
  1. #include "myudp.h"
  2. #include <thread>
  3.  
  4. MyUDP::MyUDP(QObject *parent) : QObject(parent)
  5. {
  6.  
  7. // QHostAddress receiver;
  8. // receiver = "192.168.1.11"; //Kuka computer 192.198.1.101
  9. SendXML_b.clear();
  10. socket = new QUdpSocket(this);
  11. socket->bind(QHostAddress("192.168.1.11"),6050);
  12. connect(socket, SIGNAL(readyRead()),this,SLOT(readPendingDatagram()));
  13. qDebug() <<"Socket binded";
  14.  
  15.  
  16. }
  17.  
  18.  
  19.  
  20. void MyUDP::readPendingDatagram(){
  21.  
  22.  
  23. mtxReceiver.lock();
  24. Buffer.resize(socket->pendingDatagramSize());
  25. socket->readDatagram(Buffer.data(),Buffer.size(),&sender,&senderPort);//collects information
  26. //emit started();
  27. mtxReceiver.unlock();
  28. CopyIPOC();
  29. SendUDP();
  30.  
  31.  
  32.  
  33. }
  34. void MyUDP::readFile(QString file){
  35. SendXML = file;
  36.  
  37. }
  38.  
  39. void MyUDP::CopyIPOC(){
  40.  
  41. //Reading IPOC positions
  42. int beginsAt = Buffer.indexOf("<IPOC>") + 6;
  43. int stopsAt = Buffer.indexOf("</IPOC>");
  44. //Copying IPOC
  45. IPOCnumber = Buffer.mid(beginsAt,stopsAt-beginsAt);
  46. //finding where to insert
  47. SendXML_b.append(SendXML);
  48. beginsAt = SendXML_b.indexOf("<IPOC>") + 6;
  49. stopsAt = SendXML_b.indexOf("</IPOC>");
  50.  
  51. SendXML_b.remove(beginsAt,stopsAt-beginsAt);
  52. SendXML_b.insert(beginsAt,IPOCnumber);
  53.  
  54. }
  55.  
  56. void MyUDP::SendUDP(){
  57.  
  58. // qDebug() << "Message senT: " << SendXML_b;
  59.  
  60. socket->writeDatagram(SendXML_b, sender, senderPort);
  61. SendXML_b.clear();
  62.  
  63. }
  64.  
  65. void MyUDP::writeReceivedData(){
  66. mtxReceiver.lock();
  67. qDebug()<< Buffer;
  68. mtxReceiver.unlock();
  69. }
To copy to clipboard, switch view to plain text mode 

and finally main .cpp
Qt Code:
  1. #include <QCoreApplication>
  2. #include "myudp.h"
  3. #include "thread"
  4. int main(int argc, char *argv[])
  5. {
  6. QCoreApplication a(argc, argv);
  7. QString SendXML;
  8. // Open a file for reading
  9. QFile file("C:/Users/admin/Desktop/clein/Example/RealtimeEthernet/Server_app/ExternalData.xml");
  10. if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
  11. {
  12. qDebug() << "Failed to open the file for reading.";
  13.  
  14. }
  15. else
  16. {
  17. QTextStream in(&file);
  18. SendXML = in.readAll();
  19.  
  20. file.close();
  21.  
  22. }
  23. qDebug() << "File in External XML:\n"<<SendXML;
  24. // QByteArray ExternalFile = document.toByteArray(0);
  25. MyUDP Server; //receives by UDP
  26. Server.readFile(SendXML);
  27. qDebug() <<"Program started: ";
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34. return a.exec();
  35. }
To copy to clipboard, switch view to plain text mode 

I would really appreaciate it if someone can help me to solve my problem. Thank you!