Results 1 to 6 of 6

Thread: Broadcast string with udpsocket

  1. #1
    Join Date
    May 2009
    Location
    Gorontalo
    Posts
    200
    Thanks
    20
    Thanked 5 Times in 5 Posts
    Qt products
    Platforms
    Unix/X11 Windows

    Question Broadcast string with udpsocket

    I'm try broadcast string with udpsocket. But not work


    Server Source Code

    sender.cpp
    Qt Code:
    1. #include "sender.h"
    2.  
    3. Sender::Sender()
    4. {
    5. socket=new QUdpSocket(this);
    6. QTimer *timer=new QTimer(this);
    7. timer->setInterval(100);
    8. timer->start();
    9.  
    10. connect(timer, SIGNAL(timeout()), this, SLOT(siarkan()));
    11. }
    12.  
    13. void Sender::siarkan()
    14. {
    15. QByteArray datagram;
    16. QDataStream out(&datagram, QIODevice::WriteOnly);
    17. out.setVersion(QDataStream::Qt_4_0);
    18. out << "Salawaku" << "Wirasto" << "Slackware";
    19.  
    20. socket->writeDatagram(datagram, QHostAddress::Broadcast, 9988);
    21. }
    To copy to clipboard, switch view to plain text mode 


    main.cpp
    Qt Code:
    1. #include <QtCore/QCoreApplication>
    2. #include "sender.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QCoreApplication a(argc, argv);
    7.  
    8. Sender send;
    9.  
    10. return a.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 



    Client Source Code

    dialog.cpp
    Qt Code:
    1. #include "dialog.h"
    2. #include "ui_dialog.h"
    3. #include <QDebug>
    4.  
    5. Dialog::Dialog(QWidget *parent)
    6. : QDialog(parent), ui(new Ui::Dialog)
    7. {
    8. ui->setupUi(this);
    9.  
    10. socket=new QUdpSocket(this);
    11. socket->bind(9988);
    12.  
    13. connect(socket, SIGNAL(readyRead()), this, SLOT(terimaData()));
    14. }
    15.  
    16. Dialog::~Dialog()
    17. {
    18. delete ui;
    19. }
    20.  
    21. void Dialog::terimaData()
    22. {
    23. QString group, nama, distro;
    24.  
    25.  
    26. while( socket->hasPendingDatagrams() )
    27. {
    28. QByteArray buffer( socket->pendingDatagramSize(), 0 );
    29. socket->readDatagram( buffer.data(), buffer.size() );
    30. QDataStream stream( buffer );
    31. stream.setVersion( QDataStream::Qt_4_0 );
    32. stream >> group >> nama >> distro;
    33. }
    34.  
    35. ui->lineEdit->setText(group);
    36. ui->lineEdit_2->setText(nama);
    37. ui->lineEdit_3->setText(distro);
    38. }
    To copy to clipboard, switch view to plain text mode 

    main.cpp
    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "dialog.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. Dialog w;
    8. w.show();
    9. return a.exec();
    10. }
    To copy to clipboard, switch view to plain text mode 


    What's wrong with my code ?

  2. #2
    Join Date
    Dec 2008
    Location
    TaganrogNativelandChehov,Russia
    Posts
    64
    Thanks
    1
    Thanked 8 Times in 7 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Broadcast string with udpsocket

    Is "readyRead()" emitting?
    east or west home is best

  3. #3
    Join Date
    May 2009
    Location
    Gorontalo
    Posts
    200
    Thanks
    20
    Thanked 5 Times in 5 Posts
    Qt products
    Platforms
    Unix/X11 Windows

    Question Re: Broadcast string with udpsocket

    Is readyRead signal not emitting in my source code ?

    How and where I must emit readyRead signal ?

  4. #4
    Join Date
    Dec 2008
    Location
    TaganrogNativelandChehov,Russia
    Posts
    64
    Thanks
    1
    Thanked 8 Times in 7 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Broadcast string with udpsocket

    >where I must emit readyRead signal ?
    nowhere. Qt makes it.

    you can write:
    Qt Code:
    1. void Dialog::terimaData()
    2. {
    3. qDebug()<<__PRETTY_FUNCTION__;
    4. QString group, nama, distro;
    5. ...
    To copy to clipboard, switch view to plain text mode 
    and see log.
    east or west home is best

  5. #5
    Join Date
    May 2009
    Location
    Gorontalo
    Posts
    200
    Thanks
    20
    Thanked 5 Times in 5 Posts
    Qt products
    Platforms
    Unix/X11 Windows

    Question Re: Broadcast string with udpsocket

    Qt Code:
    1. void Dialog::terimaData()
    2. {
    3. i++;
    4. qDebug() << QString("It's OK %1").arg(i);
    5. .....
    To copy to clipboard, switch view to plain text mode 


    Result :

    It's OK 1
    It's OK 2
    It's OK 3
    It's OK 4
    ........
    ........


    I think readyRead() is emitting...

    So, why I still can't catch string broadcasting ?

  6. #6
    Join Date
    Dec 2008
    Location
    TaganrogNativelandChehov,Russia
    Posts
    64
    Thanks
    1
    Thanked 8 Times in 7 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Broadcast string with udpsocket

    try to use another function to read data
    as:
    QByteArray QIODevice::readAll ()
    qint64 QIODevice::readLine ( char * data, qint64 maxSize )

    etc.
    I think that the problem is QDataStream or QByteArray usage.
    east or west home is best

Similar Threads

  1. Using ANSI string type
    By kahahn in forum Newbie
    Replies: 1
    Last Post: 20th June 2009, 23:52
  2. Replies: 0
    Last Post: 18th February 2009, 13:31
  3. String operations, printing to stdout
    By Cruz in forum Newbie
    Replies: 3
    Last Post: 20th January 2009, 15:30
  4. saving a c string of variable length in a shared memory?
    By nass in forum General Programming
    Replies: 4
    Last Post: 3rd January 2007, 14:40
  5. Create pixmap image from string
    By Morea in forum Qt Programming
    Replies: 5
    Last Post: 17th November 2006, 16:38

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.