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
Code:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QUdpSocket>
#include <QByteArray>
#include <string.h>
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
initSocket();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::initSocket()
{
connect(udpSocket, SIGNAL(readyRead()),this, SLOT(readPendingDatagrams()));
emit SIGNAL(readyRead());
}
void MainWindow::readPendingDatagrams()
{
while (udpSocket->hasPendingDatagrams()) {
datagram.resize(udpSocket->pendingDatagramSize());
quint16 senderPort;
udpSocket->readDatagram(datagram.data(), datagram.size(),&sender, &senderPort);
processTheDatagram(datagram);
}
}
void MainWindow
::processTheDatagram(QByteArray datagram
) {
for(int i=0;i<datagram.size();i++){
//ui->label_2->setText(datagram.at(i).toString());
ui->label_2->setText("Recibido");
}
}
Application to write
Code:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
initSocket();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::initSocket()
{
connect(timer, SIGNAL(timeout()), this, SLOT(sendDatagram()));
timer->start(2*1000);
}
void MainWindow::sendDatagram()
{
datagram_send.append("Mensaje 1");
udpSocket
->writeDatagram
(datagram_send.
data(),datagram_send.
size(),
QHostAddress::LocalHost,
7755);
}
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
Re: QUpdSocket The program has unexpectedly finished.
Where does the writing application instantiate the QTimer that it used in initSocket()?
Cheers,
_
Re: QUpdSocket The program has unexpectedly finished.
header to write
Code:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QUdpSocket>
#include <QByteArray>
#include <QTimer>
namespace Ui {
class MainWindow;
}
{
Q_OBJECT
public:
explicit MainWindow
(QWidget *parent
= 0);
~MainWindow();
void initSocket();
public slots:
void sendDatagram();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
header to read
Code:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QUdpSocket>
#include <QByteArray>
#include <QTimer>
namespace Ui {
class MainWindow;
}
{
Q_OBJECT
public:
explicit MainWindow
(QWidget *parent
= 0);
~MainWindow();
void initSocket();
public slots:
void sendDatagram();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
thanks,
Re: QUpdSocket The program has unexpectedly finished.
nice, but that does not answer the question.
Again: where do you create the QTimer instance?
Cheers,
_
Re: QUpdSocket The program has unexpectedly finished.
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,
_
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?
Code:
ui->label_2->setText(datagram.at(i).toString());
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!!
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,
_
Re: QUpdSocket The program has unexpectedly finished.