Thread: File transfer over sockets

  1. #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?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    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.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

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

Tags for this Thread