-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.c~
291 lines (264 loc) · 6.33 KB
/
node.c~
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
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/times.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netdb.h>
#include <semaphore.h>
#include <pthread.h>
#include <string.h>
#include <time.h>
#include <sys/signal.h>
#include <fcntl.h>
#define messageLength 100
typedef struct frame {
int flagg;
int seq;
int ack;
int win_size;
int crc;
int has_data;
char data[100];
} frame;
char node1_IP [50];
char node2_IP [50];
char node3_IP [50];
int port1;
int port2;
int socReceiver2;
int sockSender3;
void input();
void sender();
void *receiver(void *arg);
void input()
{
int args;
printf("Enter your IP:");
if(( args = scanf("%50s", node1_IP)) == 0)
{
printf("\nPlease enter a valid input!\n");
exit(EXIT_FAILURE);
}
printf("\nEnter IP of second node:");
if(( args = scanf("%50s", node2_IP)) == 0)
{
printf("\nPlease enter a valid input!\n");
exit(EXIT_FAILURE);
}
printf("\nEnter port for communication of second node:");
if(( args = scanf("%d", &port1)) == 0)
{
printf("\nPlease enter a number!\n");
exit(EXIT_FAILURE);
}
printf("\nEnter IP of third node:");
if(( args = scanf("%50s", node3_IP)) == 0)
{
printf("\nPlease enter a valid input!\n");
exit(EXIT_FAILURE);
}
printf("\nEnter port for communication of third node:");
if(( args = scanf("%d", &port2)) == 0)
{
printf("\nPlease enter a number!\n");
exit(EXIT_FAILURE);
}
if(abs(port1-port2) == 1)
{
printf("\nPort numbers are consecutive.Please enter different port numbers!\n");
exit(EXIT_FAILURE);
}
}
void *receiver(void *arg)
{
struct hostent *nodeOtherInfo;
struct hostent *node1Info;
struct sockaddr_in node1;
struct sockaddr_in nodeOther;
int sock;
char *str;
int port;
str=(char*)arg;
char node_IP [50];
int nOfBytes;
char messageString[messageLength];
frame frm;
//get the information of other node
if(str == "0")
{
port=port1;
strcpy(node_IP, node2_IP);
}
else
{
port=port2;
strcpy(node_IP, node3_IP);
}
//Create socket
sock = socket(PF_INET, SOCK_DGRAM, 0);
if (sock < 0) {
perror("Could not create a socket\n");
exit(1);
}
// Make sock non-blocking
fcntl(sock,F_SETFL,O_NONBLOCK);
// Fix the nodeOther struct
nodeOther.sin_family = AF_INET;
nodeOther.sin_port = htons(port);
nodeOtherInfo = gethostbyname(node_IP);
if (nodeOtherInfo == NULL)
{
fprintf(stderr, "initSocketAddress - Unknown host %s\n", node_IP);
exit(1);
}
nodeOther.sin_addr = *(struct in_addr *)nodeOtherInfo->h_addr;
// Fix node1 struct
node1.sin_family = AF_INET;
node1.sin_port = htons(port+1);
node1Info = gethostbyname(node1_IP);
if (node1Info == NULL)
{
fprintf(stderr, "initSocketAddress - Unknown host %s\n", node1_IP);
exit(1);
}
node1.sin_addr = *(struct in_addr *)node1Info->h_addr;
//node1.sin_addr.s_addr = INADDR_ANY;
if (bind(sock, (struct sockaddr *)&node1, sizeof(struct sockaddr)) == -1)
{
printf("Failure when attempt to bind to node:%s\n",node_IP);
exit(1);
}
printf("receiver is done. Node IP:%s\n",node_IP);
while (1)
{
nOfBytes = recvfrom(sock, &frm, sizeof(frame), 0, NULL, NULL);
if (nOfBytes > 0)
{
printf("Received from %s: %s\n>",node_IP, frm.data);
fflush(stdout);
}
}
}
void sender()
{
struct hostent *node1_2Info;
struct hostent *node1_3Info;
struct hostent *node2Info;
struct hostent *node3Info;
struct sockaddr_in node1_2;
struct sockaddr_in node1_3;
struct sockaddr_in node2;
struct sockaddr_in node3;
int sock1, sock2;
int nOfBytes;
char messageString[messageLength];
frame frm;
//Create sockets
sock1 = socket(PF_INET, SOCK_DGRAM, 0);
if (sock1 < 0) {
perror("Could not create a socket\n");
exit(1);
}
sock2 = socket(PF_INET, SOCK_DGRAM, 0);
if (sock2 < 0) {
perror("Could not create a socket\n");
exit(1);
}
// Make sock non-blocking
fcntl(sock1,F_SETFL,O_NONBLOCK);
fcntl(sock2,F_SETFL,O_NONBLOCK);
// Fix the node2 struct
node2.sin_family = AF_INET;
node2.sin_port = htons(port1+1);
node2Info = gethostbyname(node2_IP);
if (node2Info == NULL)
{
fprintf(stderr, "initSocketAddress - Unknown host %s\n", node2_IP);
exit(1);
}
node2.sin_addr = *(struct in_addr *)node2Info->h_addr;
// Fix the node3 struct
node3.sin_family = AF_INET;
node3.sin_port = htons(port2+1);
node3Info = gethostbyname(node3_IP);
if (node3Info == NULL)
{
fprintf(stderr, "initSocketAddress - Unknown host %s\n", node3_IP);
exit(1);
}
node3.sin_addr = *(struct in_addr *)node2Info->h_addr;
// Fix node1_2 struct
node1_2.sin_family = AF_INET;
node1_2.sin_port = htons(port1);
node1_2Info = gethostbyname(node1_IP);
if (node1_2Info == NULL)
{
fprintf(stderr, "initSocketAddress - Unknown host %s\n", node1_IP);
exit(1);
}
node1_2.sin_addr = *(struct in_addr *)node1_2Info->h_addr;
//node1.sin_addr.s_addr = INADDR_ANY;
if (bind(sock1, (struct sockaddr *)&node1_2, sizeof(struct sockaddr)) == -1)
{
printf("Failure when attempt to bind 1\n");
exit(1);
}
// Fix node1_3 struct
node1_3.sin_family = AF_INET;
node1_3.sin_port = htons(port2);
node1_3Info = gethostbyname(node1_IP);
if (node1_3Info == NULL)
{
fprintf(stderr, "initSocketAddress - Unknown host %s\n", node1_IP);
exit(1);
}
node1_3.sin_addr = *(struct in_addr *)node1_3Info->h_addr;
//node1.sin_addr.s_addr = INADDR_ANY;
if (bind(sock2, (struct sockaddr *)&node1_3, sizeof(struct sockaddr)) == -1)
{
printf("Failure when attempt to bind 2\n");
exit(1);
}
while(1)
{
printf("\n>");
fgets(messageString, messageLength, stdin);
messageString[messageLength - 1] = '\0';
if(strncmp(messageString,"quit\n",messageLength) != 0)
{
strcpy(frm.data, messageString);
nOfBytes = sendto(sock1, &frm, sizeof(frm), 0, (struct sockaddr *)&node2, sizeof(struct sockaddr));
if (nOfBytes < 0)
{
perror("Could not write data\n");
exit(1);
}
nOfBytes = sendto(sock2, &frm, sizeof(frm), 0, (struct sockaddr *)&node3, sizeof(struct sockaddr));
if (nOfBytes < 0)
{
perror("Could not write data\n");
exit(1);
}
}
else
{
close(sock1);
close(sock2);
exit(1);
}
}
}
void main()
{
pthread_t tID1;
pthread_t tID2;
input();
char buffer[50];
pthread_create(&tID1,NULL,receiver,"0");
pthread_create(&tID2,NULL,receiver,"1");
sender();
}