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
) : 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");
}
}
#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");
}
}
To copy to clipboard, switch view to plain text mode
Application to write
#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);
}
#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.size(),QHostAddress::LocalHost, 7755);
}
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
Bookmarks