views:

599

answers:

3

I've heard that we can somehow send an image file with binary over a socket... But I have no idea on how to convert an image file into binary or how to even think of sending it over a socket...

Was hoping if someone could post a simple example? or point me in the right direction :) I am also using QT for just my gui, but not using QT socket programming.

Thanks so much :D I really appreciate it


Question @ djc:

How would you get the directory path for an image, and somehow use the send command on that image? I'm basically using C++. But this is also a question I've had for awhile.

+2  A: 

Any image files you have are already binary. You can just send them over the socket.

djc
A: 

Question @ djc:

How would you get the directory path for an image, and somehow use the send command on that image? I'm basically using C++. But this is also a question I've had for awihle.

confusedEj
Please edit your question rather than add an 'answer' that is actually an addendum to your question.
Jonathan Leffler
A: 

You will need to know, or have the user tell you, a path that will find the image file.

Once you have that, then you logically open the file, read it into a buffer, and then write that buffer over the socket, and finally close the file (always close what you open and free what you allocate). However, there are details to be sorted - like how does the receiving end know that the data that follows is an image and how big it is (so it knows when you've sent it all). Your protocol will, presumably, define a bit pattern (one or two bytes) that identifies the message as an image, and then probably use four bytes to specify the size of the image, followed by the correct number of bytes. You can find the size of a file using the POSIX-based stat() system call. Alternatively, you can send a series of packets containing parts of the image (again with a type - this time, of type 'image packet' instead of 'image') plus the length of the packet (which might only be a 16-bit unsigned integer, for a maximum size of 65535 bytes), plus an 'end image packet' for the last segment. This is perhaps easier for the sender; it is easy for the receiver if the data goes direct to file, but messy if the receiver needs the image in memory.

Jonathan Leffler