[Interest] How to send an arbitrary file over sockets
Jason H
jhihn at gmx.com
Sat May 30 01:39:42 CEST 2020
How do you know how Big the file is? You assume to get it all in a single operation. You should use qdatastream which will properly serialize the byte array including a length. I've recently taken to using the experimental q HTTP server to provide a web interface, or more recently using qwebsocket to send a file over a websocket.
the only reason why your codeworks right now is because you're a text file is small and it can all be done in one write call. You're going to get many readyReads. The network can break your packets apart so you cannot rely on everything coming on one read.
> Sent: Saturday, May 30, 2020 at 1:29 AM
> From: "Ghobad Zarrinchian" <gzarrin.edu at gmail.com>
> To: interest at qt-project.org
> Subject: [Interest] How to send an arbitrary file over sockets
>
> Dear all,
> I'm writing a client/server application in which the client can send a file
> to the server. At the client side, my code is something like below:
>
> QFile fl(file_name);
>
> fl.open(QIODevice::ReadOnly);
>
> QByteArray fileBytes = fl.readAll();
>
> socket->write(fileBytes);
>
> fl.close();
>
> And at the server side, the code that receives the file is something like this:
>
> QByteArray data = client_socket->readAll();
> QFile fl(file_name);fl.open(QIODevice::WriteOnly);fl.write(data);fl.close();
>
> While the code above works well with text files, it does not work for
> other file types (like JPG and PDF files). What is the problem? How
> should i modify the code so that it works for all kinds of files?
>
> Thanks.
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> https://lists.qt-project.org/listinfo/interest
>
More information about the Interest
mailing list