-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.cpp
More file actions
322 lines (259 loc) · 11.2 KB
/
client.cpp
File metadata and controls
322 lines (259 loc) · 11.2 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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <iostream>
using namespace std;
#define SERVERPORT 3333
#define MAXDATASIZE 100
#define BACKLOG 10
int content[4][4]; //the neighbor information from different serveres
struct sockaddr_in my_addr;
//create the TCP connection of the server and the client
void client_createTCP(){
int sock_fd,client_fd,recvbytes;
string s;
int buf[MAXDATASIZE]; //the size of the buffer to store information
struct sockaddr_in remote_addr;
//create the socket in the client side
if((sock_fd = socket(AF_INET, SOCK_STREAM,0)) ==-1){
perror("creat socket in error!");
exit(1);
}
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(SERVERPORT);
my_addr.sin_addr.s_addr = INADDR_ANY;
socklen_t len = sizeof(my_addr);
//bind with the server
if(bind(sock_fd,(struct sockaddr*)&my_addr,sizeof(struct sockaddr_in)) == -1){
perror("bind in error!");
exit(1);
}
//listen for the request from the serverf
if(listen(sock_fd,BACKLOG) == -1){
perror("listen in error");
exit(1);
}
int i = 0;
// ready for the transfer of information from the servers
while(i < 4){
socklen_t sin_size = sizeof(my_addr);
//accept the request of the server
if((client_fd = accept(sock_fd, (struct sockaddr *)&remote_addr, &sin_size)) == -1) {
perror("accept in error");
continue;
}
//receive information from the servers
if((recvbytes = recv(client_fd, buf, MAXDATASIZE,0))==-1){
perror("recv in error!");
exit(1);
}
buf[recvbytes] = '\0';
if(buf[0] == 1){ //reveive information from the serverA
s = "serverA";
cout<<"The Client has TCP port number 25748 and IP address "<<inet_ntoa(remote_addr.sin_addr)<<"."<<endl;
cout<<"The Client recevies neighbor information from the ServerA with TCP port number "<<ntohs(remote_addr.sin_port)<<" and IP address "<<inet_ntoa(remote_addr.sin_addr)<<endl;
cout<<"The Server A has the following neighbor information:"<<endl;
//print out the neighbor information of the servers
cout<<"Neighbor-----Cost"<<endl;
for(int i=0;i<4;i++) {
content[0][i] = buf[i+1];
if(content[0][i]!=0){
if(i == 0){
cout<<"serverA "<<content[0][i]<<endl;
}
if(i ==1){
cout<<"serverB "<<content[0][i]<<endl;
}
if(i ==2){
cout<<"serverC "<<content[0][i]<<endl;
}
if(i ==3){
cout<<"serverD "<<content[0][i]<<endl;
}
}
}
cout<< "For this connection with Server A, the client has TCP port number 25748 and IP address "<<inet_ntoa(remote_addr.sin_addr)<<endl;
}
if(buf[0] == 2){ //reveive information from the serverB
s = "serverB";
cout<<"The client recevies neighbor information from the ServerB with TCP port number "<<ntohs(remote_addr.sin_port)<<" and IP address "<<inet_ntoa(remote_addr.sin_addr)<<endl;
for(int i=0;i<4;i++) {
content[1][i] = buf[i+1];
if(content[1][i]!=0){
if(i == 0){
cout<<"serverA "<<content[1][i]<<endl;
}
if(i ==1){
cout<<"serverB "<<content[1][i]<<endl;
}
if(i ==2){
cout<<"serverC "<<content[1][i]<<endl;
}
if(i ==3){
cout<<"serverD "<<content[1][i]<<endl;
}
}
//cout <<"serverB: "<< content[1][i]<<endl;
}
cout<< "For this connection with Server B, the client has TCP port number 25748 and IP address "<<inet_ntoa(my_addr.sin_addr)<<endl;
}
if(buf[0] == 3){ //reveive information from the serverC
s = "serverC";
cout<<"The client recevies neighbor information from the ServerC with TCP port number "<<ntohs(remote_addr.sin_port)<<" and IP address "<<inet_ntoa(remote_addr.sin_addr)<<endl;
for(int i=0;i<4;i++) {
content[2][i] = buf[i+1];
if(content[2][i]!=0){
if(i == 0){
cout<<"serverA "<<content[2][i]<<endl;
}
if(i ==1){
cout<<"serverB "<<content[2][i]<<endl;
}
if(i ==2){
cout<<"serverC "<<content[2][i]<<endl;
}
if(i ==3){
cout<<"serverD "<<content[2][i]<<endl;
}
}
}
cout<< "For this connection with Server C, the client has TCP port number 25748 and IP address "<<inet_ntoa(my_addr.sin_addr)<<endl;
}
if(buf[0] == 4){ //reveive information from the serverD
s = "serverD";
cout<<"The client recevies neighbor information from the ServerD with TCP port number "<<ntohs(remote_addr.sin_port)<<" and IP address "<<inet_ntoa(remote_addr.sin_addr)<<endl;
for(int i=0;i<4;i++) {
content[3][i] = buf[i+1];
if(content[3][i]!=0){
if(i == 0){
cout<<"serverA "<<content[3][i]<<endl;
}
if(i ==1){
cout<<"serverB "<<content[3][i]<<endl;
}
if(i ==2){
cout<<"serverC "<<content[3][i]<<endl;
}
if(i ==3){
cout<<"serverD "<<content[3][i]<<endl;
}
}
}
cout<< "For this connection with Server D, the client has TCP port number 25748 and IP address "<<inet_ntoa(my_addr.sin_addr)<<endl;
}
i++;
//get the IP address and the port number of the servers and the client
socklen_t len = sizeof(my_addr);
int socket_name = getpeername(client_fd, (struct sockaddr *)&remote_addr, &len);
socklen_t lenC = sizeof(my_addr);
int ssocket_name = getsockname(client_fd, (struct sockaddr *)&my_addr, &lenC);
}
close(client_fd);
}
//create the UDP connection between the server and the client
void client_createUDP(){
int h= 0;
int i =21748;
while(h<4){
// create the interface of the UDP
struct sockaddr_in server_addr;
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
server_addr.sin_port = htons(i);
i+=1000;
// create the socket of the client
int client_socket_fd = socket(AF_INET, SOCK_DGRAM, 0);
if(client_socket_fd < 0){
perror("create socket failed!");
exit(1);
}
// send the network topology to the servers
if(sendto(client_socket_fd, content, sizeof(content), 0, (struct sockaddr*)&server_addr, sizeof(server_addr))< 0){
perror("send the wrong network topology");
exit(1);
}
//get the IP address and the port number of the servers and the client
socklen_t lenl = sizeof(client_socket_fd);
int client_UDP_socket_name = getsockname(client_socket_fd, (struct sockaddr *)&my_addr, &lenl);
socklen_t lenb = sizeof(server_addr);
int socket_name = getpeername(client_socket_fd, (struct sockaddr *)&server_addr, &lenb);
cout<<"The Client has sent the network topology to the network topology to the Server A with UDP port number "<<ntohs(server_addr.sin_port)<<" and IP address "<<inet_ntoa(server_addr.sin_addr)<<endl;
//print out the connection between differnt servers
cout<<"Edge-----Cost"<<endl;
for(int i=0;i<4;i++){
for(int j=i;j<5;j++){
if(content[i][j]!=0){
if(i ==0){
if(j ==0){
cout<<"AA "<<content[0][j]<<endl;
}
if(j ==1){
cout<<"AB "<<content[0][j]<<endl;
}
if(j ==2){
cout<<"AC "<<content[0][j]<<endl;
}
if(j ==3){
cout<<"AD "<<content[0][j]<<endl;
}
}
if(i==1){
if(j==1){
cout<<"BB "<<content[1][j]<<endl;
}
if(j ==2){
cout<<"BC "<<content[1][j]<<endl;
}
if(j ==3){
cout<<"BD "<<content[1][j]<<endl;
}
}
if(i==2){
if(j==2){
cout<<"CC "<<content[2][j]<<endl;
}
if(j ==3){
cout<<"CD "<<content[2][j]<<endl;
}
if(j ==3){
cout<<"CD "<<content[2][j]<<endl;
}
}
if(i==3){
if(j==3){
cout<<"DD "<<content[2][j]<<endl;
}
}
}
}
}
if(h==0){
cout<<"For this connection with Server A, the Client has UDP port number "<<ntohs(my_addr.sin_port)<<" and IP address "<<inet_ntoa(my_addr.sin_addr)<< endl;
}
if(h==1){
cout<<"For this connection with Server B, the Client has UDP port number "<<ntohs(my_addr.sin_port)<<" and IP address "<<inet_ntoa(my_addr.sin_addr)<< endl;
}
if(h==2){
cout<<"For this connection with Server C, the Client has UDP port number "<<ntohs(my_addr.sin_port)<<" and IP address "<<inet_ntoa(my_addr.sin_addr)<< endl;
}
if(h==3){
cout<<"For this connection with Server D, the Client has UDP port number "<<ntohs(my_addr.sin_port)<<" and IP address "<<inet_ntoa(my_addr.sin_addr)<< endl;
}
close(client_socket_fd);
h++;
}
}
int main()
{
client_createTCP();
client_createUDP();
return 0;
}