-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathselective_server.c
102 lines (102 loc) · 2.47 KB
/
selective_server.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
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <fcntl.h>
void rsendd(int ch, int c_sock)
{
char buff2[60];
bzero(buff2, sizeof(buff2));
strcpy(buff2, "Retransmission message: ");
buff2[strlen(buff2)] = (ch) + '0';
buff2[strlen(buff2)] = '\0';
printf("Resending Message to client: %s \n", buff2);
write(c_sock, buff2, sizeof(buff2));
usleep(1000);
}
int main()
{
int s_sock, c_sock;
s_sock = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in server, other;
memset(&server, 0, sizeof(server));
memset(&other, 0, sizeof(other));
server.sin_family = AF_INET;
server.sin_port = htons(7004);
server.sin_addr.s_addr = INADDR_ANY;
socklen_t add;
if (bind(s_sock, (struct sockaddr *)&server, sizeof(server)) == -1) {
printf("Binding failed\n");
return 0;
}
printf("\nSelective repeat - Server\n\n");
printf("Total frames to be send:9\n");
printf("Window Size:3\n");
listen(s_sock, 10);
add = sizeof(other);
c_sock = accept(s_sock, (struct sockaddr *)&other, &add);
time_t t1, t2;
char msg[50] = "Server message: ";
char buff[50];
int flag = 0;
fd_set set1, set2, set3;
struct timeval timeout1, timeout2, timeout3;
int rv1, rv2, rv3;
int tot = 0;
int ok[20];
memset(ok, 0, sizeof(ok));
while (tot < 9)
{
int toti = tot;
for (int j = (0 + toti); j < (3 + toti); j++)
{
bzero(buff, sizeof(buff));
char buff2[60];
bzero(buff2, sizeof(buff2));
strcpy(buff2, "Server message: ");
buff2[strlen(buff2)] = (j) + '0';
buff2[strlen(buff2)] = '\0';
printf("Message sent to client: %s \n", buff2);
write(c_sock, buff2, sizeof(buff2));
usleep(1000);
}
for (int k = 0 + toti; k < (toti + 3); k++)
{
qq:
FD_ZERO(&set1);
FD_SET(c_sock, &set1);
timeout1.tv_sec = 2;
timeout1.tv_usec = 0;
rv1 = select(c_sock + 1, &set1, NULL, NULL, &timeout1);
if (rv1 == -1)
perror("select error");
else if (rv1 == 0)
{
printf("Timeout for message: %d \n", k);
rsendd(k, c_sock);
goto qq;
}
else
{
read(c_sock, buff, sizeof(buff));
printf("Message from Client: %s\n", buff);
if (buff[0] == 'n')
{
printf("Corrupted message acknowledgement (msg %d) \n",buff[strlen(buff) -1]-'0');
rsendd((buff[strlen(buff) - 1] - '0'), c_sock);
goto qq;
}
else
tot++;
}
}
}
close(c_sock);
close(s_sock);
return 0;
}