You are on page 1of 20

CONTENTS

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 Client Server


2 sockets 10

Implementation of ARP using UDP


3 18

Implementation of Raw sockets


4 25

Downloading File from Http Server


5 34

Implementation of Remote
6 procedure calls 42

Implementation of Subnetting
7 51

8 Implementation of TCP Chat 59

Remote Command Execution using


9 UDP 68

Implement and simulate congestion


10 control using NS2 simulator 77

Implement and simulate distance


vector routing protocol using NS2
11 84
simulator

Simulation of Flooding in NS2


12 91

RECORD COMPLETION DATE: AVERAGE MARKS SCORED:

LAB-IN-CHARGE:
OBJECTIVE:

 Learn socket programming.


 Be familiar with simulation tools.
 Have hands on experience on various networking protocols.
EX.No:1 Experiment Score 10
Date of
Completion Additional Credits

Simulation of sliding window protocol

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

SAMPLE PROGRAM TITLE:


To simulate 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:

1. Write a program to implement stop and wait protocol.


2. Write a program to implement sliding window protocol with frame size 7.
3. Examine the bit error rate and troughput in sliding window protocol
4. Examine the throughput performance of sliding window with varying packet size.
5. Examine the throughput performance of sliding window with varying error rates
6. Examine the throughput performance of sliding window with varying round trip
times.

4
VIVA QUESTIONS:

1 Define sliding window protocol 6 What is Pipelining?

2 What is Stop-and-Wait Protocol? 7 What is Piggy Backing?

3 What is Stop-and-Wait 8 What are the two types of transmission


Automatic Repeat Request? technology available?

4 What is usage of Sequence 9 Explain Go-Back -N ARQ?


Number in Reliable Transmission?

5 Explain RARP 10 Explain selective repeat ARQ?

5
TECHNICAL QUESTIONS:

1. ARQ stands for .


A) Automatic repeat quantization B) Automatic repeat request
C) Automatic retransmission request D) Acknowledge repeat request

2. HDLC is an acronym for .


A) High-duplex line communication B) High-level data link control
C) Half-duplex digital link combination D) Host double-level circuit

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)

5. The Protocol has flow control, but not error control.


A) Stop-and-Wait B) Simplest
C) Go-Back-N ARQ D) Selective-Repeat ARQ

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

7. Which of the following are non-polling system


A) TDMA B) stop and wait.
C) Xon/Xoff D) both a and c

6
7
8
9
10
EX.No:2 /10
Date of Experiment Score

Completion Additional Credits

Implementation of Client Server sockets

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.

SAMPLE PROGRAM TITLE:


To write a network socket program to implement the Echo Server and Client communication
in the Local Area Network (LAN).

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:

1 What is a Socket? 6 The DoD model (also called the TCP/IP


stack) has four layers. Which layer of the
DoD model is equivalent to the Network
layer of the OSI model?

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?

4 Explain DHCP discover message. 9 Explain windowing concept?

5 You want to implement a 10 Discuss the role of Client/Server on a


mechanism that automates the IP data network.
configuration, including IP address,
subnet mask, default gateway, and
DNS information. Which protocol
will you use to accomplish this?

14
TECHNICAL QUESTIONS:

1. The target hardware address on an Ethernet is in an ARP request.


A) 0x000000000000
B) 0.0.0.0
C) variable
D) class dependent

2. An ARP reply is normally .


A) broadcast
B) multicast
C) unicast
D) none of the above

3. An ARP request is normally .


A) broadcast
B) multicast
C) unicast
D) none of the above

4. ICMP is a layer protocol.


A) data link
B) transport
C) network
D) none of the above

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

6. is a program requesting services from a .


A) server; client
B) client; server
C) client; client
D) None of the choices are correct

15
16

You might also like