Results 1 to 9 of 9

Thread: QUpdSocket The program has unexpectedly finished.

  1. #1
    Join Date
    Sep 2014
    Location
    Spain
    Posts
    14
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default QUpdSocket The program has unexpectedly finished.

    Hello,
    VirtualBox, openSUSE13.1, Qt Creator 2.8 Qt4.8
    I want to receive data to other machine to my application but now I want to check and I have a application to write a message and another to read messages, but the application to write unexpectedly finished.

    In the .h:

    QUdpSocket *udpSocket;
    QTimer *timer;


    Application to read
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <QUdpSocket>
    4. #include <QByteArray>
    5. #include <string.h>
    6.  
    7. MainWindow::MainWindow(QWidget *parent) :
    8. QMainWindow(parent),
    9. ui(new Ui::MainWindow)
    10. {
    11. ui->setupUi(this);
    12. initSocket();
    13. }
    14.  
    15. MainWindow::~MainWindow()
    16. {
    17. delete ui;
    18. }
    19.  
    20. void MainWindow::initSocket()
    21. {
    22. udpSocket = new QUdpSocket(this);
    23. udpSocket->bind(QHostAddress::LocalHost, 7755);
    24.  
    25. connect(udpSocket, SIGNAL(readyRead()),this, SLOT(readPendingDatagrams()));
    26. emit SIGNAL(readyRead());
    27. }
    28.  
    29. void MainWindow::readPendingDatagrams()
    30. {
    31. while (udpSocket->hasPendingDatagrams()) {
    32. QByteArray datagram;
    33. datagram.resize(udpSocket->pendingDatagramSize());
    34. QHostAddress sender;
    35. quint16 senderPort;
    36.  
    37. udpSocket->readDatagram(datagram.data(), datagram.size(),&sender, &senderPort);
    38. processTheDatagram(datagram);
    39. }
    40. }
    41.  
    42.  
    43. void MainWindow::processTheDatagram(QByteArray datagram)
    44. {
    45. QDataStream in(&datagram, QIODevice::ReadOnly);
    46.  
    47. for(int i=0;i<datagram.size();i++){
    48. //ui->label_2->setText(datagram.at(i).toString());
    49. ui->label_2->setText("Recibido");
    50. }
    51. }
    To copy to clipboard, switch view to plain text mode 

    Application to write
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4. MainWindow::MainWindow(QWidget *parent) :
    5. QMainWindow(parent),
    6. ui(new Ui::MainWindow)
    7. {
    8. ui->setupUi(this);
    9. initSocket();
    10. }
    11.  
    12. MainWindow::~MainWindow()
    13. {
    14. delete ui;
    15. }
    16.  
    17. void MainWindow::initSocket()
    18. {
    19. udpSocket = new QUdpSocket(this);
    20. udpSocket->bind(QHostAddress::LocalHost, 7755);
    21.  
    22. connect(timer, SIGNAL(timeout()), this, SLOT(sendDatagram()));
    23. timer->start(2*1000);
    24. }
    25.  
    26. void MainWindow::sendDatagram()
    27. {
    28. QByteArray datagram_send;
    29.  
    30. datagram_send.append("Mensaje 1");
    31. udpSocket->writeDatagram(datagram_send.data(),datagram_send.size(),QHostAddress::LocalHost, 7755);
    32. }
    To copy to clipboard, switch view to plain text mode 
    Maybe to check I don't need another application to check and only a method to write and call it when I initialize the QUpdSocket.
    Any idea?? thanks
    Last edited by anda_skoa; 18th September 2014 at 10:56. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QUpdSocket The program has unexpectedly finished.

    Where does the writing application instantiate the QTimer that it used in initSocket()?

    Cheers,
    _

  3. #3
    Join Date
    Sep 2014
    Location
    Spain
    Posts
    14
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: QUpdSocket The program has unexpectedly finished.

    header to write

    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include <QUdpSocket>
    6. #include <QByteArray>
    7. #include <QTimer>
    8.  
    9. namespace Ui {
    10. class MainWindow;
    11. }
    12.  
    13. class MainWindow : public QMainWindow
    14. {
    15. Q_OBJECT
    16.  
    17. public:
    18. explicit MainWindow(QWidget *parent = 0);
    19. ~MainWindow();
    20. void initSocket();
    21.  
    22. public slots:
    23. void sendDatagram();
    24.  
    25. private:
    26. Ui::MainWindow *ui;
    27. QUdpSocket *udpSocket;
    28. QTimer *timer;
    29. };
    30.  
    31. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 




    header to read

    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include <QUdpSocket>
    6. #include <QByteArray>
    7. #include <QTimer>
    8.  
    9. namespace Ui {
    10. class MainWindow;
    11. }
    12.  
    13. class MainWindow : public QMainWindow
    14. {
    15. Q_OBJECT
    16.  
    17. public:
    18. explicit MainWindow(QWidget *parent = 0);
    19. ~MainWindow();
    20. void initSocket();
    21.  
    22. public slots:
    23. void sendDatagram();
    24.  
    25. private:
    26. Ui::MainWindow *ui;
    27. QUdpSocket *udpSocket;
    28. QTimer *timer;
    29. };
    30.  
    31. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 


    thanks,

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QUpdSocket The program has unexpectedly finished.

    nice, but that does not answer the question.
    Again: where do you create the QTimer instance?

    Cheers,
    _

  5. #5
    Join Date
    Sep 2014
    Location
    Spain
    Posts
    14
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: QUpdSocket The program has unexpectedly finished.

    That's all!

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QUpdSocket The program has unexpectedly finished.

    What I was trying to hit at is that you need to create an instance of QTimer if you want to use it
    Right now you are using an uninitialized pointer.

    Cheers,
    _

  7. #7
    Join Date
    Sep 2014
    Location
    Spain
    Posts
    14
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: QUpdSocket The program has unexpectedly finished.

    Thanks!!!! I can send and receive!!!


    Now I have the next problem, but why I can't do this?
    Qt Code:
    1. ui->label_2->setText(datagram.at(i).toString());
    To copy to clipboard, switch view to plain text mode 

    Build:
    Request for member 'toString' in 'datagram.QByteArray::at(i), which is of non-class type char
    ui->label_2->setText(datagram.at(i).toString());

    Thanks another time!!
    Last edited by ariad; 18th September 2014 at 15:36.

  8. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QUpdSocket The program has unexpectedly finished.

    QByteArray::at() returns a char, a single character. It is not a class and does not have any methods.

    If you want to display the string inside the byte array, use one of the QString::fromXYZ methods, e.g. QString::fromAscii()

    Cheers,
    _

  9. #9
    Join Date
    Sep 2014
    Location
    Spain
    Posts
    14
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: QUpdSocket The program has unexpectedly finished.

    Ok, thanks!!!

Similar Threads

  1. The program has unexpectedly finished.
    By smemamian in forum Newbie
    Replies: 4
    Last Post: 1st April 2013, 00:44
  2. Replies: 4
    Last Post: 17th September 2012, 15:23
  3. the program has unexpectedly finished
    By narlapavan in forum Qt Programming
    Replies: 9
    Last Post: 9th July 2012, 09:04
  4. Replies: 6
    Last Post: 24th June 2012, 18:32
  5. Program has unexpectedly finished
    By Maluko_Da_Tola in forum Newbie
    Replies: 5
    Last Post: 1st December 2010, 09:54

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.