QT UDPSocket
-
Sir,
I am using udpsocket to send and receive data. I have two application one for sending, another to receive the same.
The data I am sending is nothing but Images.But my problem is that the data is sending but not receiving and creates an error in the receiver end as "Corrupt JPEG data: 1 extraneous bytes before marker 0xd9
JPEG datastream contains no image"MY CODES
SENDER@#include "udpsocket.h"
#include <QTime>
#include<QBuffer>
#include <QImage>
#include<QPixmap>QHostAddress ad= QHostAddress("192.168.137.1");
Udpsocket::Udpsocket(QObject *parent) :
QObject(parent)
{
socket=new QUdpSocket(this);
socket->bind(ad,1234);
QImage image;
if(!image.load("1.JPG","JPG"))
qDebug()<<"failed";
else
qDebug()<<"succes";QByteArray ba; QBuffer buffer(&ba); if(image.save(&buffer,"JPG")) qDebug()<<"done"<<ba.size(); socket->writeDatagram(ba.data(),QHostAddress::Broadcast,1234);
}
@RECEIVER
@#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPixmap>MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
socket=new QUdpSocket(this);socket->bind(1234,QUdpSocket::ShareAddress); connect(socket,SIGNAL(readyRead()),this,SLOT(displayData()));
}
MainWindow::~MainWindow()
{
delete ui;
}void MainWindow::displayData()
{qDebug()<<"Received"; QPixmap p; QByteArray buffer; QString string; while (socket->hasPendingDatagrams()) { buffer.resize(socket->pendingDatagramSize()); QHostAddress sender; quint16 senderPort; if(!socket->readDatagram(buffer.data(), buffer.size(), &sender, &senderPort)) qDebug()<<"error"<<socket->errorString(); if(!p.loadFromData(buffer.data(),"JPG")) qDebug()<<"failed"<<buffer.size();; ui->label->setWordWrap(true); ui->label->setText(tr( "Message from: ")+ sender.toString()+ tr( "\nMessage port: " )+ string.append(QString("%1").arg(senderPort))+ tr("\nMessage: ") + buffer);
ui->label->setPixmap(p);
}
}
@Please reply the solution.. I need that for my project.
Thanks in advance -
Maximum message size for UDP is very small and varies between platforms. You need to either chop your transmission into small pieces (you need to test how much works for you; and consult the documentation to know the safe limits), and/ or use TCP protocol. It handles bigger amounts of data more reliably.
-
But my ultimate aim is to send videos in the place of images.
Whether TCPSocket supports sending videos with high fps.
Can you please share the sample code or logic for sending videos at high fps. -
Keeping with the original question, and the data is not too big, I would suggest that you not make use of the writeDatagram() function you are using
Instead of what you have:
@
socket->writeDatagram(ba.data(),QHostAddress::Broadcast,1234);
@Call rather one of the following:
@
socket->writeDatagram(ba.data(),ba.size(),QHostAddress::Broadcast,1234);
// OR
socket->writeDatagram(ba,QHostAddress::Broadcast,1234);
@The difference is small but I think the effect is a problem. In the one that you call, I am assuming that the call would take the ba.data() and create a QByteArray using the constructor that stops on the first '\0' byte. Which would definitely be the wrong data.
If you call either of the 2 options at least you are sure the correct amount of data will get transmitted.
Look at "this":http://qt-project.org/forums/viewthread/47927/ thread for some more information regarding this problem.
-
Also a general comment: Always check the size that you write to any stream to make sure that the amount of data is the same as the amount that you wanted to write.
@
qint64 bytesWritten = socket->writeDatagram(ba,QHostAddress::Broadcast,1234);
if(bytesWritten != ba.size()) {
// Not all bytes were written, do something
}
@