title: Send XML data by UDP
body: <p>I am writing a program that on input sends out a message with some data.  I want to be able to send out a udp message by multicast, and I want to be able to send this message out in an xml format.  Currently my program is written in qt, and if possible I would like to keep it in qt.</p>
  
  <p>From the documentation, I can create and send data with a udp socket by the following:</p>
  
  <pre><code>udpSocket = new QUdpSocket(this);
  QByteArray datagram = "stuff here";
  udpSocket-&gt;writeDatagram(datagram.data(), datagram.size(), groupAddress, port)
  </code></pre>
  
  <p>where the groupaddress is the ip address I am sending the message to, and the port is the port that I am sending it on.</p>
  
  <p>on the other hand, I can use:</p>
  
  <pre><code>QXmlStreamWriter xml;
  xml.setDevice(QIODevice);
  </code></pre>
  
  <p>to send out xml data, and the QIODevice can be either sending data to a file, or sending data out by TCP, and formatting all the data in xml form.</p>
  
  <p>However, I am unable to find a way to set the udpSocket as the device for QXmlStreamWriter and set to to automatically send out that data when it is passed this way.  Is there a way to do this?  Or am I going to have to format everything in a byte array in order to send it out?</p>
  
  <p>Edit:  In case someone else stumbles across this looking for information.
  The method described by Nejat (Listed as the answer) will indeed send xml data by UDP.  However, QXmlStreamWriter ends up sending a very large number of packets, which is not very useful if you are trying to send a single formatted xml block.  If you need to send a formatted xml block, then you will want to do the following:</p>
  
  <p>Step 1: create your udp socket</p>
  
  <pre><code>udpSocket = new QUdpSocket(this);
  </code></pre>
  
  <p>Step 2: create a QByteArray, and set it as the "device" that QXmlStreamWriter is writing into.</p>
  
  <pre><code>QByteArray message;
  QXmlStreamWriter xml(&amp;message);
  //proceed to write the xml like you would normally
  </code></pre>
  
  <p>Step 3: send your QByteArray message like the documentation states.</p>
  
  <pre><code>udpSocket-&gt;writeDatagram(message.data(), message.size(), groupAddress, port);
  </code></pre>
  
  <p>Doing it this way will create a single, large packet, then send that packet via UDP messages.  It is up to you to make sure that your packet is small enough that it won't be broken up by the routing on its way to its destination.</p>