-1

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

这是客户端的代码:

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 字节。

4

1 回答 1

1

您没有检查 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;
于 2012-10-27T14:23:48.863 回答