C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 03-01-2009, 01:12 PM   #1
Registered User
 
Join Date: Mar 2009
Posts: 1
Smile File transfer over sockets

I was wondering if someone could point me in the right direction with my project:

I'm to write a simple client and server programs that will use sockets. The client will be able to sent a GET <file name> command to the server and the server will send back a file (which does not have to be ASCII characters).

I have basic knowledge of sockets, so I can send text messages back and forth, but I can't figure out how to transfer files.

Let me describe my understanding of what I should do so far, so that someone could tell me if I'm on the right track:

1. At first, I need to establish a socket between the server and client
2. Set server socket to listen
3. Client will open a local file (.bmp lets say) using fread() and save the whole thing into buffer
4. Client then will write() the buffer into the socket
5. Server will accept() that buffer and using fwrite() it will recreate the bmp

Is this the right flow of action? Do I need to handle splitting the buffer into smaller pieces for transfer or will sockets handle that themselves?
adrians is offline   Reply With Quote
Old 03-01-2009, 02:10 PM   #2
and the hat of vanishing
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,214
Not jumping forums every other post might help maintain continuity
http://www.tek-tips.com/viewthread.c...1533785&page=1
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
Up to 8Mb PlusNet broadband from only £5.99 a month!
Salem is offline   Reply With Quote
Old 03-01-2009, 02:57 PM   #3
Senior software engineer
 
brewbuck's Avatar
 
Join Date: Mar 2007
Location: Portland, OR
Posts: 5,381
Your idea is basically correct, except that the client doesn't accept() bytes, it recv()'s them. accept() is something done on the server side when the client connects.

You do not have to handle breaking anything into packets. The protocol (TCP) will do that for you. But I wouldn't recommend writing 10 megabytes at a time, either. To send the contents of the file I'd use a loop and write a few kilobytes at a time.

You don't have to do anything special if the data is binary.
__________________
"Congratulations on your purchase. To begin using your quantum computer, set the power switch to both off and on simultaneously." -- raftpeople@slashdot
brewbuck is offline   Reply With Quote
Old 03-02-2009, 12:56 AM   #4
CSharpener
 
vart's Avatar
 
Join Date: Oct 2006
Posts: 5,242
Quote:
Originally Posted by brewbuck View Post
You don't have to do anything special if the data is binary.
Except you need to transfer the file size beforehead so the client will know how many bytes to read from the socket. High chance that whole file will not arrive at one read
__________________
If I have eight hours for cutting wood, I spend six sharpening my axe.
vart is offline   Reply With Quote
Reply

Tags
file, sockets

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
File transfer- the file sometimes not full transferred shu_fei86 C# Programming 13 03-13-2009 12:44 PM
Possible circular definition with singleton objects techrolla C++ Programming 3 12-26-2004 10:46 AM
System drdroid C++ Programming 3 06-28-2002 10:12 PM
Hmm....help me take a look at this: File Encryptor heljy C Programming 3 03-23-2002 10:57 AM
Need a suggestion on a school project.. Screwz Luse C Programming 5 11-27-2001 02:58 AM


All times are GMT -6. The time now is 03:54 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22