Search code examples
csocketsrecvsendfile

Not sending file correctly. Sockets. C


I am writing a program that a client can ask for files to a server. Then the server will send them in chunks of 512 bytes. The problem is that when the client read the file:

*Sometimes the first 512 bytes are different from the original file. The total read file also has a different size (and obviously it also ends different from the original file) and therefore the client loop that writes to the new file does never end.

*Sometimes it works perfectly and i don't know why.

Server:

            /* Check if file exists */
            if(access(retrFileName, F_OK) == 0){

                /* Open file */
                fd = open(retrFileName, O_RDONLY); 
                lseek(fd, 0, SEEK_SET);
                if (fd == -1){
                        fprintf(stderr, "Error opening file --> %s", strerror(errno));

                        exit(EXIT_FAILURE);
                }

                /* Get file stats */
                if (fstat(fd, &fileStat) < 0){
                        fprintf(stderr, "Error fstat --> %s", strerror(errno));
                        exit(EXIT_FAILURE);
                }

                sprintf(fileSize, "%li", fileStat.st_size);

                /* Sending file data */
                offset = 0;
                remainData = fileStat.st_size;
                while (((sentBytes = sendfile(clientSock, fd, &offset, 512)) == 512) && (remainData > 0)) {
                        remainData -= sentBytes;
                        fprintf(stdout, "Server envio %d bytes del file, offset ahora vale: %li y quedan = %d bytes\n", sentBytes, offset, remainData);
                }
                remainData -= sentBytes;
                fprintf(stdout, "Server envio %d bytes del file, offset ahora vale: %li y quedan = %d bytes\n", sentBytes, offset, remainData);//do while
                close(fd);////////////////////////
                send(clientSock, NICETRANSFER, sizeof(NICETRANSFER), 0); //LO METE AL ARCHIVO
                printf("send\n");
                //close(clientSock);///////////

            }
            else{
                send(clientSock, FILEERROR, sizeof(FILEERROR), 0);
                printf("send\n");
            }


        }

Client:

/* Open file */
            receivedFile = fopen("r.txt", "wb");
            if (receivedFile == NULL){
                fprintf(stderr, "Failed to open file --> %s\n", strerror(errno));

                exit(EXIT_FAILURE);
            }

            /* Write to the file */
            int contador = 0;
            int remainData = fileSize;
            do{
                if(remainData < 512)
                    bytesLeidos = recv(clientSock, readingBuffer, remainData, 0);
                else
                    bytesLeidos = recv(clientSock, readingBuffer, 512, 0);

                fwrite(readingBuffer, bytesLeidos, 1, receivedFile);

                remainData -= 512;
                contador += 512;
                printf("bytesleidos: %li, contador: %d:\n%s\n\n", bytesLeidos, contador, readingBuffer);

            }while(contador < fileSize);
            fclose(receivedFile);

Solution

  • I solved my problem!! I needed to sync both client and server. To do so, the server send the size of the file and waits for an answer for the client with recv. When the client recieve the file size, it send a "" message.

    I don't know if this is the correct solution, but this way you can sync server and client.

    After sync, the server send the respective file normally with sendfile