You are not logged in.
i was working on a project in linux where tcp/ip communication is needed(i am using BSD sockets,calls like connect,send etc., ).
i send the data in the structure format. now i need to send an image, there comes the problem. i was new to this so can any one throw some light on how to send the image?
my guess were :
should i convert that Image into some binary data?? if so how can i do that?
or is there any way to send the image itself without any conversion?
Thnk in advance
Thank you very much.
i have read through the post:How to send binary file using UDP socket?
and the reply by:Robsease
In that regard i have few doubts:
i am using TCP
ssize_t sendfile(int out_fd, int in_fd, off_t *offset, size_t count);
i am confused with the out_fd and in_fd
i am thinking that out_fd would be the socket file descriptor whhich i obtain from socket system call.. and what abt the in_fd?
ssize_t send(int s, const void *buf, size_t len, int flags);
i am familiar with this send command.
If i want to send the image .
a) open the binary file(as image is a binary)
b)Read into a buffer .
now int s will be the socket fd
buf will be the image buffer
size_t will be sizeof(buffer)
Is that all correct??
any modifications or suggestions
Thanx in advance.
Basically, there's nothing special about sending an "image" vs. sending anything
else through a socket... It's all just data, and the socket doesn't care... There's no
magic trick to sending it through; you send it the same exact way you send anything
else... If you have the image stored in a file, then yes you can use sendfile(), if your
system has such a thing... You can also do tricks like mmap() the file, and send()/write()
that mapped memory to the socket... Or, you can just do things the old fashioned way:
open the file, read the contents into a buffer, and send()/write() that buffer to the
socket... It really doesn't matter much HOW you do it, except from an efficiency
standpoint; they all accomplish the same end goal...
However, the important thing is to establish a protocol for transfering the data between
sender and receiver... The receiver needs to know how much data to expect to read,
so you probably at least want a fixed-size header containing the full size of the
data you are about to send to it... Also, you may or may not want to send along
other info, like type of data, filename, owner/group/perm info, etc... That's just
something you'll have to work out for yourself based on your own needs...