-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
137 lines (98 loc) · 3.54 KB
/
main.cpp
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
#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 = "192.168.71.50";
//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(27015);
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("winbox.cfg", 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();
}