PDA

View Full Version : Transfer Files: using socket programming


qnx_user
03-27-2003, 12:37 AM
i have written a simple client/server program that says hello to each other after establishing connection. that is working fine. now i am into a bigger task of transfering files. I am wondering where and what to start with. i know how to send text but how can i send a file, its name, handshake etc etc. my previous code can be found in the topic "socket program with direct pc-pc connection". plz help me out in this, i am not an experienced network programmer.

qnx_user
03-27-2003, 12:39 AM
plz suggest me a good tutorial that gives "TRANSFER A FILE" using sockets. i have ones for socket programming but none have anything to say about Transfering a file.

shibiny
03-27-2003, 08:52 AM
if you want to transfer a file (very large space),you 've to do the following step:


send host:


1 open the file you want to transfer

2 read the file content to a buffer which size is 512

3 define your own data structure ,such as orderno

4 send the structure to other host through socket.

loop from 2 to 4


receive host:

recv a structure defined by your own
the check the orderno and place it to corresponding space in a file

recv over check orderno ,if all the packet arrival on ,the file is closed!

OK ,else your retry it !



xi

emihaly
03-27-2003, 09:01 AM
man sendfile:



Reformatting page. Please Wait... done

Extended Library Functions sendfile(3EXT)

NAME
sendfile - send files over sockets or copy files to files

SYNOPSIS
cc [flag ...] file ... -lsendfile [library ...]
#include <sys/sendfile.h>

ssize_t sendfile(int out_fd, int in_fd, off_t *off, size_t
len);

DESCRIPTION
The sendfile() function copies data from out_fd to in_fd
starting at offset off and of length len bytes. The in_fd
argument should be a file descriptor to a regular file
opened for reading. See open(2). The out_fd argument should
be a file descriptor to a regular file opened for writing or
...........

RobSeace
03-27-2003, 02:56 PM
Yeah, sendfile() is the easy way to do it... And, at least with some
versions of sendfile(), you can reverse the order of the FDs, and use
it to also easily receive the file, too...

However, if you're still working on QNX, I don't think there is any
sendfile() function there, at all... (At least with QNX 4.x, which is
what I'm familiar with...) So, you'll probably be forced to do it
manually... Not that hard: just read a chunk of data from the file,
and send it, and keep doing that until EOF... (I'm not sure I understand
the need for shibiny's mentioned orderno and struct... If using TCP,
you're guaranteed everything will be in the order sent, so there's no
need to add any additional checking on top of that... All you really need
to do is dump the raw bytes of the file to the socket, as read...)

qnx_user
03-27-2003, 08:49 PM
oh! but my server is windows. so can i use sendfile in server program? i mean would the client program(qnx) understand?

RobSeace
03-27-2003, 09:49 PM
I have no idea if Windoze has a sendfile()... I know absolutely
nothing about Windoze coding (and, hope to always keep it that
way ;-))... However, if they DID, and it worked the same way
that standard Unix sendfile() implementations do, then yeah,
there's no reason why you couldn't just use it on one side of the
transfer... The other side would just see a stream of bytes, and
could read them however it likes, and manually write them to a
file, if it wants...

shibiny
03-28-2003, 05:12 AM
to Robseace:

why not need order no. as you said ,if using tcp , all the control is graceful.so all the data will arrival on server correctly . but do you know whether all the data is received completely .How many chunks should be received? let 's think if the last three chunks not arrival because network dump.then how can our server program do? write all the received bytes to a file?

emihaly
03-28-2003, 12:28 PM
lot of text, but no help!
Here how do this:

Open file
Do connect
while((ch=get(formfile)!= -1)
{
b=(byte)ch;
if(writeable(socket)) send(b.....)
}
close conn;
close file;


Its all what you need. This is simple pseaudo, after you see it work, simple extent read and send small buffers -> 1024 byte, on lan you can set 8096 bytes

howk

RobSeace
03-28-2003, 01:21 PM
shibiny, why do you need order numbers on each separate buffer full
of data for that?? If all you want is to ensure you've read the full
amount, all that's needed is one initial send of the full size of the data
that you're sending... Just a header message sent prior to sending the
data itself... Then, the receiver will know exactly how much to expect...
But, it won't need any order#'s for any reason... The data is guaranteed
to arrive in the proper order, just by the fact of being transfered over TCP...

dev
04-07-2003, 01:57 PM
hi all,

well i think , Rob is right,
-just send the files info struct(get it using stat()/fstat()),
-send it first,
-then send the bytes in groups, till end, last packet can contain less bytes then normal, here the client can be prepared to read the shorter packet , as he has the info on file ,which he got at start,
- client writes on and on , till all data is recived, any errors will automatically be detected( as if final file is shorter then mentioned in info struct)

but it could be interesteing on how to provide checkpoints here, well then at the time of handshaking both parites can have an option to ask for the number of bytes to skip , instead of transferring the file.

and Rob how bout permissions? and other stuff?, will coping the whole of struct stat buf,passed work out?(as it holds the entry for uid,gid), but how to get the rwx...... permissions from it? ( all the struct stat stuff was for unix systems).

RobSeace
04-07-2003, 07:47 PM
Well, I wouldn't go transfering raw stat structs, unless you're going
between two identical systems... Because, struct stat varies among
different systems... They all have a certain minimum set of members,
but many systems add other non-standard members, as well... Plus,
the locations can vary... So, if you want to transfer all that ownership
and permission info, you'd be best to come up with your own custom
struct to send as part of the initial header before the file data...

As for how to get perms from stat info, that's all contained in st_mode...
(You can & with 0777 to get at JUST the perms, if that's all you want...
Or, 07777 for perms plus setuid/setgid/sticky bits...) There are several
macros defined in <sys/stat.h> for accessing this info...

justmesean
11-20-2006, 07:34 PM
anybody could help me out with the following problem(just need the C code for the server file transfering part):
Your server should accept HTTP GET requests for files. The server should ignore the request headers (for now) and then check if the requested file exists relative to the current directory (i.e., if the request is for /this/is/a/test.html and your program was run from H:\cs465\server\, then your program should attempt to open the file H:\cs465\server\this\is\a\test.html).
o If the file does not exist, send back a reponse of HTTP/1.0 404 File Not Found.
o Otherwise, send back an HTTP response of
HTTP/1.0 200 OK
Server: CS465 server v0.1
Content-type: text/html
...followed by the content of the requested file.

hlasso
11-21-2006, 03:06 PM
justmesean:

It looks like you need someone to do your homework...

Why don't you just try to understand the code posted in this place. There are several examples of how to do stuff similar to what you are asking, and guidelines on how to use sockets to communicate...

If you just need to know how to find a file in Windows or UNIX, then go ahead and ask so. But don't ask people to do your homework.

CreateFile/GetFileSizeEx in Windows and, open/stat in UNIX come to my mind when I think of opening files/getting their size.