From adc400aee473233ca64d95bfcc3a00fc8368990c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=BA=90=E6=96=87=E9=9B=A8?= <41315874+fumiama@users.noreply.github.com> Date: Wed, 8 Nov 2023 18:07:58 +0900 Subject: [PATCH] fix client --- client.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/client.c b/client.c index c19b232..f1ef84c 100644 --- a/client.c +++ b/client.c @@ -24,15 +24,19 @@ struct sockaddr_in their_addr; pthread_t thread; uint32_t file_size; int recv_bin = 0; +FILE* fp; + +static void __attribute__((destructor)) defer_close_fp() { + if (fp) fclose(fp); +} void getMessage(void *p) { int c, i; while((c = recv(sockfd, bufr, BUFSIZ, 0)) > 0) { printf("Recv %u bytes: ", c); if(recv_bin) { - FILE* fp = fopen("dump.bin", "w+"); + if(fp == NULL) fp = fopen("dump.bin", "w+"); fwrite(bufr, c, 1, fp); - fclose(fp); } else for(i = 0; i < c; i++) putchar(bufr[i]); putchar('\n'); }