通过套接字发送文件大小后更改

时间:2012-10-27 13:25:18

标签: 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

我已经用编辑器打开了文件。传输的文件与源文件几乎相同,除了一些奇怪的数据。如果块大小是512字节,则在开始时发送3584字节的加法二进制数据。如果我将块大小更改为256字节,则会增加3840字节。

1 个答案:

答案 0 :(得分:1)

您没有检查send()的返回值 Send()可以返回介于-1和第三个(长度)参数之间的任何值。您假设任何返回值&gt; 0等于第3个参数。他们不必是。

处理短发送()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;