![]() |
| | #1 |
| Registered User Join Date: Mar 2009
Posts: 1
| 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 | |
| | #2 |
| and the hat of vanishing 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 | |
| | #3 |
| Senior software engineer 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 | |
| | #4 |
| CSharpener Join Date: Oct 2006
Posts: 5,242
| 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 | |
![]() |
| Tags |
| file, sockets |
| Thread Tools | |
| Display Modes | |
|
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 |