Escolar Documentos
Profissional Documentos
Cultura Documentos
com - blog
2010-12-05
A question that pops up every now and again and this time Ill document my attempt to
do this. To do this I use the Qt framework. More specifically, I used QtcpSocket for the
connection.
In this example program I will use a client-server approach to this problem. I will build a
server that accepts one or more connections and can receive images. I will also build a
client that can connect to the server and send an image.
The Client-Server
open in browser PRO version
pdfcrowd.com
The Client-Server
architecture
Most of the code that set up the actuall connection have been borrowed from the Simple
Chat program. In this example a client-server system is build. What I removed from this
example is all the GUI interfacing and the functions that receive and send the messages.
The first is because I do not need a GUI here as I will use it as a lib, and the latter is
because I do not need to send and receive QString objects (and more important, I do not
have to forward them to all other connected clients).
So If you want a deeper understanding of how the connection is set-up and how the
server deals with multiple connections, then I point forward to the wiki at qtcentre.org.
pdfcrowd.com
//
//
//
//
And that is it! Yes, there are some beautifications possible, but this is the trick!
pdfcrowd.com
If you want the client-side to give a name to this image then this is perfectly possible by
using some interesting options from the QImage object. To attach the image name as a
tag to the image use:
//QImage image has already been constructed
image.setText("name", name);
On the receiving end we can retrieve this tag information easily using:
//QImage image has already
image.text("name")
been constructed
This leaves us with a lot of interesting possibilities so be sure you check those out as
well!
open in browser PRO version
pdfcrowd.com
Final notes
Well, I took also the liberty to put this in code so you can download it, test it, and learn
from it. You can find the source code here.
If you have any comments or questions on the code then leave a comment below or send
me an e-mail. If you make some changes in the code then I am happy to receive those
changes as well (as diff or as new zip file).
I hope this was helpful for you!
## Comments
### Anonymous (not verified)
. January 20th, 2011
### [Source Code Link Broken](/?q=comment/30924#comment-30924)
Hey, the code examples look good. I wanted to look at the total program and source cod
e so I could have a better idea of what was going on. Anyway you could email it to me?
or check the link?
Thanks
pdfcrowd.com
### patrick
. December 4th, 2012
### [Link checked and should be working.](/?q=comment/34581#comment-34581)
Link checked and should be working.
### VIVEK (not verified)
. October 14th, 2011
### Error
qDebug() << QImageReader::supportedImageFormats () << "WE R HERE";
image->load("abc","png");
qDebug() << QImageReader::supportedImageFormats () << "WE R HERE";
QByteArray ba; // Construct a QByteArray object
QBuffer buffer(&ba); // Construct a QBuffer object using the QbyteArray
image->save(&buffer, "PNG"); // Save the QImage data into the QBuffer
socket->write(ba);
OUTPUT:
("bmp", "gif", "ico", "jpeg", "jpg", "mng", "pbm", "pgm", "png", "ppm", "svg", "svgz",
"tif", "tiff", "xbm", "xpm")
The program has unexpectedly finished.WE R HERE
### patrick
. October 19th, 2011
pdfcrowd.com
pdfcrowd.com
recvdImage.loadFromData(ImageBuffer->buffer());
if(!recvdImage.isNull()){
qDebug() << "Image file was received ";
}
/******************End of Code**************************/
I get an error on the server console:
QBuffer::seek: Invalid pos:1;
I get some image data from clients through the socket, I would like to save it to a fil
e as png, jpg. So that I can do further analysis on that.
Any help will be greatly appreciated, thanks.
### patrick
. December 1st, 2012
If you get an error server-side, then please provide the server side code. The client-s
ide code seems oke... but will not help me (or you) to debug. If you do not want to pas
t it, then send it to me via [e-mail](http://www.hanckmann.net/?q=node/7)!
### Jerry (not verified)
. December 3rd, 2012
Hi Patrick..this is the server side code, the client is an android phone sending an ima
ge. i can't seem to get the image file in full, please help. The client is sending the
data in int64.
### patrick
open in browser PRO version
pdfcrowd.com
pdfcrowd.com
pdfcrowd.com
0 Comments
Recommend
hanckmann.com
Share
ALSO ON HANCKMANN.COM
Subscribe
Privacy
pdfcrowd.com