Thread: send file and then 2 messages with C socket

  1. #1
    Registered User
    Join Date
    May 2012
    Location
    Italy
    Posts
    53

    send file and then 2 messages with C socket

    This is a snippet of my client code:

    Code:
    while(((uint32_t)total_bytes_read != fsize) && ((nread = read(sockd, filebuffer, sizeof(filebuffer))) > 0)){
            if(write(fd, filebuffer, nread) < 0){
                perror("write");
                close(sockd);
                exit(1);
            }
            total_bytes_read += nread;
        }
        memset(buffer, 0, sizeof(buffer));
        if(recv(sockd, buffer, sizeof(buffer), 0) < 0){
            perror("Errore ricezione 226");
            close(sockd);
            exit(1);
        }
        printf("%s", buffer);
        memset(buffer, 0, sizeof(buffer));
        if(recv(sockd, buffer, sizeof(buffer), 0) < 0){
            perror("Errore ricezione 221");
            close(sockd);
            exit(1);
        }
        printf("%s", buffer);
    where it first receives a file and than it listen for the 2 server's messages!
    Here the snippet of the server:
    Code:
    rc = sendfile(newsockd, fd, &offset, fileStat.st_size);
        if(rc == -1) {
                 fprintf(stderr, "Errore durante l'invio di: '%s'\n", strerror(errno));
                 onexit(newsockd, sockd, fd, 3);
        }
        if((uint32_t)rc != fsize) {
            fprintf(stderr, "Trasferimento incompleto: %d di %d bytes inviati\n", rc, (int)fileStat.st_size);
            onexit(newsockd, sockd, fd, 3);
        }
        memset(buffer, 0, sizeof(buffer));
        strcpy(buffer, "226 File trasferito con successo\n");
        if(send(newsockd, buffer, strlen(buffer), 0) < 0){
          perror("Errore durante l'invio 226");
          onexit(newsockd, sockd, 0, 2);
        }
        memset(buffer, 0, sizeof(buffer));
        strcpy(buffer, "221 Goodbye\n");
        if(send(newsockd, buffer, strlen(buffer), 0) < 0){
          perror("Errore durante l'invio 221");
          onexit(newsockd, sockd, 0, 2);
        }
    The problem is that the file that have been RETRived contains also the 2 messages that were sent by the server O.o
    Why it happens? I've said to the client "recv until the file size"...i don't understand why the file contains also the 2 messages -.-''

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    I've said to the client "recv until the file size"
    What you're actually doing is this:
    Code:
    read(sockd, filebuffer, sizeof(filebuffer))
    That always attempts to read sizeof(filebuffer) bytes. By the time you check how much has been read, it's too late: you've quite probably read more than you expect. If you want to only read fsize bytes, pass that value to read().

  3. #3
    Registered User
    Join Date
    May 2012
    Location
    Italy
    Posts
    53
    Also with fsize instead of sizeof(filebuffer) i got the messages problem

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    What happens if, in the first loop iteration, only 75% of the file is received? How many bytes do you attempt to read on the second iteration?

  5. #5
    Registered User
    Join Date
    May 2012
    Location
    Italy
    Posts
    53
    again fsize! So i have to change
    Code:
    nread = read(sockd, filebuffer, fsize)
    to
    Code:
    nread = read(sockd, filebuffer, fsize_tmp)
    where
    Code:
    fsize_tmp = fsize
    while(((uint32_t)total_bytes_read != fsize) && ((nread = read(sockd, filebuffer, fsize_tmp)) > 0)){
         if(write(fd, filebuffer, nread) < 0){
        perror("write");
        close(sockd);
        exit(1);
         }
         total_bytes_read += nread;
         fsize_tmp -= nread;
    }
    right?

  6. #6
    Registered User
    Join Date
    May 2012
    Location
    Italy
    Posts
    53
    Gooooood it woooooooooorks
    you're a fukin genius

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c socket send file that contain LIST
    By polslinux in forum C Programming
    Replies: 11
    Last Post: 07-06-2012, 02:06 AM
  2. send file via socket
    By beon in forum Networking/Device Communication
    Replies: 10
    Last Post: 05-21-2007, 09:12 PM
  3. IRC get/send messages help
    By RedZone in forum Networking/Device Communication
    Replies: 2
    Last Post: 07-22-2006, 03:27 PM
  4. send file through across socket
    By adk1283 in forum C++ Programming
    Replies: 4
    Last Post: 08-16-2005, 01:51 PM
  5. send zip file via socket
    By WaterNut in forum C Programming
    Replies: 11
    Last Post: 05-24-2005, 11:49 AM