send file via socket

This is a discussion on send file via socket within the Networking/Device Communication forums, part of the General Programming Boards category; Hi guys, Do anyone here know how to send file via socket in unix? I need to use the client ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    58

    send file via socket

    Hi guys,
    Do anyone here know how to send file via socket in unix? I need to use the client program to send file to server program. Any idea? If possible please provide me with some examples. thanks

  2. #2
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

  3. #3
    Registered User
    Join Date
    May 2007
    Posts
    4
    What library are you using for Sockets, Berkley?
    You may want to try this library. Should be possible, if not, I suppose fstream could just read the entire file into a string, and send it that way.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,448
    Moved to Networking/Device Communication board.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    58
    yup Berkley.. fstream could just read the entire file into a string? I'm sorry but still very new with socket programming. I read the link that send a String "hello world " from http://beej.us/guide/bgnet/ but I was wondering how can I transfer a file? stead of just a string that print out hello world ? And also how can the server reply to the client ? if i'm going to transfer file to the server.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    In the end, it's all just an array of bytes which you're sending from one machine to another.

    You just have to make the first part of the message mean
    - here is a file
    - this is it's name
    - this is how long it is

    Then you send
    - this is the file data.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    May 2007
    Posts
    4
    Yup, something like:
    Code:
    using namespace std;
    fstream f("Myfile", ios::in);
    string theFile="";
    char buff[1024]
    while(!f.eof())
    {
        f.get(buff, 1023);
        theFile += buff;
        buff="";
    }
    f.close();
    Then just send the string as per usual.... As for server/client responses, have you thought about enums?

  8. #8
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    Quote Originally Posted by Vacuus View Post
    Yup, something like:
    Code:
    using namespace std;
    fstream f("Myfile", ios::in);
    string theFile="";
    char buff[1024]
    while(!f.eof())
    {
        f.get(buff, 1023);
        theFile += buff;
        buff="";
    }
    f.close();
    Then just send the string as per usual.... As for server/client responses, have you thought about enums?
    Not good...
    when f.get failes - you still add buff to the string...
    Do not use eof flag to control the loop - check the return value of the input function...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  9. #9
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589
    vart
    check the return value of the input function
    can you expand on this please.
    i'm new to networking code and i'm not sure what input function?
    do you mean to do error (exception) checking on the
    Code:
    f.get(buff, 1023);
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

  10. #10
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,801
    Basically the f.get returns success or failure upon reading in the buffer from the file. So it is smarter to read as your break statement in the loop then check the eof of the filestream because that isn't set until you read past the end of the file so you can incur double reading of the last item or the stream could have failed for other reasons besides EOF.
    Woop?

  11. #11
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    f.eof() returns true when the EOF has been reached. As your loop is setup, this could happen:
    1) f.get() reads data
    2) f.feof() returns false
    3) f.get() reads an EOF, and fails.
    4) The remainder of the loop proceeds to corrupt the string.

    Also, note:
    Code:
    theFile += buff;
    buff="";
    Since buff is a char array, this will only work for text files. This fails on binary files since std::string treats the pointer as a null terminated string.
    The second line is just wrong because you're assigning a string literal to an array. (Shouldn't compile...)

    There's no need to read the whole file in at once. (Indeed, doing so restricts the program to small files - what if I want to send a CD ISO?) Choose a buffer size, read as much as possible from the file to that buffer, and send it. Rinse, lather, repeat. 4KB, 16KB, etc buffers are probably fine. Remember: check for the amount of characters that were read from the file (fstream's gcount() ) and sent on the network (the return value of send() ) - although you may request a read or a send of some value, you might not get it. (Especially on the last read of the file, it'll probably be less.)

    Also: pay attention to Salem's post on what must also be sent. Pay attention to byte ordering (network vs. host), and be sure the other end knows how much data you're sending, or how it could figure it out.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

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. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 01:58 AM

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