PDA

View Full Version : QUpdSocket The program has unexpectedly finished.



ariad
18th September 2014, 09:07
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


#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QUdpSocket>
#include <QByteArray>
#include <string.h>

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
initSocket();
}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::initSocket()
{
udpSocket = new QUdpSocket(this);
udpSocket->bind(QHostAddress::LocalHost, 7755);

connect(udpSocket, SIGNAL(readyRead()),this, SLOT(readPendingDatagrams()));
emit SIGNAL(readyRead());
}

void MainWindow::readPendingDatagrams()
{
while (udpSocket->hasPendingDatagrams()) {
QByteArray datagram;
datagram.resize(udpSocket->pendingDatagramSize());
QHostAddress sender;
quint16 senderPort;

udpSocket->readDatagram(datagram.data(), datagram.size(),&sender, &senderPort);
processTheDatagram(datagram);
}
}


void MainWindow::processTheDatagram(QByteArray datagram)
{
QDataStream in(&datagram, QIODevice::ReadOnly);

for(int i=0;i<datagram.size();i++){
//ui->label_2->setText(datagram.at(i).toString());
ui->label_2->setText("Recibido");
}
}


Application to write


#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
initSocket();
}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::initSocket()
{
udpSocket = new QUdpSocket(this);
udpSocket->bind(QHostAddress::LocalHost, 7755);

connect(timer, SIGNAL(timeout()), this, SLOT(sendDatagram()));
timer->start(2*1000);
}

void MainWindow::sendDatagram()
{
QByteArray datagram_send;

datagram_send.append("Mensaje 1");
udpSocket->writeDatagram(datagram_send.data(),datagram_send.s ize(),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

anda_skoa
18th September 2014, 10:58
Where does the writing application instantiate the QTimer that it used in initSocket()?

Cheers,
_

ariad
18th September 2014, 12:05
header to write


#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QUdpSocket>
#include <QByteArray>
#include <QTimer>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
void initSocket();

public slots:
void sendDatagram();

private:
Ui::MainWindow *ui;
QUdpSocket *udpSocket;
QTimer *timer;
};

#endif // MAINWINDOW_H





header to read


#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QUdpSocket>
#include <QByteArray>
#include <QTimer>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
void initSocket();

public slots:
void sendDatagram();

private:
Ui::MainWindow *ui;
QUdpSocket *udpSocket;
QTimer *timer;
};

#endif // MAINWINDOW_H


thanks,

anda_skoa
18th September 2014, 12:49
nice, but that does not answer the question.
Again: where do you create the QTimer instance?

Cheers,
_

ariad
18th September 2014, 14:44
That's all! :confused:

anda_skoa
18th September 2014, 14:52
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,
_

ariad
18th September 2014, 15:30
Thanks!!!! I can send and receive!!! :)


Now I have the next problem, but why I can't do this?
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!!

anda_skoa
18th September 2014, 16:13
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,
_

ariad
18th September 2014, 17:06
Ok, thanks!!!