Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QT UDPSocket
Forum Updated to NodeBB v4.3 + New Features

QT UDPSocket

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 2.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    aniljoby
    wrote on 5 Oct 2014, 11:35 last edited by
    #1

    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

    1 Reply Last reply
    0
    • S Offline
      S Offline
      sierdzio
      Moderators
      wrote on 5 Oct 2014, 12:21 last edited by
      #2

      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.

      (Z(:^

      1 Reply Last reply
      0
      • A Offline
        A Offline
        aniljoby
        wrote on 5 Oct 2014, 15:15 last edited by
        #3

        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.

        1 Reply Last reply
        0
        • T Offline
          T Offline
          TheBadger
          wrote on 6 Oct 2014, 08:57 last edited by
          #4

          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.


          Check out my SpellChecker Plugin for Qt Creator @ https://github.com/CJCombrink/SpellChecker-Plugin

          1 Reply Last reply
          0
          • T Offline
            T Offline
            TheBadger
            wrote on 6 Oct 2014, 09:04 last edited by
            #5

            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
            }
            @


            Check out my SpellChecker Plugin for Qt Creator @ https://github.com/CJCombrink/SpellChecker-Plugin

            1 Reply Last reply
            0

            1/5

            5 Oct 2014, 11:35

            • Login

            • Login or register to search.
            1 out of 5
            • First post
              1/5
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved