FILE *stream; if ((stream = fopen("abc.txt", "w+")) == NULL) { break; } int ret_recv = -100; while (1) { char request[4068] = { 0 }; memset(request, 0, 4068); ret_recv = recv(fd, request, sizeof(request), 0); if (ret_recv == -1) { break; } fseek(stream, 0, SEEK_END); if (fwrite(request, 1, ret_recv, stream) < 0){ fclose(stream); } fflush(stream); } fclose(stream);