Timing file transfer over socket

Hi Experts

I am attempting to time a file transfer using the windows QueryPerformanceCounter() to display a transfer rate and estimated transfer time etc. The socket has been set using SO_SNDBUF to match the block size. However i cannot seem to display the results as i would like to. im not sure if the calculations i have made are correct.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/* Sendall */
int sendall (int sock, char *buf, int len)
{
	char *p;
	int i, n;

	for (i = len, p = buf; i > 0;)
	{
		if ((n = send (sock, p, i, 0)) < 0)
			return -1;

		p += n;
		i -= n;
	}

	return len - i;
}


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;
}


Output:
 
log: Sent: 142491648 of 936280564 Prog: 15.22% Rate: 33929.2KiB/s ETA: 389.9


My main concern is trying to get the last two fields to display in a more user friendly format, Im not sure if its "accuracy" which is causing this strange display, or my crappy math ;-(. for example say it takes "70" seconds, i would like to display "1.10", this is being testing on the loop-back address hence why the speed is faster.

Many thanks.
Last edited on
Huh? First you're displaying the rate in Kilobytes per second then you're wanting to display the ETA (sic) in Minutes? Am I understanding this correctly?
To be honest all I want to achieve is to be able to display the results for the rate, time it's going to take etc, in a more user friendly way, just like how a Internet browser, Or p2p app does etc. I'm still not sure how to do this, as you can see from the output there the percentage is correct, so is the rate, but the ETA is completely wrong. If you can give me some guidance on this and explain how I can make it look better I will be most grateful

many thanks
Topic archived. No new replies allowed.