I've got a couple questions ab开发者_如何转开发out sending images over.
How do I handle different types of files, jpeg, png, etc.
If the file is large, I ave to use sequence numbers... but I don't know how to stop recving if I do not know the number of sequence numbers.
My knowledge of transfering images / files is next to none. I have never programmed anything like that before. Was hoping to get some tips and tricks =)
Thanks alot.
I am also using QT, if that helps my situation at all.
For images at least, assuming you are using QImage, you can use a QDataStream to convert your QImage to a QByteArray, which can then be written to the QTcpSocket object using write(). It is possible to serialize pretty much any of Qt's data types into a QByteArray using this method.
For general files, the QIODevice (base for QFile, among others) provides read functions such as readAll(), which will read the whole file into a QByteArray ready for you to send.
You will find a number of networking examples included with the Qt distribution. See Qt Assistant -> Contents -> Tutorials and Examples -> Network for more information.
If you use a TCP socket, you need no sequence numbers, because TCP already ensures that data arrive in the same order as they where send. Just send the data, and when done close the connection. Optionally you can use some self-defined type of packet header that gives additional information (e.g. if you want to transmit multiple files over one connection).
Transfer them as you would any other binary data. send()
, recv()
. Or whichever abstraction Qt provides.
精彩评论