-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient2.cpp
More file actions
262 lines (238 loc) · 7.11 KB
/
client2.cpp
File metadata and controls
262 lines (238 loc) · 7.11 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
// ************************************************
// filename: client2.cpp
//
// creator:Sangamesh Patil
//
// Description: This is a client to contact server. It works on UDP protocol and code is inspired by earlier
// implementation of UDP classes done by myself
//
// This computer program is protected.
// Recipient is to retain the program in confidence, and
// is not permitted to copy, use, distribute, modify or
// translate the program without authorization from me.
//
// History
//
// date name description
// 09Mar2015 Sangamesh Patil initial creation and file reading
// 15Mar2015 Sangamesh Patil added UDP client
// 17Apr2015 Sangamesh Patil final fix
// ***************************************************
#include <cstdlib>
#include<list>
#include<string>
#include <string.h>
#include<stdio.h>
#include<iostream>
#include<sstream>
#include<stdlib.h>
#include<fstream>
#include <unistd.h>
#include <errno.h>
#include <string>
#include <vector>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#define TRUE 0;
#define FALSE 1;
#define UDP 21851
using namespace std;
std ::ifstream input;
string key[20],value[20];
string inputkey;
bool foundkey=false;
string foundvalue;
const string HOSTNAME = "nunki.usc.edu";
// **************************************
// Class for UDP socket client
// **************************************
class udpsocketclass
{
private:
void showErr(const char *);
int bufferSize;
struct sockaddr_in peer_address;
struct sockaddr_in local_address;
public:
int udpSocket;
char localIP[32];
socklen_t localPort;
char peerIP[32];
socklen_t peerPort;
string recvString;
string clientName;
udpsocketclass(string, int, string, int);
void closeUdpSocket();
int start();
int sendData(string);
int recvData();
};
// **************************************
// function to receive data from server.
// **************************************
int udpsocketclass::recvData()
{
ssize_t n;
socklen_t lengthOfAddress = sizeof(struct sockaddr);
// receive the length of data
int recvLength = 0;
if (recvfrom(this->udpSocket, &recvLength, sizeof(recvLength), 0, (struct sockaddr *)&this->peer_address, &lengthOfAddress) < 0)
{
this->showErr("Receive Error");
return -1;
}
recvLength = ntohl(recvLength);
// get peer IP and port
strcpy(this->peerIP, inet_ntoa(this->peer_address.sin_addr));
this->peerPort = ntohs(this->peer_address.sin_port);
// receive data
char buffer[this->bufferSize];
memset(buffer, 0, this->bufferSize);
if ((recvfrom(this->udpSocket, buffer, recvLength, 0, (struct sockaddr *)&this->peer_address, &lengthOfAddress)) < 0)
{
this->showErr("Receive Error");
return -1;
}
this->recvString = buffer;
return 1;
}
// **************************************
// function to send date to server.
// **************************************
int udpsocketclass::sendData(string str)
{
ssize_t n;
int sendLength = htonl(str.size());
socklen_t lengthOfAddress = sizeof(struct sockaddr);
// send the length of data
if ((sendto(this->udpSocket, &sendLength, sizeof(sendLength), 0, (struct sockaddr *)&this->peer_address, lengthOfAddress)) < 0)
{
this->showErr("Send Error");
return -1;
}
if ((sendto(this->udpSocket, str.c_str(), str.size(), 0, (struct sockaddr *)&this->peer_address, lengthOfAddress)) < 0)
{
this->showErr("Send Error");
return -1;
}
return 1;
}
// **************************************
// function to start the udp client
// **************************************
int udpsocketclass::start()
{
this->udpSocket = socket(AF_INET, SOCK_DGRAM, 0);
if (udpSocket < 0)
{
showErr("Socket cannot create");
}
if (bind(this->udpSocket, (struct sockaddr *)&this->local_address, sizeof(struct sockaddr)) < 0)
{
showErr("Bind Error");
return -1;
}
// update port number after udp socket been created
struct sockaddr_in udpTemp;
socklen_t udpTempLen = sizeof(udpTemp);
getsockname(this->udpSocket, (struct sockaddr *)&udpTemp, &udpTempLen);
strcpy(this->localIP, inet_ntoa(udpTemp.sin_addr));
this->localPort = ntohs(udpTemp.sin_port);
return 0;
}
// **************************************
// function to create UDP socket client.
// **************************************
udpsocketclass::udpsocketclass(string srcHost, int srcPort, string dstHost, int dstPort)
{
this->bufferSize = 128;
this->localPort = srcPort;
this->peerPort = dstPort;
// get UDP local IP address
struct hostent *local;
local = gethostbyname(srcHost.c_str());
strcpy(this->localIP, inet_ntoa(*((struct in_addr *)local->h_addr)));
memset((char *) &this->local_address, 0, sizeof(this->local_address));
this->local_address.sin_family = AF_INET;
this->local_address.sin_port = htons(srcPort);
this->local_address.sin_addr = *((struct in_addr *)local->h_addr);
// get UDP peer IP address
struct hostent *peer;
peer = gethostbyname(dstHost.c_str());
strcpy(this->peerIP, inet_ntoa(*((struct in_addr *)peer->h_addr)));
memset((char *) &this->peer_address, 0, sizeof(this->peer_address));
this->peer_address.sin_family = AF_INET;
this->peer_address.sin_port = htons(dstPort);
this->peer_address.sin_addr = *((struct in_addr *)peer->h_addr);
}
// **************************************
// function to close the UDP socket
// **************************************
void udpsocketclass::closeUdpSocket()
{
close(this->udpSocket);
}
// **************************************
// function to show error during sockets.
// **************************************
void udpsocketclass::showErr(const char *err)
{
perror(err);
exit(1);
}
// **************************************
// function to read the file
// **************************************
void fileread()
{
if (input.is_open())
{
for(int i=0;i<20;i++)
{
input>>key[i]>>value[i];
}
}
}
// **************************************
// Main
// **************************************
main()
{
string val="overrwrite";
input.open("client2.txt");
fileread();
input.close();
cout<<"Please Enter Your Search (USC, UCLA etc.):"<<endl;
cin>>inputkey;
for(int i=0;i<20;i++)
{
if(key[i]==inputkey)
{
foundkey=true;
foundvalue=value[i];
}
}
if(foundkey==true)
{
cout<<"The Client2 has received a request with search word "+inputkey+", which maps to "+ foundvalue+"." <<endl;
udpsocketclass udpClient(HOSTNAME, INADDR_ANY, HOSTNAME,UDP);
udpClient.start();
string getvalue="GET " +foundvalue;
printf("The Client2 sends the request \"%s\" to the Server 1 with port number %d and IP address %s.\n",getvalue.c_str(),udpClient.peerPort,udpClient.localIP);
printf("The Client2 port number is %d and IP address is %s.\n",udpClient.localPort,udpClient.localIP);
udpClient.sendData(getvalue);
udpClient.recvData();
val=udpClient.recvString;
printf("The Client2 received the value \"%s\" from the Server 1 with port number %d and IP address %s.\n",val.c_str(),udpClient.peerPort,udpClient.localIP);
printf("The Client2 port number is %d and IP address is %s.\n",udpClient.localPort,udpClient.localIP);
udpClient.closeUdpSocket();
}
else
{
cout<<"The Client1 has received a request with search word"+inputkey+", which doesn't maps to any key..bye!!!"<<endl;
}
}