-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.cpp
127 lines (107 loc) · 3.27 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
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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <netdb.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <iostream>
#include <strings.h>
#include <sstream>
#include <iomanip>
#define buff_size 256 //max length for the name passed
#define cliPort 21543
#define sa struct sockaddr
char inputData[buff_size] = {0};
char recvRes[buff_size] = {0};
using namespace std;
// Function declaration for message exchange between client and server
void sendInputData(int sock, char* id, char* sz);
void displayRes(string);
int main(int argc, char* argv[]) {
int soc_c, flag, dynPort;
char* id = argv[1]; //stores id entered during execution
char* size = argv[2]; //stores size entered during execution
struct sockaddr_in serverAddr;
socklen_t sa_len;
// socket creation and verification
soc_c = socket(AF_INET, SOCK_STREAM, 0); //TCP connection
if (soc_c == -1) {
cout<<"Socket creation failed."<<endl;
exit(0);
}
/*else
cout<<"Socket successfully created."<<endl;*/
bzero(&serverAddr, sizeof(serverAddr));
// assign IP, PORT
serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
serverAddr.sin_port = htons(cliPort);
// Client connection to Server and its verification
if (connect(soc_c, (sa*)&serverAddr, sizeof(serverAddr)) != 0) {
cout<<"The Client is down."<<endl;
exit(0);
}
else
cout<<"The Client is up and running."<<endl;
//Dynamic PORT assignment
sa_len=sizeof(serverAddr);
flag=getsockname(soc_c, (sa*)&serverAddr, &sa_len);
if (flag==-1) {
perror("getsockname() failed");
exit(1);
}
else
dynPort=ntohs(serverAddr.sin_port);
// Function call for message exchange between client and server
sendInputData(soc_c, id, size);
// Closing the socket after exchange
close(soc_c);
return 0;
}
// Function designed for message exchange between client and server.
void sendInputData(int sock, char* id, char* sz) {
char buf[buff_size];
strcat (inputData, id);
strcat (inputData, " ");
strcat (inputData, sz);
strcat (inputData, " ");
//int n;
cout<<"Sent link "<<id<<" and File Size "<<sz<<"MB to Main Server."<<endl;
write(sock, inputData, sizeof(inputData));
bzero(buf, sizeof(buf));
read(sock, buf, sizeof(buf));
if ((strncmp(buf, "No", 2)) == 0)
cout<<"No match found."<<endl;
else{
cout<<"Received ";
string result(buf);
displayRes(result);
}
if ((strncmp(buf, "exit", 4)) == 0) {
cout<<"Client Exit."<<endl;
exit(1); //break;
}
}
void displayRes(string r){
float tt,tp,sum;
string t, p, s, dat;
//cout<<endl<<"Res: "<<r<<endl;
int pos=r.find(" ");
t=r.substr(0,(pos));
dat=r.substr(pos+1);
stringstream stt(t);
stt >> tt;
int pos1=dat.find(" ");
p=dat.substr(0,(pos1));
dat=dat.substr(pos1+1);
stringstream stp(p);
stp >> tp;
int pos2=dat.find(" ");
s=dat.substr(0,(pos2));
stringstream ssum(s);
ssum >> sum;
cout<<"Transmission Delay "<<std::fixed<<std::setprecision(2)<<tt<<"ms, ";
cout<<"Propagation Delay "<<std::fixed<<std::setprecision(2)<<tp<<"ms, ";
cout<<"and Total Delay "<<std::fixed<<std::setprecision(2)<<sum<<"ms."<<endl<<endl;
}