2016-08-20 14 views
0

クライアントからサーバーにファイルを送信しようとしています。それはいつか働き、いつかはしない。最初にファイルを送信しようとすると、破損したファイルが送信され、サーバー側で形成されたファイル面はそのサイズの2倍になります。しかし、最初の試行の後にすべての試行で成功した転送をファイルします。誰もこれで私を助けることができますか?クライアントからサーバーへのファイル転送

いるclient.c

手始めに
 message msg; 
     msg.type = SEND_FILE; 
     char *username, *filename; 

     username = strtok(input+6, " "); 
     filename = strtok(NULL, ""); 

     //Get Picture Size 
     printf("Getting Picture Size\n"); 
     FILE *picture; 
     picture = fopen(filename, "r"); 
     int size; 
     fseek(picture, 0, SEEK_END); 
     size = ftell(picture); 
     fseek(picture, 0, SEEK_SET); 

     //Send Picture Size 
     printf("Getting Picture Size\n"); 
     sprintf(msg.data, "%d", size); 
     strncpy(msg.username, username, 20); 

     if(send(connection->socket, &msg, sizeof(message), 0) < 0) 
     { 
      perror("Send failed"); 
      exit(1); 
     } 
     //Send Picture as Byte Array 
     printf("Sending Picture as Byte Array\n"); 
     char send_buffer[size+1]; 
     fread(send_buffer, 1, sizeof(send_buffer), picture); 
     write(connection->socket, send_buffer, sizeof(send_buffer)); 
     bzero(send_buffer, sizeof(send_buffer)); 

server.c

//Read Picture Size 
printf("Reading Picture Size\n"); 
int size = atoi(message_text); 

//Read Picture Byte Array 
printf("Reading Picture Byte Array\n"); 
char p_array[size]; 
printf("Converting Byte Array to Picture %d\n", size); 
FILE *image; 
image = fopen("c4.png", "w"); 
int readSize = 0; 
while (readSize < size) { 
    readSize = readSize + read(clients[sender].socket, p_array, size); 
    fwrite(p_array, 1, sizeof(p_array), image); 
} 

fclose(image); 
+1

ポスト完全な機能と構造の定義、または問題を表示し、より良い完全なプログラム:あなたができるこの問題を解決するには


。 – chqrlie

答えて

1

あなたはすべてのfwrite()

fwrite(p_array, 1, sizeof(p_array), image); 
に同じバイト数を格納する必要はありません

実際に読み取られたバイト数のみ。

sizeof(p_array)p_arrayのサイズを返します。sizeのサイズはcharです。後者は1であると定義される。このread()完全への呼び出しに加え


は反対側が接続をシャットダウンするかどうかをテストと共に、エラーチェックを欠いています。これは遠くには導かれていません。

#include <errno.h> /* for errno */ 

... 

    size_t size = atoi(message_text); 
    printf("Reading Picture Byte Array\n"); 
    char p_array[size]; 
    size_t readSize = 0; 
    while (readSize < size) { 
    ssize_t result = read(clients[sender].socket, p_array, size); /* Mind the ssize_t, it's *not* size_t! */ 
    if (0 >= result) 
    { 
     if (0 == result) 
     { 
     fprintf(stderr, "The other end gracefully shut down the connection.\n"); 

     break; 
     } 
     else 
     { 
     if (EINTR == errno) /* got interrupted, start over */ 
     { 
      continue; 
     } 

     if (EAGAIN == errno) /* in case reading from a non-blocking socket: no data available, start over */ 
     { 
      continue; 
     } 

     /* Something went wrong unrecoverable. */ 
     perror("read() failed"); 

     break; 
     } 
    } 
    else 
    { 
     fwrite(p_array, 1, result, image); 
     /* Also add error checking here! */ 
     readSize += result; 
    } 
} 
+0

どういうわけか、 'if(EAGAIN == errno)'が後ろ向きに感じる... – chqrlie

+0

@ ack thanks、私のために働いた。これを使用して転送できるファイルサイズはどれくらいですか教えてください。 –

+0

@chqrlie:あまりにも頻繁に 'errno = EAGAIN' ...:}とタイプし、このようなバグを決して再度検索しないことに決めました! – alk

関連する問題