Thread: Copying a file using sockets

Threaded View

Previous Post Previous Post   Next Post Next Post
  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

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