-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserverA.cpp
More file actions
226 lines (188 loc) · 7.4 KB
/
serverA.cpp
File metadata and controls
226 lines (188 loc) · 7.4 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
#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>
#include <fstream>
using namespace std;
#define SERVERPORT 3333
#define MAXDATASIZE 100
int data[5];
//output an empty line
void OutPutAnEmptyLine(){
cout<<"\n";
}
//read data from the file, Word by Word
void ReadDataFromFileWBW(){
ifstream fin("serverA.txt");
string s;
data[0] = 1;
// read the neighbor information to the server
cout<< "The server A has the following neighbor information:"<<endl;
cout<< "Neighbor-----Cost"<<endl;
while(fin >> s){
if(s == "serverA"){
int i = 1;
fin >> s;
int cost = atoi(s.c_str());
data[i] = cost;
cout << "serverA " << cost << endl;
}
if(s == "serverB"){
int i = 2;
fin >> s;
int cost = atoi(s.c_str());
data[i] = cost;
cout << "serverB " << cost << endl;
}
if(s=="serverC"){
int i = 3;
fin >> s;
int cost = atoi(s.c_str());
data[i] = cost;
cout << "serverC " << cost << endl;
}
if(s == "serverD"){
int i =4;
fin >> s;
int cost = atoi(s.c_str());
data[i] = cost;
cout << "serverD " << cost << endl;
}
}
}
//create the TCP connection between the server and the client
void serverA_createTCP(){
int sock_fd;
char buf[MAXDATASIZE]; //define the max size of the buffer
struct hostent *host;
struct sockaddr_in serv_addr, serverA;
ReadDataFromFileWBW();
OutPutAnEmptyLine();
if((host = gethostbyname("localhost")) == NULL){
herror("gethostbyname in error!");
exit(1);
}
//create the socket in the serverA side
if((sock_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1){
perror("creat socket in error!");
exit(1);
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(SERVERPORT);
serv_addr.sin_addr = *((struct in_addr *)host->h_addr);
//create the connection between the serverA and the client
if (connect(sock_fd, (struct sockaddr *)&serv_addr, sizeof(struct sockaddr)) == -1) {
perror("connect in error!");
exit(1);
}
//send the neighbor information of serverA to the client
if(send(sock_fd, data, sizeof(data), 0) == -1){
perror("send in error!");
exit(1);
}
socklen_t lenA = sizeof(serv_addr);
int socket_name = getsockname(sock_fd, (struct sockaddr *)&serverA, &lenA);
cout<< "The server A finishes sending its neighbor information to the Client with TCP port number "<<"25748"<<" and IP address "<<inet_ntoa(serverA.sin_addr)<<endl;
cout<<"For this connection with the Client, the Server A has TCP port number "<<ntohs(serverA.sin_port)<<" and IP address "<<inet_ntoa(serverA.sin_addr)<<endl;
close(sock_fd);//close the connection of the TCP connection
}
//create the UDP connection of the serverA and the client
void serverA_createUDP(){
struct sockaddr_in server_addr,serverA;
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
server_addr.sin_port = htons(21748); // the UDP port number of the serverA
string x;
// create socket of the serverA
int server_socket_fd = socket(AF_INET, SOCK_DGRAM, 0);
if(server_socket_fd == -1)
{
perror("create socket failed:");
exit(1);
}
// bind the serverA and the client
if((bind(server_socket_fd,(struct sockaddr*)&server_addr,sizeof(struct sockaddr_in)))==-1)
{
perror("server bind failed:");
exit(1);
}
struct sockaddr_in client_addr;
socklen_t client_addr_length = sizeof(client_addr);
int buffer[4][4]; //the buffer to receive the information from the client
// receive the data from the client
if(recvfrom(server_socket_fd,buffer,sizeof(buffer),0,(struct sockaddr*)&client_addr, &client_addr_length) == -1){
perror("receive data failed");
exit(1);
}
// to get the port number and IP address of the client and serverA
socklen_t lenA = sizeof(server_addr);
int UDP_socket_name = getsockname(server_socket_fd, (struct sockaddr *)&serverA, &lenA);
socklen_t len = sizeof(server_addr);
int socket_name = getpeername(server_socket_fd, (struct sockaddr *)&client_addr, &len);
cout<<"The server A has received the network tpology from Client with UDP port number "<<ntohs(client_addr.sin_port)<<" and IP address "<<inet_ntoa(client_addr.sin_addr)<<endl;
//print out the connection information of all the server
cout<<"Edge-----Cost"<<endl;
for(int i=0;i<4;i++){
for(int j=i;j<5;j++){
if(buffer[i][j]!=0){
if(i ==0){
if(j ==0){
cout<<"AA "<<buffer[0][j]<<endl;
}
if(j ==1){
cout<<"AB "<<buffer[0][j]<<endl;
}
if(j ==2){
cout<<"AC "<<buffer[0][j]<<endl;
}
if(j ==3){
cout<<"AD "<<buffer[0][j]<<endl;
}
}
if(i==1){
if(j==1){
cout<<"BB "<<buffer[1][j]<<endl;
}
if(j ==2){
cout<<"BC "<<buffer[1][j]<<endl;
}
if(j ==3){
cout<<"BD "<<buffer[1][j]<<endl;
}
}
if(i==2){
if(j==2){
cout<<"CC "<<buffer[2][j]<<endl;
}
if(j ==3){
cout<<"CD "<<buffer[2][j]<<endl;
}
if(j ==3){
cout<<"CD "<<buffer[2][j]<<endl;
}
}
if(i==3){
if(j==3){
cout<<"DD "<<buffer[2][j]<<endl;
}
}
}
}
}
cout<<"For this connection with Client, the Server A has UDP port number "<<ntohs(serverA.sin_port)<<" and IP address "<<inet_ntoa(client_addr.sin_addr)<< endl;
close(server_socket_fd);
}
int main(){
cout << "The Server A is up and running."<<endl;
serverA_createTCP();
serverA_createUDP();
return 0;
}