Unsolved How to Share Any File Using QUdpSocket
-
Hello Experts
I Want To Share Any File Like (Image File , etc..) Through QUdpSocket
Please Help Me To Solve This ProblemMy Server Side Code :
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); socket = new QUdpSocket(this); //////////////////////// Get Image From Client ///////////////////////////////// connect(socket ,&QUdpSocket::readyRead, [&]() { if(socket->hasPendingDatagrams()) { QByteArray Datagram = socket->readAll(); QFile target; target.setFileName("D:/1.jpg"); if (!target.open(QIODevice::WriteOnly | QIODevice::Append)) { qDebug() << "Can't open file for written"; return; } target.write(Datagram); target.close(); QHostAddress sender; quint16 senderPort; Datagram.resize(socket->pendingDatagramSize()); socket->readDatagram(Datagram.data(), Datagram.size(), &sender, &senderPort); ui->textEdit->setText(QString(Datagram)); } }); } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_pushButton_clicked() { //////////////////////// Send Image To Client ///////////////////////////////// QFile file("D:/Image.jpg"); if(!file.open(QIODevice::ReadWrite)) qDebug() << "Can't Send"; auto datagram = file.readAll(); socket->bind(ui->spinBox->value(), QUdpSocket::ShareAddress); socket->writeDatagram(datagram, QHostAddress(ui->txt_Ip->text()), ui->spinBox->value()); }
My Client Side Code :
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); socket = new QUdpSocket(this); ////////////////////////////// Read Image From Server //////////////////////// connect(socket ,&QUdpSocket::readyRead, [&]() { if(socket->hasPendingDatagrams()) { QByteArray Datagram = socket->readAll(); QFile target; target.setFileName("/home/pi/Desktop/Test.jpg"); if (!target.open(QIODevice::WriteOnly | QIODevice::Append)) { qDebug() << "Can't open file for written"; return; } target.write(Datagram); target.close(); QHostAddress sender; quint16 senderPort; Datagram.resize(socket->pendingDatagramSize()); socket->readDatagram(Datagram.data(), Datagram.size(), &sender, &senderPort); ui->textEdit->setText(QString(Datagram)); } }); } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_pushButton_clicked() { //////////////////////////////////////////// Send Image To Server ////////////////////////////// QFile file("/home/pi/Desktop/Images/Vision_2.jpg"); if(!file.open(QIODevice::ReadWrite)) qDebug() << "Can't Send"; auto datagram = file.readAll(); socket->bind(ui->spinBox->value(), QUdpSocket::ShareAddress); socket->writeDatagram(datagram, QHostAddress("***.***.**.**"), ui->spinBox->value()); }
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Please Help Me For Correct My Code
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-
@Ketan__Patel__0011 said in How to Share Any File Using QUdpSocket:
Please Help Me To Solve This Problem
Please Help Me For Correct My CodeIn all this code you don't want to tell us what needs solving/correcting, you want us to do all the work to find out? When posting a question it is usual to supply this information....
On a general matter, however. If I understand right, you are trying to use UDP to send the content of files around?? Since UDP is a lossy protocol (i.e. no guarantee all packets get delivered anywhere), and also no guarantee on reception order, it's not going to be suitable for transferring file content!. You will need TCP for that....
-
@JonB said in How to Share Any File Using QUdpSocket:
going
Thanks For The Reply
I Have Multiple Client And One Server,
And I Want To Listen Any One Client At Time So Whatever File Send By Client That Time Server Get The Response Of ClientI Know About TcpSocket And TcpServer And I was Also Try With IT
But I Can't Listen Any One Particular Client
So I Am Moved On QUdpSocket
In This Concept I Am Able To Listen Any One Client In Same Port Connection -
@Ketan__Patel__0011 said in How to Share Any File Using QUdpSocket:
But I Can't Listen Any One Particular Client
This is also not needed - the server opens a port and listens on them... see the examples
-
@Ketan__Patel__0011
In addition to what @Christian-Ehrlicher has written, the fact remains that UDP is not going to be suitable for file transfer, regardless....