-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathclient.c
71 lines (69 loc) · 2 KB
/
client.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
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
int main()
{
int c_sock;
c_sock = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in client;
memset(&client, 0, sizeof(client));
client.sin_family = AF_INET;
client.sin_port = htons(9009);
client.sin_addr.s_addr = inet_addr("127.0.0.1");
if (connect(c_sock, (struct sockaddr *)&client, sizeof(client)) == -1)
{
printf("Connection failed");
return 0;
}
printf("\n\tClient -with individual acknowledgement scheme\n\n");
char msg1[50] = "akwnowledgementof-";
char msg2[50];
// char msg3[50]="Final cumulative aknowledgement";
char buff[100];
int flag = 1, flg = 1;
for (int i = 0; i <= 9; i++)
{
flg = 1;
bzero(buff, sizeof(buff));
bzero(msg2, sizeof(msg2));
if (i == 8 && flag == 1)
{
printf("here\n"); // simulating loss
// i--;
flag = 0;
read(c_sock, buff, sizeof(buff));
}
int n = read(c_sock, buff, sizeof(buff));
if (buff[strlen(buff) - 1] != i + '0')
{ // outoforder
printf("Discarded as out of order \n");
// printf("%c, %c, -- %s\n",buff[strlen(buff)-1],'0'+i,buff);
// flg=0;
i--;
}
else
{
printf("Message received from server : %s \n", buff);
printf("Aknowledgement sent for message \n");
strcpy(msg2, msg1);
msg2[strlen(msg2)] = i + '0';
// if(i==8)
// continue; //we will send cumulative for last 2
// if(i==9){
// write(c_sock,msg3,sizeof(msg3));
// printf("- %s",msg3);}
// else
write(c_sock, msg2, sizeof(msg2));
// printf("- %s",msg2);
}
}
close(c_sock);
return 0;
}