tags:

views:

211

answers:

2
  1. How do I convert an image into a buffer so that I can send it over using socket programming? I'm using C++ socket programming. I am also using QT for my Gui.

  2. Also, after sending it over through the socket, how do I put that image back together so it can become a file again?

If someone could put up some sample code that would be great :)

I was looking around and found something about rfile but still not going through my head.

tyty

+2  A: 

Assuming that you want to send an image as a file,
here is pseudocode to send it through a TCP connection.

Client A

Set up a connection  
Open a file in binary mode  
Loop:  
   read/load N bytes in a buffer  
   send(buffer)  
Close file  
Close connection

Client B

Listen and accept a connection  
Create a new file in binary mode  
Loop:  
   n = read(buffer)   
   write n bytes in the file
Close file  
Close connection
Nick D
Just remember endianess issues.
gnud
@gnud, yes we have to take care of endianess when we fill the structures with data. In the case of transmitting a raw sequence of bytes, we can omit that step.
Nick D
+3  A: 

If you're using Qt, use a QImageWriter and a QImageReader and let them deal directly with the socket, thus avoiding the need for a temporary file.

e8johan