开发者

How do I convert an image into a buffer so that I can send it over using socket programming? C++

  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 开发者_JAVA百科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


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.


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  


I figured this out as well and wrote down what I did. You can find the description here: http://hanckmann.net/?q=node/44. It is fine to download and use the examples there!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜