UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

  • Index
  • » C++
  • » How to send image through ethernet(tcp/ip)?

#1 2010-04-21 05:57 AM

socketnewbie
Member
Registered: 2010-04-21
Posts: 11

Re: How to send image through ethernet(tcp/ip)?

HI,
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
@socketnewbie

Offline

#2 2010-04-21 10:43 AM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,230

Re: How to send image through ethernet(tcp/ip)?

See the replies on the cpp forum. Or go through our UNIX Socket FAQ.

Offline

#3 2010-04-21 11:34 AM

socketnewbie
Member
Registered: 2010-04-21
Posts: 11

Re: How to send image through ethernet(tcp/ip)?

@I3839
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

1)

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?

2)

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.

Offline

#4 2010-04-21 02:22 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,826
Website

Re: How to send image through ethernet(tcp/ip)?

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...

Offline

  • Index
  • » C++
  • » How to send image through ethernet(tcp/ip)?

Board footer

Powered by FluxBB