Results 1 to 3 of 3

Thread: QUdpSocket transmission

  1. #1
    Join Date
    Oct 2017
    Posts
    2
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Windows

    Default QUdpSocket transmission

    I try to run QT example "Broadcast Sender Example". (http://doc.qt.io/qt-5/qtnetwork-broa...r-example.html).
    It run without errors, but datagrams are not sended. And I have messages "QNativeSocketEngine::writeDatagram() was called not in QAbstractSocket::BoundState or QAbstractSocket::ConnectedState" in Application Output tab. Can anybode help me&

  2. #2
    Join Date
    Feb 2012
    Location
    Warsaw, Poland
    Posts
    37
    Thanks
    3
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: QUdpSocket transmission

    Show your code

  3. #3
    Join Date
    Oct 2017
    Posts
    2
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Windows

    Default Re: QUdpSocket transmission

    sender.h

    #ifndef SENDER_H
    #define SENDER_H

    #include <QWidget>

    QT_BEGIN_NAMESPACE
    class QDialogButtonBox;
    class QLabel;
    class QPushButton;
    class QTimer;
    class QUdpSocket;
    QT_END_NAMESPACE

    class Sender : public QWidget
    {
    Q_OBJECT

    public:
    Sender(QWidget *parent = 0);

    private slots:
    void startBroadcasting();
    void broadcastDatagram();

    private:
    QLabel *statusLabel;
    QPushButton *startButton;
    QPushButton *quitButton;
    QDialogButtonBox *buttonBox;
    QUdpSocket *udpSocket;
    QTimer *timer;
    int messageNo;
    };

    #endif

    --------------------------------------------------------

    main.cpp

    #include <QApplication>

    #include "sender.h"

    int main(int argc, char *argv[])
    {
    QApplication app(argc, argv);
    Sender sender;
    sender.show();
    return app.exec();
    }

    ---------------------------------------------------------------

    sender.cpp

    #include <QtWidgets>
    #include <QtNetwork>

    #include "sender.h"

    Sender::Sender(QWidget *parent)
    : QWidget(parent)
    {
    statusLabel = new QLabel(tr("Ready to broadcast datagrams on port 45454"));
    statusLabel->setWordWrap(true);

    startButton = new QPushButton(tr("&Start"));
    quitButton = new QPushButton(tr("&Quit"));

    buttonBox = new QDialogButtonBox;
    buttonBox->addButton(startButton, QDialogButtonBox::ActionRole);
    buttonBox->addButton(quitButton, QDialogButtonBox::RejectRole);

    timer = new QTimer(this);
    //! [0]
    udpSocket = new QUdpSocket(this);
    //! [0]
    messageNo = 1;

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

    QVBoxLayout *mainLayout = new QVBoxLayout;
    mainLayout->addWidget(statusLabel);
    mainLayout->addWidget(buttonBox);
    setLayout(mainLayout);

    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));
    //! [1]
    QByteArray datagram = "Broadcast message " + QByteArray::number(messageNo);
    udpSocket->writeDatagram(datagram.data(), datagram.size(),
    QHostAddress::Broadcast, 45454);
    //! [1]
    ++messageNo;
    }

Similar Threads

  1. Unicast with QUdpSocket
    By bryang in forum Qt Programming
    Replies: 1
    Last Post: 15th February 2011, 05:29
  2. Not getting readyRead from a QUdpSocket
    By evelBist in forum Newbie
    Replies: 3
    Last Post: 20th July 2010, 20:43
  3. Null character transmission in QTcpSocket
    By Manish.S in forum Qt Programming
    Replies: 4
    Last Post: 18th June 2010, 08:31
  4. UDP - QUdpSocket
    By denwelzie in forum Qt Programming
    Replies: 7
    Last Post: 29th April 2008, 10:02
  5. how to transmission a big file use the QtNetwork
    By fengtian.we in forum Qt Programming
    Replies: 9
    Last Post: 26th March 2007, 15:17

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.