Winsock file transfer question

I'm writing a program to time how long it takes to upload/download a file. I have my file sending correctly, but the time is different between the client and the server and I'm not sure why.

Server:
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101

#include <iostream>
#include <winsock2.h>
#include <fstream>
#include <time.h>

// adds wsock32.lib to the linker
#pragma comment(lib, "wsock32.lib")

using namespace std;

void main() {
	WSADATA wsa_data; // this variable will contain the details of the winsock connection
					 // specifies winsock version 2.0
	if(WSAStartup(MAKEWORD(2,0), &wsa_data)!=0) {
		cout << "Error initializing winsock version 2.0" << endl;
		WSACleanup(); // terminates the use of the winsock 2 DLL
		return;
	}

	SOCKET sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	if(sock == INVALID_SOCKET) {
		cout << "Error creating socket" << endl;
		WSACleanup(); // terminates the use of the winsock 2 DLL
		return;
	}


	SOCKADDR_IN server_info;
	server_info.sin_family = AF_INET;
	server_info.sin_addr.s_addr = INADDR_ANY;
	server_info.sin_port = htons(6789);


	if(bind(sock, (SOCKADDR *)(&server_info), sizeof(server_info)) == SOCKET_ERROR) {
		cout << "Error binding socket" << endl;
		WSACleanup(); // terminates the use of the winsock 2 DLL
		return;
	}


	listen(sock, 1);

	cout << "Waiting for connection . . ." << endl;

	SOCKET client_socket = accept(sock, NULL, NULL);

	cout << "Client connected" << endl;

	/*+++++++++++++++++++++++
	Download file from client
	++++++++++++++++++++++++*/

	char* rbuffer;
	rbuffer = new char[5000]; //will write message to this buffer

	clock_t ustart, uend;
	ustart = clock();
	int k = recv(client_socket, rbuffer, sizeof(rbuffer), NULL);
	if (k < 0){
		cout << "Error uploading file" << endl;
	}
	cout << "File is being uploaded..." << endl;

	cout << "File received!" << endl;
	
	//send acknowledgement
	/*char* ackmsg;
	ackmsg = new char[500];
	ackmsg = "File successfully received!";

	int a = send(client_socket, ackmsg, 500, NULL);*/

	uend = clock()-ustart;

	cout << "Time to upload file was " << (double)uend/((double)CLOCKS_PER_SEC) << " seconds." << endl;

	/*+++++++++++++++++++++
	Next, send the file back
	++++++++++++++++++++++*/


	clock_t cstart, cend;
	cstart = clock();
	int s = send(client_socket, rbuffer, sizeof(rbuffer), NULL); //send message back to client
	if (s == SOCKET_ERROR){
		cout << "Error sending file back to client :(" << endl;
	}
	cout << "Sending file back to client..." << endl;
	
	cend = clock()-cstart;	

	cout << "Time to send file back to client was " << (double)cend/((double)CLOCKS_PER_SEC) << " seconds." << endl;

	shutdown(client_socket, SD_SEND);

	closesocket(client_socket);

	WSACleanup();
}


Client:

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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139

#include <iostream>
#include <winsock2.h>
#include <fstream>
#include <time.h>

// adds wsock32.lib to the linker
#pragma comment(lib, "wsock32.lib")

using namespace std;


void main() {
	WSADATA wsa_data; // this variable will contain the details of the winsock connection
	if(WSAStartup(MAKEWORD(2,0), &wsa_data) != 0) {
		cout << "Error initializing winsock version 2.0" << endl;
		WSACleanup(); // terminates the use of the winsock 2 DLL
		return;
	}

	
	SOCKET sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	if(sock == INVALID_SOCKET) {
		cout << "Error creating socket" << endl;
		WSACleanup(); // terminates the use of the winsock 2 DLL
		return;
	}

	char servaddr[100];
	int port;
	char filepath[1024];
	long size;

	//display to user
	cout << "Please enter the server address:" << endl;
	cin >> servaddr;
	char *hostname = servaddr;

	cout << "Please enter the server port" << endl;
	cin >> port;

	cout << "Please enter the path to the file you would like to upload" << endl;
	cin >> filepath;


	// resolves the hostname to an IP address, stored in the hostent structure
	struct hostent *server = gethostbyname(hostname);
	if(server == NULL) {
		cout << "Error resolving hostname to an IP address" << endl;
		WSACleanup(); // terminates the use of the winsock 2 DLL
		return;
	}

	SOCKADDR_IN server_info;
	server_info.sin_port = htons(port);
	server_info.sin_family = AF_INET;
	server_info.sin_addr.s_addr = *((unsigned long *)server->h_addr);

	
	if(connect(sock, (SOCKADDR*)(&server_info), sizeof(server_info)) != 0) {
		cout << "Error connecting to server" << endl;
		WSACleanup(); // terminates the use of the winsock 2 DLL
		return;
	}

	/*++++++++++++++++++++
	First, upload to server
	++++++++++++++++++++++*/
	char* rbuffer;			//will save file content here
	
	
	ifstream file;
	file.open(filepath, ios::in | ios::binary | ios::ate);		//open file
	
	if(file.is_open()){
		file.seekg(0, ios::end);
		size = file.tellg();	//file size! 
		
		cout << "The file size is " << size << " Bytes" << endl;

		file.seekg(0, ios::beg);		//sets location back to beginning of file

		rbuffer = new char[size];
		file.read(rbuffer, size);		//write file to buffer

		clock_t ustart, uend;			//timer
		ustart=clock();
		int j = send(sock, rbuffer, size, NULL);	//send file to server
		
		if (j == -1){
			cout << "Error uploading file to server :(" << endl;
		}

	//	char* ackbuf;
	//	ackbuf = new char[500]; //will write the acknowledgement to this buffer

	//	int a = recv(sock, ackbuf, 500, NULL);

//		cout << ackbuf << endl;

		uend=clock()-ustart;
		cout << "The file took " << (double)uend / ((double)CLOCKS_PER_SEC) << " seconds to upload" << endl;

		float up_br = (float)(8*size)/((float)uend/(double)CLOCKS_PER_SEC);
		cout << "The upload bit rate is " << up_br << " bps" << endl;

		file.close();
	}

	/*+++++++++++++++++++++
	Next, download file back
	++++++++++++++++++++++*/

	char* wbuffer;
	wbuffer = new char[size];		//will write message to this buffer

	clock_t dstart, dend;			//timer
	dstart=clock();		
	int k = recv(sock, wbuffer, size, NULL);	//receiving message from server
	if (k == -1){
		cout << "Error downloading file from server :(" << endl;
	}
	cout << "receiving file" << endl;
	dend=clock()-dstart;
		
	cout << "The file took " << (double)dend / ((double)CLOCKS_PER_SEC) << " seconds to download" << endl;

	float down_br = (float)(8*size)/((float)dend/(double)CLOCKS_PER_SEC);
	cout << "The upload bit rate is " << down_br << " bps" << endl;

	cout << "Thank you for using my program!" << endl;

	shutdown(sock, SD_SEND);

	closesocket(sock);

	WSACleanup();
}


Topic archived. No new replies allowed.