You are on page 1of 9

Ex No:

Date:

SLIDING WINDOW GO BACK N
AIM:
To write a C program to implement Sliding Window protocol of type go back n.

ALGORITHM:

Server:

1. Include necessary header files to support functions for socket definition, socket types, internet addresses, I/O functions, UNIX system calls.
2. Declare variables for socket ID, port number, socket addresses, character buffer, etc.
3. Create socket for server.
4. Bind socket with addresses.
5. Specify number of allowed connection.
6. Wait for connection.
7. Accept connection (If any).
8. Read the message from the user and transmit it to the client.
9. Read the status of the transmitted frame; in each case of error retransmit all the frames from the error frame till the last sent frame.
10. Repeat previous step until the whole message is received by the client.
11. Close the connected socket.

Client:
1. Include necessary header files to support functions for socket definition, socket types, internet addresses, I/O functions, UNIX system calls.
2. Declare variables for socket ID, port number, socket addresses, character buffer, etc.
3. Create socket for client.
4. Connect client socket to the server socket addresses.
5. Read a frame from the server.
6. Check the received frame and report error if any.
7. Repeat the previous step until the message is error-free.
8. Receive the next frame from the server and continue the previous two steps till end.
9. Close the connected socket.

SLIDING WINDOW GO BACK N


Filename: GN_server.c
Server program Code:
#include<stdio.h>
#include<string.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#define SIZE 4
main()
{
int std, lfd, len, i, j, status, sport;
char str[20], frame[20], temp[20], ack[20];
struct sockaddr_in saddr, caddr;
printf("Enter the port address");
scanf("%d", &sport);
std = socket(AF_INET, SOCK_STREAM, 0);
if(std<0)
perror("Error");
bzero(&saddr, sizeof(saddr));
saddr.sin_family = AF_INET;
saddr.sin_addr.s_addr = htonl(INADDR_ANY);
saddr.sin_port = htons(sport);
lfd = bind(std, (struct sockaddr *)&saddr, sizeof(saddr));
if(lfd)perror("Bind Error");
listen(std, 5);
len = sizeof(&caddr);
lfd = accept(std, (struct sockaddr *)&caddr, &len);
printf("Enter the text:");
scanf("%s", str);
i = 0;
while(i<strlen(str))
{
memset(frame, 0, 20);
strncpy(frame, str+i, SIZE);
printf("\nTransmitting frames:");
len = strlen(frame);
for(j=0; j<len; j++)
{
printf("%d", i+j);
sprintf(temp, "%d", i+j);
strcat(frame, temp);
}
write(lfd, frame, sizeof(frame));
read(lfd, ack, 20);
sscanf(ack, "%d", &status);
if(status == -1)
printf("\nTransmission successful");
else
{
printf("Received error in: %d", status);
printf("\nRetransmitting frames");
for(j=0;;)
{
frame[j] = str[j+status];
j++;
printf("%d", j+status);
if((j+status)%4 == 0)
break;
}
printf("\n");
frame[j] = '\0';
len = strlen(frame);
for(j=0; j<len; j++)
{
sprintf(temp, "%d", j+status);
strcat(frame, temp);
}
write(lfd, frame, sizeof(frame));
}
i = i + SIZE;
}
write(lfd, "Exit", sizeof("Exit"));
printf("\nExitting!\n");
sleep(2);
close(lfd);
close(std);
}

Filename: GN_client.c
Client program Code:

#include<stdio.h>
#include<string.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<arpa/inet.h>
main()
{
int std, lfd, len, choice, cport;
char str[20], str1[20], err[20];
struct sockaddr_in saddr, caddr;printf("Enter the port address:");
scanf("%d", &cport);
std = socket(AF_INET, SOCK_STREAM, 0);
if(std<0)perror("Error");
bzero(&saddr, sizeof(saddr));
saddr.sin_family = AF_INET;
inet_pton(AF_INET, "127.0.0.1", &saddr.sin_addr);
saddr.sin_port = htons(cport);
connect(std, (struct sockaddr *)&saddr, sizeof(saddr));
for(;;)
{
read(std, str, 20);
if(strcmp(str, "Exit") == 0)
{
printf("Exitting!\n");
break;
}
printf("Received: %s\nError? 1 - S or 0 - NO", str);
scanf("%d", &choice);
if(choice == 0)
write(std, "-1", sizeof("-1"));
else
{
printf("Enter the sequence no of the frame where error has occured");
scanf("%s", err);
write(std, err, sizeof(err));
read(std, str, 20);
printf("Received the transmitted frame: %s\n", str);
}
}
close(std);
}

Result:
Thus the program to implement Sliding Window protocol of type Go back n was writtensuccessfully and the output was verified.



Output:

Server Side:
[network@server Network]$ cc GN_server.c
[network@server Network]$ ./a.out
Enter the port address: 1250
Enter text: TrialText
Transmitting frames: 0123
Received error at 0
Retransmitting frames: 1234
Transmitting frames: 4567
Transmission successful
Transmitting frames: 8
Transmission successful
Exiting

Client Side:

[network@server Network]$ cc GN_client.c


[network@server Network]$ ./a.out
Enter the port address: 1250
Received: Tria0123
Do you want to report an error(1-Yes, 2-No): 1
Enter the seq no of the frame: 0
Received the retransmitted frame: Tria0123
Received: lTex4567
Do you want to report an error(1-Yes, 2-No): 0
Received: t8
Do you want to report an error(1-Yes, 2-No): 0
Exiting

Ex No:
Date:
SLIDING WINDOW SELECTIVE REPEAT
AIM:
To write a C program to implement Sliding Window protocol of type selective repeat.

ALGORITHM:
Server:
1. Include necessary header files to support functions for socket definition, socket types, internetaddresses, I/O functions, UNIX system calls.
2. Declare variables for socket ID, port number, socket addresses, character buffer, etc.
3. Create socket for server.
4. Bind socket with addresses.
5. Specify number of allowed connection.
6. Wait for connection.
7. Accept connection (If any).
8. Read the message from the user and transmit it to the client.
9. Read the status of the transmitted frame; in each case of error retransmit the particular frame.
10. Repeat previous step until the whole message is received by the client.
11. Close the connected socket.
Client:

1. Include necessary header files to support functions for socket definition, socket types, internet addresses, I/O functions, UNIX system calls.
2. Declare variables for socket ID, port number, socket addresses, character buffer, etc.
3. Create socket for client.
4. Connect client socket to the server socket addresses.
5. Read a frame from the server.
6. Check the received frame and report error if any.
7. Repeat the previous step until the message is error-free.
8. Receive the next frame from the server and continue the previous two steps till end.
9. Close the connected socket.

SLIDING WINDOW SELECTIVE REPEATFilename:


SR_server.c
Server program Code:
#include<stdio.h>
#include<string.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#define SIZE 4
main()
{
int std, lfd, len, i, j, status, sport;
char str[20], frame[20], temp[20], ack[20];
struct sockaddr_in saddr, caddr;
printf("Enter the port address");
scanf("%d", &sport);
std = socket(AF_INET, SOCK_STREAM, 0);
if(std<0)perror("Error");
bzero(&saddr, sizeof(saddr));
saddr.sin_family = AF_INET;
saddr.sin_addr.s_addr = htonl(INADDR_ANY);
saddr.sin_port = htons(sport);
lfd = bind(std, (struct sockaddr *)&saddr, sizeof(saddr));
if(lfd)perror("Bind Error");
listen(std, 5);
len = sizeof(&caddr);
lfd = accept(std, (struct sockaddr *)&caddr, &len);
printf("Enter the text:");
scanf("%s", str);
i = 0;
while(i<strlen(str))
{
memset(frame, 0, 20);
strncpy(frame, str+i, SIZE);
printf("\nTransmitting frames:");
len = strlen(frame);
for(j=0; j<len; j++)
{
printf("%d", i+j);
sprintf(temp, "%d", i+j);
strcat(frame, temp);
}
write(lfd, frame, sizeof(frame));
read(lfd, ack, 20);
sscanf(ack, "%d", &status);
if(status == -1)
printf("\nTransmission successful");
else{printf("Received error in: %d", status);
printf("\nRetransmitting frame");
frame[0] = str[status];
frame[1] = '\0';
write(lfd, frame, sizeof(frame));
}
i = i + SIZE;
}
write(lfd, "Exit", sizeof("Exit"));
printf("\nExitting!\n");
sleep(2);
close(lfd);
close(std);
}

Filename:
SR_client.c
Client program Code:
#include<stdio.h>
#include<string.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<arpa/inet.h>
main()
{
int std, lfd, len, choice, cport;
char str[20], str1[20], err[20];
struct sockaddr_in saddr, caddr;
printf("Enter the port address:");
scanf("%d", &cport);
std = socket(AF_INET, SOCK_STREAM, 0);
if(std<0)perror("Error");
bzero(&saddr, sizeof(saddr));
saddr.sin_family = AF_INET;
inet_pton(AF_INET, "127.0.0.1", &saddr.sin_addr);
saddr.sin_port = htons(cport);
connect(std, (struct sockaddr *)&saddr, sizeof(saddr));
for(;;)
{
read(std, str, 20);
if(strcmp(str, "Exit") == 0)
{
printf("Exitting!\n");
break;
}
printf("Received: %s\nError?(1 - YES or 0 - NO): ", str);
scanf("%d", &choice);
if(choice == 0)
write(std, "-1", sizeof("-1"));
else
{
printf("Enter the sequence no of the frame where error has occured");
scanf("%s", err);
write(std, err, sizeof(err));
read(std, str, 20);
printf("Received the transmitted frame: %s\n", str);
}
}
close(std);
}

Result:

Thus the program to implement Sliding Window protocol of type selective repeat was writtensuccessfully and the output was verified.

Output:

Server Side:
[network@server Network]$ cc SR_server.c
[network@server Network]$ ./a.out
Enter the port address: 1250
Enter text: HelloTransmitting frames: 0123
Received error at 2
Retransmitting frame
Transmitting frames: 4
Transmission successful
Exiting

Client Side:
[network@server Network]$ cc SR_client.c
[network@server Network]$ ./a.out
Enter the port address: 1250
Received: Hell0123
Do you want to report an error(1-Yes, 2-No): 1
Enter the seq no of the frame: 2
Received the retransmitted frame: l
Received: o4
Do you want to report an error(1-Yes, 2-No): 0
Exiting

You might also like