Thread: file transfer across network

  1. #1
    Eager young mind
    Join Date
    Jun 2006
    Posts
    342

    file transfer across network

    hi,
    I have written small server/client programs to transfer files from one pc to another on the network..
    Naturally ,I have used the send() and recv() functions on both sides to send and receive buffer.

    I am transferring the characters one at a time in any given file into the buffer and then sending the buffer.
    sending side:
    Code:
      FILE *fp;
      fp=fopen("filename","r");
      while((c=getc(fp))!=EOF)
           {
             buf[i]=c;
             i++;
             }
      buf[i]='\0';
      fclose(fp);
      send(sockfd,buf,sizeof(buf),0);
    receiving side:
    Code:
      recv(sockfd1,buf1,sizeof(buf1),0);
      FILE *fp;
      fp=fopen("filename","w");
      fprintf(fp,"%s",buf1);
      fclose(fp);
    This does help in reproducing the exact image of the file on another pc for smaller files.. but,I have noticed the data being currupted for larger files ( >8000 chars).
    I have to send only data files with some formatting.
    Is this method good enough?
    Is there any other way of doing it?
    thanks

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Apparently, you don't listen to a word people say
    http://cboard.cprogramming.com/showthread.php?t=80083
    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
    Eager young mind
    Join Date
    Jun 2006
    Posts
    342
    sir,..
    As I had replied to your previous post, your code fragment works. But, that was about sending huge files.
    What I have asked here, is about packing the buffer with the contents of a file before I call send()...and writing the contents of the buffer into the file at the receiver end ...because,I am finding some errors at the receiving end. And I have mentioned what the problem I am facing here :
    "but,I have noticed the data being currupted for larger files ( >8000 chars)."

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Until you start paying attention to the return values of send() and recv(), there is no point in continuing to explain.
    10 bytes or 10MB is all the same problem, you can't just decide that "this is small" therefore I won't bother paying attention to the return result.

    > send(sockfd,buf,sizeof(buf),0);
    What is i ?
    What is c ?
    What is buf?
    Why are you sending sizeof(buf) when the real number of characters is in i ?
    Why are you appending a \0 anyway - send()/recv() don't give a damn about whether your data has a \0, they work with binary 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.

  5. #5
    Eager young mind
    Join Date
    Jun 2006
    Posts
    342
    All i wanted to know was: is there a better method to write into the buffer from a source. I am not having any problem with sending huge files anymore..
    Last edited by kris.c; 06-17-2006 at 01:05 PM.

  6. #6
    Eager young mind
    Join Date
    Jun 2006
    Posts
    342
    ...Do I have to write what i ,c stand for?? isn't it obvious from the prog itself. besides, I know the problem is not with these parameters.
    OK, the next time I ask a question, I will write

    Code:
       int i;  // i is an integer
       char c; //c is a character

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > char c;
    See - you've already got it wrong before you get anywhere near the network.
    c has to be declared int when using the likes of fgetc(), since it has to hold ALL possible char values AND EOF. Your loop may exit early if there is a char in your file which equates to (char)EOF.

    > isn't it obvious from the prog itself.
    No, experience tells me that most people get even the most basic of stuff wrong.
    Actually, I assume that it's wrong - and it usually is.

    Lemme guess, you also have char *buf as well in the mistaken belief that an array won't work because it's length is limited, and a char *isn't.

    Even if you have an array, there is nothing stopping you running off the end, trashing who knows what.
    while( i < BUFSIZ && (c=getc(fp))!=EOF)
    would at least have a modicum of safety

    Maybe begin with
    Code:
    int main ( ) {
      char buff[BUFSIZ];
      FILE *fp;
      fp=fopen("filename","r");
      if ( fp != NULL ) {
        while ( fgets( buff, BUFSIZ, fp ) != NULL ) {
          /* use a send which is checked */
          sendLargeBuffer( sockfd, buff, strlen(buff), 0 );
        }
        fclose( fp );
      } else {
        fprintf(stderr,"Could not open file\n" );
      }
      return 0;
    }
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File Writing Problem
    By polskash in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 10:47 AM
  2. Header File Question(s)
    By AQWst in forum C++ Programming
    Replies: 10
    Last Post: 12-23-2004, 11:31 PM
  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