c - 通过套接字发送后文件大小发生变化

标签 c linux sockets networking network-programming

当通过套接字从客户端向服务器发送文件时,文件大小会发生变化。问题可能出在哪里?

这是客户端代码:

char chunk[512];
host_info = gethostbyname(server);
if (host_info == NULL) {
    perror("get host by name");
    exit(errno);
}

socket_desc = socket(AF_INET, SOCK_STREAM, 0);
if (socket_desc < 0) {
    perror("socket");
    exit(errno);
}

server_address.sin_family = host_info->h_addrtype;
memcpy((char *) &server_address.sin_addr.s_addr, host_info->h_addr_list[0], host_info->h_length);
server_address.sin_port = htons(PORT);

if (connect(socket_desc, (struct sockaddr *) &server_address, sizeof(server_address)) < 0) {
    perror("connect");
    exit(errno);
}


file_to_send = fopen (lfile,"rb");
if(!file_to_send) {
    perror("fopen");
    close(socket_desc);
    exit(errno);
} else {
    long file_size;
    fseek (file_to_send, 0, SEEK_END);     
    file_size = ftell (file_to_send);
    rewind(file_to_send);
while(totally_read < file_size){
    chunk[0] = '\0';
    bytes_read = fread(chunk, sizeof(char), sizeof(chunk), file_to_send);
    totally_read += bytes_read;
    int sent = send(socket_desc, chunk, bytes_read, 0);
    if(sent < 0){
        perror("connect");
        exit(errno);
    }
    totally_sent += sent;
    printf("read: %7db sent: %7db totally read: %7db totally sent: %7db\n", bytes_read, sent, totally_read, totally_sent);
}

这是服务器端:

char chunk[512];
listen_socket = socket(AF_INET, SOCK_STREAM, 0);
if (listen_socket < 0) {
    perror("socket");
    close(listen_socket);
    exit(errno);
}

server_address.sin_family = AF_INET;
server_address.sin_addr.s_addr = htonl(INADDR_ANY);
server_address.sin_port = htons(PORT);

if (bind(listen_socket, (struct sockaddr *) &server_address, sizeof(server_address)) < 0) {
    perror("bind");
    close(listen_socket);
    exit(errno);
}

listen(listen_socket, 5);
client_address_length = sizeof(client_address);

while(1){    
    connect_socket = accept(listen_socket, (struct sockaddr *) &client_address, &client_address_length);
    if (connect_socket < 0) {
    perror("accept");
    close(listen_socket);
    exit(errno);
}

recv_file = fopen(filename,"wb");
int received = 0;
int totally_wrote = 0, totally_received = 0;
while(1){
    chunk[0] = '\0';
    received = recv(connect_socket, chunk, sizeof(chunk), 0);
    if(received < 0) {
        perror("recv");
    } else if(received > 0) {
        int wrote = fwrite(chunk, sizeof(char), received, recv_file);
        totally_wrote +=wrote;
        totally_received += received;
        printf("received: %7db wrote: %7db totally received: %7db torally wrote: %7db\n", received, wrote, totally_received, totally_wrote);
    } else {
        printf("Complete!\n");
        break;
    }
}

发送文本文件时,我得到以下输出 在客户端:

totally read:  299695b   |   totally sent:  299695b

但在服务器端:

totally received:  303279b   |   torally wrote:  303279b

我已使用编辑器打开该文件。传输的文件实际上与源文件相同,除了开头有一些奇怪的数据。如果 block 大小为 512 字节,则传输的文件在开头添加 3584 字节的二进制数据。如果我将 block 大小更改为 256 字节,它会增加 3840 字节。

最佳答案

您没有检查 send() 的返回值 Send() 可以返回 -1 和第三个(长度)参数(含)之间的任何值。您假设任何大于 0 的返回值都等于第三个参数。他们不必如此。

要处理短的send()s(或recv()s(或read()/write())),你需要类似的东西:

int sent, pos, bytes_read;
bytes_read = fread(chunk, sizeof(char), sizeof(chunk), file_to_send);

totally_read += bytes_read;

for (pos = 0; pos < bytes_read; pos += sent) {
    sent = send(socket_desc, chunk+pos, bytes_read-pos, 0)
    switch(sent) {
    case -1: /* handle errno here, especially EAGAIN/EINTR */
    case 0: /*handle EOF here */
        break;
    default:
        break;
        }
    }
totally_sent += pos;

关于c - 通过套接字发送后文件大小发生变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13100582/

相关文章:

c - TASK_UNINTERRUPTIBLE 和使用 C 在 linux 内核开发中处理线程

c++ - 发送没有文件描述符的文件

linux - linux x86_64 上的简单堆栈溢出利用

c - 我试图从正数倒数到零,然后使用负数从零向上数

c - 用C代码管理Linux网络接口(interface)

unit-testing - 需要 TCP/IP 服务器模拟/ stub 软件

c# - 从LAN读取8位数据并在GUI中显示

c# - UDP 广播不能在 Windows 8 中的同一台机器上工作

c - 为什么redis sds将buf部分暴露给上层而不是整个sdshdr

c - 在 C 中减去任意大整数