Linux server sending file to windows client (Socket)

When I send a file to the client, it becomes damaged and has a size in bytes above.

I have a version of this server running on Windows and it works fine, but I don't have the same result on Linux.

Can a file size on disk be a time error to send a size in bytes to a client that runs on a different platform?

the client side works fine, since I said that I have a version of this server running on Windows, the only difference in fread is: Size = fread(mfcc, 1, min(sizeof(mfcc), FileSize), fp);

is the fread function used correctly?

can an expert analyze and help find a mistake?

int Socket_Setup::FILE_UPLOAD(int iD, std::string DIR_UPLOAD)
{   
    char Block[1024]; 
    long FileSize;      
    fp = fopen(DIR_UPLOAD.c_str(), "rb");
    if (!fp)
    {
          errno_message.append((char*)strerror(errno));
          FUNCTION_LOG(errno_message);
          return 1;
    }

    fseek(fp, 0, SEEK_END);
    FileSize = ftell(fp);
    rewind(fp);

    long Size_Send = htonl(FileSize);
    Total = FileSize;

    // Sending the file size to the Windows Client  
    iResult = send(client[iD].socket, (const char*)&Size_Send, sizeof(long), 0);        
    if (iResult <= 0)
    {
          errno_message.append((char*)strerror(errno));
          FUNCTION_LOG(errno_message);
          return 1;
    }   

     while (FileSize > 0)
    {
        BytesRead = fread(Block, 1, sizeof(Block), fp);
        if (BytesRead <= 0)
        {
            errno_message.append((char*)strerror(errno));
            FUNCTION_LOG(errno_message);
            fclose(fp);
            return 1;
        }
        if (send(client[iD].socket, Block, BytesRead, 0) != BytesRead)
        {
            errno_message.append((char*)strerror(errno));
            FUNCTION_LOG(errno_message);
            fclose(fp);
            return 1;
        }
        FileSize -= BytesRead;      
    }
    fclose(fp);
    return 0;
}
+4
source share
2 answers

, :

iResult = send(client[iD].socket, (const char*)&Size_Send, Size_Send, 0);       

Size_Send ( Size_Send), , , , , sizeof (long) . Size_Sent sizeof (long), .

+3

, POSIX (IEEE Std 1003.1, 2013 Edition).

:

  • .

    // Sending the file size to the Windows Client  
    iResult = send(client[iD].socket, (const char*)&Size_Send, sizeof(long), 0);
    if (iResult <= 0)
    {
          errno_message.append((char*)strerror(errno));
          FUNCTION_LOG(errno_message);
          return 1;
    }
    
  • "".

    if (send(client[iD].socket, Block, BytesRead, 0) != BytesRead)
    {
        errno_message.append((char*)strerror(errno));
        FUNCTION_LOG(errno_message);
        fclose(fp);
        return 1;
    }
    

send() , "" (.. ):

send() . -1, errno - .

- - , Open Group Issue 7, IEEE Std 1003.1, 2013 ..

if (send(client[iD].socket, Block, BytesRead, 0) != BytesRead) .

send() fread(). SendAllBytes(). (. "" ).

:

#include <stdio.h>
#include <sys/socket.h>

...

if (SendAllBytes(socket, &file_size, sizeof(file_size)) != 0) {
    // Error.
    return;
}

...

char buffer[1024];
while (feof(file_stream) == 0) {
    const size_t bytes_read = fread(buffer, sizeof(*buffer), sizeof(buffer) / sizeof(*buffer), file_stream);
    // Upon successful completion, fread() shall return the number of elements
    // successfully read which is less than nitems only if a read error or end-of-file is encountered.
    // If size or nitems is 0, fread() shall return 0 and the contents of the array and the state of the stream remain unchanged.
    // Otherwise, if a read error occurs, the error indicator for the stream shall be set, and errno shall be set to indicate the error.
    // (Source: http://pubs.opengroup.org/onlinepubs/9699919799/functions/fread.html).
    if (bytes_read < sizeof(buffer) / sizeof(*buffer) &&
        ferror(file_stream) != 0) {
        // Error.
        return;
    }

    if (SendAllBytes(socket, buffer, bytes_read) != 0) {
        // Error.
        return;
    }
}

int SendAllBytes(int socket, const char *buffer, size_t bytes_to_send)
{
    size_t total_bytes_sent = 0;
    while (bytes_to_send > 0) {
        const ssize_t bytes_sent = send(socket, &buffer[total_bytes_sent], bytes_to_send, 0);
        // Upon successful completion, send() shall return the number of bytes sent.
        // Otherwise, -1 shall be returned and errno set to indicate the error.
        // (Source: http://pubs.opengroup.org/onlinepubs/9699919799/).
        if (bytes_sent == -1) {
            // Error.
            return -1;
        }

        total_bytes_sent += bytes_sent;
        bytes_to_send -= bytes_sent;
    }
    return 0;
}

. .

+2

Source: https://habr.com/ru/post/1609743/


All Articles