1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
|
/* File to socket */
int file2socket (FILE *fp, int sockfd, long fsize)
{
LARGE_INTEGER freq, start, end;
double speed, rate, eta;
char buf[16*1024];
long tot;
int i, n;
/* Frequency */
if (!QueryPerformanceFrequency(&freq))
return -1;
/* Start time */
if (!QueryPerformanceCounter(&start))
return -1;
/* Main loop */
for (tot = 0; tot < fsize;)
{
if ((n = fread(buf, 1, sizeof(buf), fp)) <= 0)
{
if (feof(fp))
break;
else
return -1;
}
/* Sendall */
if ((i = sendall(sockfd, buf, n)) != n)
return -1;
/* End time */
if (!QueryPerformanceCounter(&end))
return -1;
/* Calculate */
tot += i;
speed = (double)(end.QuadPart - start.QuadPart) / (double)freq.QuadPart;
rate = (double)(tot / speed) / 1024; // KiB/s
eta = (double)(fsize - tot) / (double)rate / 60; // Minutes
/* Display results */
printf("log: Sent: %ld of %ld Prog: %.2f%% Rate: %.1fKiB/s ETA: %.1f\r",
tot, fsize, ((double)tot *100.0) / (double)fsize, rate, eta);
fflush(stdout);
}
return tot;
}
| |