{"id":591,"date":"2015-02-10T17:29:31","date_gmt":"2015-02-10T11:59:31","guid":{"rendered":"http:\/\/www.e-consystems.com\/blog\/linux-android\/?p=591"},"modified":"2023-08-15T12:33:58","modified_gmt":"2023-08-15T07:03:58","slug":"qt-network-qudpsocket","status":"publish","type":"post","link":"https:\/\/www.e-consystems.com\/blog\/system-on-module-som\/qt-network-qudpsocket\/","title":{"rendered":"Qt Network &#8211;  QUdpSocket"},"content":{"rendered":"<h2>Introduction:<\/h2>\n<p>This article discusses how to communicate via UDP socket in Qt program.<\/p>\n<p><strong>What is UDP?<\/strong><\/p>\n<p>UDP (User Datagram Protocol) is a lightweight, unreliable, datagram-oriented, connectionless protocol. It can be used when reliability isn&#8217;t important. Some application-level protocols use UDP because it is more lightweight than TCP. With UDP, data is sent as packets (datagrams) from one host to another. There is no concept of connection, and if a UDP packet doesn&#8217;t get delivered successfully, no error is reported to the sender.<\/p>\n<p><strong>What is QUdpSocket ?<\/strong><\/p>\n<p>The QUdpSocket class allows you to send and receive UDP datagrams. QUdpSocket transfers data as datagrams instead of continuous stream of data. QUdpSocket is a subclass of QAbstractSocket that allows you to send and receive UDP datagrams.<\/p>\n<p><strong>How does it work ?<\/strong><\/p>\n<p>The most common way to use QUDPSocket class is to bind to an address and port using bind(), then call writeDatagram() and readDatagram() to transfer data.<\/p>\n<p>1. In main(), we create an instance of UDPSocket class:<br \/>\nUDPSocket client;<br \/>\n2. In the constructor, UDPSocket::UDPSocket(), a QUdpSocket will be created:<br \/>\nsocket = new QUDPSocket(this);<br \/>\n3. Then, we bind to address and port:<br \/>\nsocket-&gt;bind(QHostAddress::LocalHost, 1234);<br \/>\n4. In main(), we call UDPSocket::Hello(), and it actually sends data gram:<br \/>\nsocket-&gt;writeDatagram(Data, QHostAddress::LocalHost, 1234);<br \/>\n5. When the data comes in, we read that datagram in UDPSocket::readyRead():<br \/>\nsocket-&gt;readDatagram(buffer.data(), buffer.size(), &amp;sender, &amp;senderPort);<\/p>\n<p><strong>Sample Application:<\/strong><\/p>\n<p>\/\/main.cpp<\/p>\n<p>#include<br \/>\n#include &#8220;udp.h&#8221;<\/p>\n<p>int main(int argc, char *argv[])<br \/>\n{<br \/>\nQCoreApplication a(argc, argv);<br \/>\nUDPSocket client;<br \/>\nclient.Hello();<br \/>\nreturn a.exec();<br \/>\n}<br \/>\n\/\/udp.h<br \/>\n#ifndef UDP_H<br \/>\n#define UDP_H<\/p>\n<p>#include<br \/>\n#include<\/p>\n<p>class UDPSocket : public QObject<br \/>\n{<br \/>\nQ_OBJECT<br \/>\npublic:<br \/>\nexplicit UDPSocket(QObject *parent = 0);<br \/>\nvoid Hello();<br \/>\nsignals:<\/p>\n<p>public slots:<br \/>\nvoid readyRead();<\/p>\n<p>private:<br \/>\nQUdpSocket *socket;<br \/>\n};<\/p>\n<p>#endif \/\/ UDP_H<br \/>\n\/\/udp.cpp<br \/>\n#include &#8220;udp.h&#8221;<br \/>\nUDPSocket::UDPSocket(QObject *parent) :<br \/>\nQObject(parent)<br \/>\n{<br \/>\n\/\/ create a QUDP socket<br \/>\nsocket = new QUdpSocket(this);<br \/>\n\/\/ The most common way to use QUdpSocket class is to bind to an address and port using bind()<br \/>\nsocket-&gt;bind(QHostAddress::LocalHost, 1234);<br \/>\nconnect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()));<br \/>\n}<\/p>\n<p>void UDPSocket::Hello()<br \/>\n{<br \/>\nQByteArray Data;<br \/>\nData.append(&#8220;Hello from UDP&#8221;);<\/p>\n<p>\/\/ Sends the datagram datagram to the host address and at port.<\/p>\n<p>socket-&gt;writeDatagram(Data, QHostAddress::LocalHost, 1234);<br \/>\n}<br \/>\nvoid UDPSocket::readyRead()<br \/>\n{<br \/>\n\/\/ when data comes in<br \/>\nQByteArray buffer;<br \/>\nbuffer.resize(socket-&gt;pendingDatagramSize());<\/p>\n<p>QHostAddress sender;<br \/>\nquint16 senderPort;<\/p>\n<p>\/\/ Receives a datagram no larger than maxSize bytes and stores it in data.<br \/>\n\/\/ The sender&#8217;s host address and port is stored in *address and *port<\/p>\n<p>socket-&gt;readDatagram(buffer.data(), buffer.size(),<br \/>\n&amp;sender, &amp;senderPort);<br \/>\nqDebug() &lt;&lt; &#8220;Message from: &#8221; &lt;&lt; sender.toString();<br \/>\nqDebug() &lt;&lt; &#8220;Message port: &#8221; &lt;&lt; senderPort;<br \/>\nqDebug() &lt;&lt; &#8220;Message: &#8221; &lt;&lt; buffer;<br \/>\n}<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone\" src=\"https:\/\/www.e-consystems.com\/Blog\/linux-android\/wp-content\/uploads\/2015\/02\/Qt-Network.jpg\" alt=\"\" width=\"680\" height=\"352\" \/><\/p>\n<h2>References<\/h2>\n<p>1. http:\/\/doc.qt.io\/qt-5\/qudpsocket.html#details<\/p>\n<p>For any technical support or queries feel free to write to us at <a href=\"mailto:sales@e-consystems.com\" target=\"_blank\" rel=\"noopener noreferrer\">sales@e-consystems.com<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction: This article discusses how to communicate via UDP socket in Qt program. What is&#8230;<\/p>\n","protected":false},"author":17,"featured_media":744,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[127],"tags":[207,129,130,132,131],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.e-consystems.com\/blog\/system-on-module-som\/wp-json\/wp\/v2\/posts\/591"}],"collection":[{"href":"https:\/\/www.e-consystems.com\/blog\/system-on-module-som\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.e-consystems.com\/blog\/system-on-module-som\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.e-consystems.com\/blog\/system-on-module-som\/wp-json\/wp\/v2\/users\/17"}],"replies":[{"embeddable":true,"href":"https:\/\/www.e-consystems.com\/blog\/system-on-module-som\/wp-json\/wp\/v2\/comments?post=591"}],"version-history":[{"count":10,"href":"https:\/\/www.e-consystems.com\/blog\/system-on-module-som\/wp-json\/wp\/v2\/posts\/591\/revisions"}],"predecessor-version":[{"id":2877,"href":"https:\/\/www.e-consystems.com\/blog\/system-on-module-som\/wp-json\/wp\/v2\/posts\/591\/revisions\/2877"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.e-consystems.com\/blog\/system-on-module-som\/wp-json\/wp\/v2\/media\/744"}],"wp:attachment":[{"href":"https:\/\/www.e-consystems.com\/blog\/system-on-module-som\/wp-json\/wp\/v2\/media?parent=591"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.e-consystems.com\/blog\/system-on-module-som\/wp-json\/wp\/v2\/categories?post=591"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.e-consystems.com\/blog\/system-on-module-som\/wp-json\/wp\/v2\/tags?post=591"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}