Date Name of The Experiment: S.No No. Date of Completion Marks Awarded Staff Signature Remarks
Date Name of The Experiment: S.No No. Date of Completion Marks Awarded Staff Signature Remarks
Date Name of The Experiment: S.No No. Date of Completion Marks Awarded Staff Signature Remarks
Page
S.No Date Name of the Experiment No.
Date of Marks Staff Remarks
Completion Awarded Signature
Simulation of sliding window
1 protocol 1
Implementation of Remote
6 procedure calls 42
Implementation of Subnetting
7 51
LAB-IN-CHARGE:
OBJECTIVE:
OBJECTIVE:
Acquire knowledge onsliding window protocols and it can be easily exhibited using C
programming.
OUTCOME:
Students gain knowledge in understanding sliding window concepts and implement using C.
PREREQUISITE:
Basics of C and usage of commands in sliding window protocol
ALGORITHM:
Sender
1. Create a serversocket
2. Assume the sending window size as 7(m=3).
3. If receiver is ready, initialize sender's frame sequence to 0.
4. Get data from user.
5. Send it to the receiver along with sequence number.
6. Increment sequence number by 1.
7. Repeat steps 4–6 until all frames have been sent.
8. Wait for acknowledgements.
9. If all acknowledgements have arrived then go to step 12.
10. Set sequence number to earliest outstanding frame for which there is no ACK.
11. Go to step4.
Receiver
1. Indicate to sender, the readiness to accept frames.
2. Initialize receiver's expected frame sequence to 0.
3. Accept the incoming frame.
4. If frame's sequence is not receiver's sequence then go to step 7.
5. Send an acknowledgement.
6. Repeat steps 3–6 until all frames are received in sequence and go to step 9
7. Discard frame, there by force the sender to retransmit.
8. Go to step 3.
9. Stop
1
PROGRAM:
Sender
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<netdb.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<unistd.h>
#include<errno.h>
int main()
{
int sock,bytes_received,connected,true=1,i=1,s,f=0,sin_size;
char send_data[1024],data[1024],c,fr[30]=" ";
struct sockaddr_in server_addr,client_addr;
if((sock=socket(AF_INET,SOCK_STREAM,0))==-1)
{
perror("Socket not created");
exit(1);
}
if(setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,&true,sizeof(int))==-1)
{
perror("Setsockopt");
exit(1);
}
server_addr.sin_family=AF_INET;
server_addr.sin_port=htons(17000);
server_addr.sin_addr.s_addr=INADDR_ANY;
if(bind(sock,(struct sockaddr *)&server_addr,sizeof(struct sockaddr))==-1)
{
perror("Unable to bind");
exit(1);
}
if(listen(sock,5)==-1)
{
perror("Listen");
exit(1);
}
fflush(stdout);
sin_size=sizeof(struct sockaddr_in);
connected=accept(sock,(struct sockaddr *)&client_addr,&sin_size);
while(strcmp(fr,"exit")!=0)
{
printf("Enter Data Frame %d:(Enter exit for End): ",i);
scanf("%s",fr);
send(connected,fr,strlen(fr),0);
recv(sock,data,1024,0);
2
if(strlen(data)!=0)
printf("I got an acknowledgement : %s\n",data);
fflush(stdout);
i++;
}
close(sock);
return (0);
}
Receiver
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<netdb.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<unistd.h>
#include<errno.h>
int main()
{
int sock,bytes_received,i=1;
char receive[30];
struct hostent *host;
struct sockaddr_in server_addr;
host=gethostbyname("127.0.0.1");
if((sock=socket(AF_INET,SOCK_STREAM,0))==-1)
{
perror("Socket not created");
exit(1);
}
printf("Socket created");
server_addr.sin_family=AF_INET;
server_addr.sin_port=htons(17000);
server_addr.sin_addr=*((struct in_addr *)host->h_addr);
bzero(&(server_addr.sin_zero),8);
if(connect(sock,(struct sockaddr *)&server_addr,sizeof(struct sockaddr))==-1)
{
perror("Connect");
exit(1);
}
while(1)
{
bytes_received=recv(sock,receive,20,0);
receive[bytes_received]='\0';
if(strcmp(receive,"exit")==0||strcmp(receive,"exit")==0)
{
close(sock);
break;
}
3
else
{
if(strlen(receive)<10)
{
printf("\n Frame %d data %s received\n",i,receive);
send(0,receive,strlen(receive),0);
}
else
{
send(0,"negative",10,0);
}
i++;
}
}
close(sock);
return(0);
}
OUTPUT
SENDER
$ cc sender.c
$ ./a.out
Enter Data Frame 1:(Enter exit for End): saveetha
Enter Data Frame 2:(Enter exit for End): mca
Enter Data Frame 3:(Enter exit for End): exit
RECEIVER
$ cc receiver.c
$ ./a.out
Socket created
Frame 1 data saveetha received
Frame 2 data mca received
CONCLUSION:
Thus using Sliding window procedure, all frames are sent.
PRACTICE EXERCISES:
4
VIVA QUESTIONS:
5
TECHNICAL QUESTIONS:
3. For Stop-and-Wait ARQ, for 10 data packets sent, acknowledgments are needed.
A) exactly 10 B) less than 10
C) more than 10 D) none of the above
4. The Stop-And-Wait ARQ, Go-Back-N ARQ, and the Selective Repeat ARQ are for
channels.
A) noisy B) noiseless
C) either (a) or (b) D) neither (a) nor (b)
6. The range of sequence number which is the concern of the receiver is called the
A) receive sliding window. B) sliding window.
C) frame buffer D) both a and b
6
7
8
9
10
EX.No:2 /10
Date of Experiment Score
OBJECTIVE:
Acquire knowledge on socket programming concepts and can easily exhibit it using C
programming.
OUTCOME:
Students gain knowledge in understanding socket programming and implement using C.
PREREQUISITE:
Basics of C and commands of socket programming.
ALGORITHM:
Server
1. Create a socket using socket function with family AF_INET, type as
SOCK_STREAM.
2. Initialize server address to 0 using the bzero function.
3. Assign the sin_family to AF_INET, sin_addr to INADDR_ANY, sin_port to a
dynamically assigned port number.
4. Bind the local host address to socket using the bind function.
5. Listen on the socket for connection request from the client.
6. Accept connection request from the client using accept function.
7. Within an infinite loop, using the recv function receive message from the client and
print it on the console.
Client
1. Create a socket using socket function with family AF_INET, type as
SOCK_STREAM.
2. Initialize server address to 0 using the bzero function.
3. Assign the sin_family to AF_INET.
4. Get the server IP address and port number from the console.
5. Using gethostbyname function assign it to a hostent structure, and assign it to sin_addr
of the server address structure.
6. Request a connection from the server using the connect function.
7. Within an infinite loop, read message from the console and send the message to the
server using the send function.
11
PROGRAM:
Server: server.c
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<netdb.h>
#include<arpa/inet.h>
#include<string.h>
int main(int asrgc,char*argv[])
{
int bd,sd,ad;
char buff[1024];
struct sockaddr_in cliaddr,servaddr;
socklen_t clilen;
clilen=sizeof(cliaddr);
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
servaddr.sin_port=htons(1999);
sd=socket(AF_INET,SOCK_STREAM,0);
bd=bind(sd,(struct sockaddr*)&servaddr,sizeof(servaddr));
listen(sd,5);
printf("Server is running….\n");
ad=accept(sd,(struct sockaddr*)&cliaddr,&clilen);
while(1)
{
bzero(&buff,sizeof(buff));
recv(ad,buff,sizeof(buff),0);
printf("Message received is %s\n",buff);
}
}
Client:client.c
#include<stdio.h>
#include<string.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<unistd.h>
#include<netinet/in.h>
#include<netdb.h>
#include<arpa/inet.h>
int main(int argc,char * argv[])
{
int cd,sd,ad;
char buff[1024];
struct sockaddr_in cliaddr,servaddr;
struct hostent *h;
h=gethostbyname(argv[1]);
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
memcpy((char *)&servaddr.sin_addr.s_addr,h->h_addr_list[0],h->h_length);
12
servaddr.sin_port = htons(1999);
sd = socket(AF_INET,SOCK_STREAM,0);
cd=connect(sd,(struct sockaddr*)&servaddr,sizeof(servaddr));
while(1)
{
printf("Enter the message: \n");
fgets(buff,100,stdin);
send(sd,buff,sizeof(buff)+1,0);
printf("\n Data Sent ");
//recv(sd,buff,strlen(buff)+1,0);
printf("%s",buff);
}
}
OUTPUT
Server:
[root@localhost 4ita33]# vi server.c
[root@localhost 4ita33]# cc server.c
[root@localhost 4ita33]# ./a.out
Server is running….
Message received is hi
Message received is hi
Client:
[root@localhost 4ita33]# vi client.c
[root@localhost 4ita33]# cc client.c
[root@localhost 4ita33]# ./a.out 127.0.0.1
Enter the message:
CONCLUSION:
Thus the data from client to server is echoed back to the client to check
reliability/noise level of the channel is executed and the output is verified successfully.
PRACTICE EXERCISES:
1. Write a C program to reverse the string in client server communication using Socket?
2. Write a C program to change case of a given string in client server communication using
Socket.
3. Write a C program to check whether the given word is palindrome in client server
communication using Socket.
4. Write a C program to implement Socket one way connection.
5. Write a C program to print the Socket address.
6. Write a C Program to implement sorting N numbers in client server communication
using Socket.
7. Write a C program to implement date server and client in C using sockets.
8. Write a C program to implement a chat server and client in C using sockets.
9. Write a C program to implement FTP using Socket.
10. Write a C program to implement Socket two way communication
13
VIVA QUESTIONS:
2 Explain the services that use 7 Explain how can we detect that the other
Socket? end of a TCP connection has crashed?
3 What layer in the TCP/IP stack is 8 How does TCP try to avoid network melt
equivalent to the Transport layer of down?
the OSI model?
14
TECHNICAL QUESTIONS:
5. Which of the following TCP/IP protocol is used for transferring files from one machine to
another?
A) RARP
B) ARP
C) TCP
D) FTP
15
16