Thread: Copying a file using sockets

  1. #1
    Registered User
    Join Date
    Apr 2016
    Posts
    17

    Copying a file using sockets

    So I have a client and a server. The client receives the names of the source and destination files from the command line. The server receives the destination file's name and checks if it already exists and if it doesn't he sends a confirmation to the client. The client then proceeds to transfer the file's data to server and the server then creates a new file with that content.
    My problem is that:
    1)The destination file isn't complete(Happens with bigger files), even though its size is the same as the source's size.
    2)Some characters in the dest file are "transformed" i.e. they aren't the same as the ones in the source.
    I attached both the server and client files. Here I'll just show the parts where I'm actually transferring the data from the client to the server.

    Client sends data:
    Code:
    //read and send source contents
        while((res = read(fd, cont, sizeof(cont))) != 0) {
          if(res == -1) {
        perror("read");
        exit(EXIT_FAILURE);
          }
          //printf("%s\n", cont);
          printf("res: %d\n", res);
          
          //send source content
          if(write(sd, cont, sizeof(cont)) == -1) {
        perror("write");
        exit(EXIT_FAILURE);
          }
        }
    Server reads and writes to file:
    Code:
    int k = 0;
        do {
          if((res = read(nsd, cont, sizeof(cont))) != 0) {
        if(res == -1) {
          perror("read");
          exit(EXIT_FAILURE);
        }
        if(k == 0) {
          cont2 = malloc(size);
          strcpy(cont2, cont);
          k++;
        }
        else 
          strcat(cont2, cont);
          }
        }
        while(res!=0); //while sum of bytes read < source size, keep reading
        
        // write data to dest
        if(write(fd, cont2, sizeof(char)*size) == -1) { 
          perror("write");
          exit(EXIT_FAILURE);
        }
    What am I dealing with?
    PS: Client's first parameter is the source then the destination file.
    PPS: I'm sort of supposed to send the source's contents over in blocks(say 1024 bytes long). That's probably the reason I'm making life difficult for myself.
    Attached Files Attached Files

  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
    I guess the first thing you need to do is figure out the difference between strlen() and sizeof().
    There are several places where you mess this up.

    Second, you take no account of partial success when dealing with network sockets.

    Code:
    char *p = yourDataPointer;
    size_t = amountToSend;
    do {
      ssize_t n = write( descriptor, p, amountToSend );
      if ( n > 0 ) {
        // advance pointer by amount actually sent
        p += n;
        amountToSend -= n;
      } else if ( n == 0 ) {
        // n == 0 is remote disconnect (an error?)
      } else {
        // n < 0 is error
      }
    } while ( amountToSend > 0 );
    You need the same thing on the receive side as well.
    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
    Registered User
    Join Date
    Dec 2011
    Location
    Namib desert
    Posts
    94
    When writing you might check on bytes written as follows:
    Code:
                limit = strlen(buf);
                out = 0;
                index = 0;
    
                while ((out = write(connectionFd, &buf[index], limit)) > 0)
                {
                    index += out;
                    limit -= out;
                }
    
                index = 0;
    When reading you could check on the number of bytes read as follows:
    Code:
                index = 0;
                limit = MAX_BUFFER;
                memset(buf, 0, MAX_BUFFER+1);
                while ( ((in = read(connectionFd, &buf[index], limit)) > 0)) && index <= MAX_BUFFER)
                {
                    index += in;
                    limit -= in;
                }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File Copying
    By parnaby in forum C++ Programming
    Replies: 4
    Last Post: 01-16-2006, 01:58 PM
  2. Copying a file
    By shivam in forum Windows Programming
    Replies: 2
    Last Post: 12-22-2003, 02:15 PM
  3. Copying exe file
    By vasanth in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2002, 01:57 AM
  4. copying a file
    By Unregistered in forum C Programming
    Replies: 9
    Last Post: 01-10-2002, 09:43 AM
  5. Copying a file
    By rkjd2 in forum C++ Programming
    Replies: 5
    Last Post: 09-09-2001, 10:24 AM

Tags for this Thread