-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.cpp
65 lines (56 loc) · 1.69 KB
/
client.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
#include "client.h"
client::client(string serverIP, int port, int time):serverIP(serverIP), port(port), time(time)
{
cout<<"connecting to "<<serverIP<<":"<<port<<"..."<<endl;
this->sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (this->sockfd == -1) {
perror("socket");
return;
}
sockaddr_in server_addr;
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(port);
if (inet_pton(AF_INET, serverIP.c_str(), &server_addr.sin_addr) <= 0) {
perror("inet_pton");
close(sockfd);
return;
}
if (connect(sockfd, (struct sockaddr*)&server_addr, sizeof(server_addr)) == -1) {
perror("connect");
close(sockfd);
return;
}
cout<<"Connected"<<endl;
}
int client::StartSend()
{
cout<<"Sending data..."<<endl;
auto start_time = chrono::high_resolution_clock::now();
auto end_time = start_time;
auto duration = chrono::duration_cast<chrono::milliseconds>(end_time - start_time).count();
char data[1000];
memset(data, '0', sizeof(data));
int bytes_sent;
while(duration < this->time*1000)
{
if((bytes_sent = send(sockfd, data, 1000, 0))==-1)
{
perror("send");
this->Close();
return 1;
}
this->sentKB += bytes_sent/1000.0;
end_time = chrono::high_resolution_clock::now();
duration = chrono::duration_cast<chrono::milliseconds>(end_time - start_time).count();
}
this->Mbps = this->sentKB*8/1000/(duration/1000.0);
return 0;
}
void client::Close()
{
close(sockfd);
}
void client::PrintResult()
{
cout<<"Sent = "<<(int)this->sentKB<<" KB | Rate = "<<this->Mbps<<" Mbps"<<endl;
}