Home | All Classes | Main Classes | Annotated | Grouped Classes | Functions |

# sender.cpp Example File
network/broadcastsender/sender.cpp

        #include <QtCore>
        #include <QtGui>
        #include <QtNetwork>

        #include "sender.h"

        Sender::Sender(QWidget *parent)
            : QDialog(parent)
        {
            statusLabel = new QLabel(tr("Ready to broadcast datagrams on port 45454"),
                                     this);
            startButton = new QPushButton(tr("&Start"), this);
            quitButton = new QPushButton(tr("&Quit"), this);
            timer = new QTimer(this);
            udpSocket = new QUdpSocket(this);
            messageNo = 1;

            connect(startButton, SIGNAL(clicked()), this, SLOT(startBroadcasting()));
            connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
            connect(timer, SIGNAL(timeout()), this, SLOT(broadcastDatagram()));

            QHBoxLayout *buttonLayout = new QHBoxLayout;
            buttonLayout->addStretch(1);
            buttonLayout->addWidget(startButton);
            buttonLayout->addWidget(quitButton);

            QVBoxLayout *mainLayout = new QVBoxLayout(this);
            mainLayout->addWidget(statusLabel);
            mainLayout->addLayout(buttonLayout);

            setWindowTitle(tr("Broadcast Sender"));
        }

        void Sender::startBroadcasting()
        {
            startButton->setEnabled(false);
            timer->start(1000);
        }

        void Sender::broadcastDatagram()
        {
            statusLabel->setText(tr("Now broadcasting datagram %1").arg(messageNo));
            QByteArray datagram = "Broadcast message " + QByteArray::number(messageNo);
            udpSocket->writeDatagram(datagram.data(), datagram.size(),
                                     QHostAddress::Broadcast, 45454);
            ++messageNo;
        }