Professional Documents
Culture Documents
LAB MANUAL
Prepared
by
1 / 53
Goals:
Get familiar with the C Programming Language: If you don't already know
it, in this lab you will get started on learning a new programming language.
Although this is not meant to be a tutorial, you will get exposed to the most
basic features of the language by reading and analyzing some basic
programs.
Learn to use or get better practice with Unix system calls: Operating
Systems provide programmers with a range of services that can be used
directly in their applications. These services are most often presented at
system calls, that is, functions that applications can use to ask the operating
systems to perform certain tasks for them. Since this course is based on the
Unix operating system, we will be working exclusively with Unix system calls.
These services are commonly implemented in roughly the same way across
different flavors of Unix. At Bucknell, we currently work with a mixture of Sun
Solaris and Intel Linux machines, so beware: depending on the platform you
work there may be small differences in how these system calls are
implemented. The Unix manual pages available on the computing platform
will be of great help to you in understanding what specific system calls can do
for your application and in how you use them. Make sure to take this
opportunity to learn to navigate the manual pages effectively if you don't
already know how to do that.
2 / 53
The socket call creates one single endpoint for communications, unlike pipe
which returns two. If the value returned is less than 0, the call was unsuccessful
and the error can be discovered through errno and perror. If the value returned
is greater than or equal to 0, the call was successful and the returned value is a
File-descriptor.
Althouth the AF_UNIX domain adds some interesting and flexible options for IPC
within the same host, we will leave it lone for now. We are more interested in
domains that allow two hosts connected to the Internet to communicate and will
focus exclusively on the AF_INET for now.
The next parameter, type, can assume one of three possible values as described
below:
3 / 53
struct in_addr {
__u32 s_addr;
};
4 / 53
**************
/****************************************************************
*
*
* foo.c
*
* A simple C module file that contains a function called 'foo'
*
*
****************************************************************/
#include "decls.h"
foo(int k)
{
printf("i = %d, k = %d\n", i, k);
}
*******************
/*******************************************************
*
*
* main()
*
* The main() function that calls the function 'foo()' *
*
*
*******************************************************/
#include "decls.h"
int main(void)
{
i = 1;
if (i == 0)
x = 1000;
else
foo(-1);
printf(" result x = %f\n", x);
return 0;
}
5 / 53
INDEX
1. OSI MODEL SIMULATION
2. IMPLEMTATION OF DATA LINK FRAMING METHODS
a) CHARACTER COUNT
b) BIT STUFFING AND DE STUFFING
c) CHARACTER STUFFING AND DE STUFFING
3. PARITY CHECK
a) SINGLE DIMENSIONAL DATA
b) MULTI DIMENSIONAL DATA
4. EVEN AND ODD PARITY
5. IMPLEMENTATION OF CRC POLYNOMIALS
6. IMPLEMENTATION OF DATA LINK PROTOCOLS
a) UNRESTRICTED SIMPLEX PROTOCOL
b) STOP AND WAIT PROTOCOL
c) NOISY CHANNEL
7. IMPLEMENTATION OF SLIDING WINDOW PROTOCOLS
a) One Bit sliding window protocol
b) GO BACK N SLIDING WINDOW PROTOCOL
c) SELECTIVE REPEAT SLIDING WINDOW PROTOCOL
8. IMPLEMENTATION OF ROUTING ALDORITHMS
a) DIJKSTRAS ALGORITHM
b) DISTANCE VECTOR ROUTING
c) HIREARCHICAL ROUTING
d) BROADCAST ALGORITHM
9. CONGESTION ALGORITHMS
a) TOKEN BUCKET ALGORITHM
b) LEAKY BUCKET ALGORITHM
10.
IP-ADDRESSING
11.
ENCRYPTION ALGORITHMS
a) SUBSTITUTION CIPHER
b) TRANSPOSITION CIPHER
c) DATA ENCRYPTION ALGORITHM
12.
IMPLEMENTATION OF SERVERS
a) DATE SERVER
b) CHAT SYSTEM
c) ECHO SERVER
6 / 53
7 / 53
OSI MODEL
PROGRAM DEFINITION:
This is an open system interconnection program tha transmit message
from sender to receiver through server different layers.
PROGRAM DESCRIPTION:
The OSI Model deals with connecting open system. This model does not
specify the exact services and protocols to used in each layer.
Therefore,the OSI Model is not a network architecture. This model has
seven layers. They are Physical layer, Datalink layer, Presentation layer,
Network layer, Session layer, Transport layer and Application layer. At
sender side, each layer add the header. The length of string i.e, number of
bytes are not restricted upto Session layer.
ALGORITHM:
1. Read the input string and address.
2. Add application header.
3. Print the string.
4. Add the presentation layer header.
5. Print the string.
6. Add the Session layer header.
7. Print the string.
8. Add the Transport layer header.
9. Print the string.
10. Add the Network layer header.
11. Print the string.
12. Add the Datalink layer header.
13. Print the string
14. add the physical layer header.
15. Print the string.
INPUT:
Enter the string:hai
OUTPUT:
TRANSMITTER:
APPLICATION LAYER:
AH hai
PRESENTATION LAYER:
PHAH hai
SESSION LAYER:
SHPHAH hai
TRANSPORT LAYER:
THSHPHAH hai
NETWORK LAYER:
NHTHSHPHAH hai
DATALINK LAYER:
DHNHTHSHPHAH hai
MESSAGE ENTERED INTO PHYSICAL LAYER AND TRANSMITTED.
8 / 53
RECEIVER:
MESSAGE ENTERED INTO PHYSICAL LAYER
DATALINK LAYER:
DHNHTHSHPHAH hai
NETWORK LAYER:
NHTHSHPHAH hai
TRANSPORT LAYER:
THSHPHAH hai
SESSION LAYER:
SHPHAH hai
PRESENTATION LAYER:
PHAH hai
APPLICATION LAYER:
AH hai
9 / 53
10 / 53
CHARACTER COUNT
AIM:
Implementation of data link framing methods for counting characters in a
given frame
LOGIC:
The header in the given frame is by default first frame size including the
first field data bits are counted and considered as the first frame and the
next field contains the next frame size and so on.
PSEUDO CODE:
1. At the sender side the user is asked to enter the number of frames he
want to transmit.
2. Depending upon the input, that many number of frames are taken as
input from the user and stored in a 2 by 2 matrix.
3. The length of each frame is calculated and stored in a new array.
4. While out putting the frame, the length of each frame is added to the
each frame and finally all the frames are appended and sent as a
single string.
5. At the receiver side, the first number is treated as the length of the first
frame and the string is extracted and displayed.
6. The next number is treated as the length of the next frame and so on.
At Sender:
INPUT:
Enter the number of frames you want to send:2
Enter the frame:1234
Enter the frame:678
OUTPUT
The transmitted frame is:512344678
At receiver:
INPUT:
Enter data
512344678
OUTPUT:
frame sizes are: 5 4
frames are:
frame 1: 1234
frame 2: 678
11 / 53
BIT STUFFING
AIM:
Implementation of the data link framing methods for the bit stuffing in a
frame.
LOGIC:
Stuffing a 0 bit in a data frame in order to differentiate the header,
trailer and data.
PSEUDO CODE:
/* For given data */
1.
a flag 01111110 is embedded at the starting and the ending of the
data. /* stuffing of data */
2.
if data bit is 1 increment count else count is zero.
3.
If count is five store a zero bit after the five 1s in the data array.
4.
Repeat step 3 till the end of data. /* De stuffing of data */
5.
If the data bit is 1 increment count else count is zero.
6.
If the count is five and the next bit is zero then store the next bit
after zero in the data array. /* transmit the data */
7.
De stuffed data is transmitted with out flags.
At sender:
INPUT:
Enter the string
1111110101
OUTPUT:
Transmitted data is: 01111110111111010101111110
Stuffed data is: 011111101111101010101111110
At Receiver:
OUTPUT:
De stuffed data is: 11111110101
12 / 53
CHARACTER STUFFING
AIM:
Implementation of data link framing methods for character stuffing in a
frame.
LOGIC:
In a character data frame if a DLE is encountered between the data
it is doubled and transmitted at the receiver side it is de stuffed and
original data is obtained.
PSEUDO CODE:
/* Defining DLE characters */
1.
As the DLE characters are non printable characters. The ASCII values
of the printable characters like *,#,$ are assigned to DLE, STX, ETX.
/*Stuffing the data */
2.
If the ASCII value that is assigned to DLE occurs in the data array
another DLE character is stuffed and stored in the array and
transmitted along with starting and ending flags /* destuffing data */
3.
If the ASCII value of DLE occurs in the data array, the next bit is stored
in to the array and transmitted with out the flags.
4.
Here whenever the program encounters characters like * the string
DLE is added to the original string.
At Sender:
INPUT:
Enter Data
r*gm
OUTPUT:
Stuffed data
DLESTXrDLEDLEgmDLEETX
At receiver:
OUTPUT:
The message: r*gm
13 / 53
PARITY CHECK
a)
b)
14 / 53
15 / 53
16 / 53
17 / 53
AIM:
To implement the CRC-12, CRC-16, CRC-CCITT in data link layer
LOGIC:
1.
let r be the degree of G(x).Append r zero bits to the low-order end of
the frame. So it now contains m+r bits and corresponds to the
polynomial x2 m(x).
2.
divide the bit string corresponding to G(x) into the bit string
corresponding to x2 m(x) using modulo-2 division.
3.
subtract the remainder from the bit string corresponding to x2 m(x)
using modulo-2 sub. The result is the check summed frame to be
transmitted. We call it as a polynomial.
PSEUDO CODE:
1. Take the frame as input from the user.
2. Take the users choice of use of the CRC polynomial that he wants to use:
CRC-12 = x12+x11+x 3+x 2+x+1
CRC-16 = x16+x15+x 2+1
CRC-CCITT = x16+x 12+x 5+1
3. Depending upon the users choice, append the zeros to the end of the
frame and display.
4. Calculate the CRC by performing modulo 2 division.
5. Replace the appended zeros in step 3 by the remainder of the above
performed division.
6. This is the transmitted data.
7. At the receiver side, the input string is subjected to modulo 2 division by
the same polynomial that was used at the sender side.
8. If the remainder is 0 there is no error otherwise there was an error during
transmission.
/* Result with no error */
INPUT:
Enter frame:
1101011011
Enter your choice:
1.CRC 12
2.CRC 16
3.CRC CCITT
1
18 / 53
OUTPUT:
Frames after appending zeros: 1101011011000000000000
Remainder: 111101001100
Transmitted data: 1101011011111101001100
Received data:
1101011011111101001100
Remainder: 0
No Error
/* Result with error */
INPUT:
Enter frame:
1101011011
Enter your choice:
1.CRC 12
2.CRC 16
3.CRC CCITT
1
OUTPUT:
19 / 53
20 / 53
21 / 53
INPUT:
Enter the no. of frames: 2
Entered message is
hi
The frame is transmitted
Frame has arrivedMessage given to the network layer is hi
Entered Message is
Hai
The frame is transmitted
Frame has arrivedMessage given to the network layer is hai
22 / 53
23 / 53
INPUT:
Enter no. of frames 2
Enter the message queue
M1=42
OUTPUT:
The frame is transmitted
Frame had arrived
Acknowledge to network layer
Message given to the network layer is queue
24 / 53
void receiver3(void)
{
seq_nr frame_expected;
frame s,r;
25 / 53
event_type event;
frame_expected=0;
while(true)
{
wait_for_event(&event);
if(event==frame_arrival)
{
from_physical_layer(&r);
}
to_physical_layer(&s);
}
} /*end of receiver3*/
INPUT:
Enter the number of frames: 2
Entered message is:
Hi
OUTPUT:
The frame is transmitted
Frame has arrived.
Acknowledge to network layer Message given to the network layer is hi
Entered message is
Hai
The frame is transmitted
Frame has arrived.
Acknowledge to network layer Message given to the network layer is hai
26 / 53
27 / 53
PSEUDO CODE:
Void protocol4(void)
{
seq_nr next_frame_to_send;
seq_nr frame_expected;
frame r,s;
packet buffer;
event_type event;
next_frame_to_send=0;
frame_expected=0;
from_network_layer(&buffer);
s.info=buffer;
s.seq=next_frame_to_send;
s.ack=1-frame_expected;
to_physical_layer(&s);
start_timer(s.seq);
while(true)
{
wait_for_event(&event);
if(event==frame_arrival)
from_physical_layer(&r);
if(r.seq==frame_expected)
{
to_network_layer(&r.info);
inc(next_frame_to_send);
}
}
s.info=buffer;
s.seq=next_frame_to_send;
s.ack=1-frame_expected;
28 / 53
to_physical_layer(&s);
start_timer(s.seq);
}/* end of protocol4*/
INPUT:
Enter the no. of frames: 2
Sender
Enter the message : hi
Frame is transmitted
Receiver
Frame is arrived
Message given to the network layer is hi
Sender
Enter the message: hai
Sender
Frame is transmitted
Receiver
Frame is arrived
Message given to network layer is hai
29 / 53
30 / 53
31 / 53
32 / 53
damaged frame
we timed out
ack timer expired, send ack
}
} /*end of protocol6*/
INPUT:
Maximum sequence number and the frames are given as input to this program.
OUTPUT:
Whether the transmitted frames are received or not is obtained as output of this
program.
33 / 53
34 / 53
35 / 53
36 / 53
start
print enter the number of nodes
print enter the adjacency matrix
read the matrixinto dist[i][j]
print enter the node for which routing table to be constructed
read into str
print the adjacent vertices are
print from adj[c]
read the delay time
constuct the routing table by min=delay[j]+route[i][j]
path=adj[j]
printf the min->path
stop
INPUT:
Enter the router for which you want routing table:A
Enter new time delays for neighboring routers: 6 1 9 19 26 2
OUTPUT:
Ffor A, routing table is
time delay
6
1
9
19
26
2
router
B
B
F
B
F
F
37 / 53
ALGORITHM
1. Start.
2. Print enter the router name.
3. read the router name
4. Print enter number of entries.
5. Read number of entries.
6. Repeat through steps 14 for l0 upto i<n in, steps of 1.
7. Print entry.
8. read i;
9. Print destination router.
10. Read table[i] dest.
11. print line
12. Read table[i].line.
13. print hops
14. Read table[i].hps.
15. Print full routing table for route.
16. Repeat through step 17 for u0 upto i<n in steps of 1.
17. Print table[i].dest,table[i].line,table[i].hps.
18. Print heirarchial routing table for route.
19. Repeat through step 21.6 for i0 upto i<n in steps of 1.
20. If (name[0]=table[i].dest[0]). Then print table[i]==table[i].dest[0]).then print
tab[i].line,table[i].hps).
21. Else repeat through steps-21.6.
21.1. min9999
21.2. pos0,k0.
21.3. repeat through
21.4. if(table[j].dest[0]==table[i].dest[0] then
21.4.1. kk+1.
21.4.2, if(table[j].hps<min) then.
21.4.2.1. mintable[j].hps
21.4.2.2. posj.
Computer Networks Lab Manual
38 / 53
39 / 53
40 / 53
CONGESTION ALGORITHMS
a) TOKEN BUCKET ALGORITHM
b) LEAKY BUCKET ALGORITHM
41 / 53
42 / 53
43 / 53
IP-ADDRESSING
PROGRAM DEFINITION:
To write a program for IP addressing classification problem.
PROGRAM DESCRIPTION:
In this program based on the type of classification we divide the IP
addresses. The user gives the input address based on conclusions st displays
the category of the address.
LOGIC:
1.The addresses are classified as per the rules.
2. The input from the user is taken in the form of address.
3. The program compares the first 8 bits of the address and classifies it
according to the respective class.
INPUT:
Enter the address:127.254.254.254
OUTPUT:
The address is a class A Address.
INPUT:
Enter the address:192.234.254.224
OUTPUT:
The address is a class C Address.
44 / 53
ENCRYPTION ALGORITHMS
a) SUBSTITUTION CIPHERS
b) TRANSPOSITION CIPHERS
c) DATA ENCRYPTION STANDARD ALGORITHM
45 / 53
SUBSTITUTION CIPHER
PROGRAM DEFINITION:
To write a program for substitution cipher
PROGRAM DESCRIPTION:
In this program, we use two functions namely sub(), trans() function. In the
substitution function we substitute each letter by some other letter. In the
transposition function we exchange the letters and by decrypted text. We print
the original text again.
LOGIC:
1. The string to be encrypted is taken from the user as input.
2. The characters of the string are substituted by other characters in this
program.
3. While substituting, a key is maintained throughout the substitution.
INPUT:
Enter the string RGMCET
OUTPUT:
The encrypted string for the given string is SHNDFU
46 / 53
TRANSPOSITION CIPHER
PROGRAM DEFINITION:
To write a program for substitution cipher
PROGRAM DESCRIPTION:
In this encryption technique, the characters of the string are not
substituted but are rearranged among themselves. A definite pattern is followed
for rearranging the characters and this pattern is followed throughout the
transposition.
LOGIC:
1. The string to be encrypted is taken from the user as the input.
2. A key word is written out .
3. All the characters of the input string are laid under each character of
the key in the form of a matrix.
4. Now the encrypted string is obtained as follows:
a. Transmit all the characters under the each alphabet of the key
in the alphabetical order.
b. The resultant string thus transmitted is the encrypted string.
Eg:
Key: RGMCET
String: all students are good
R
G
M
C
E
T
a
l
l
s
t
u
d
e
n
t
s
a
r
e
g
o
o
d
The transmitted string is:
stotsoleelngadruad.
INPUT:
Enter the string to be encrypted: all students are good
OUTPUT:
The transmitted string is
stotsoleelngadruad
47 / 53
48 / 53
IMPLEMENTATION OF SERVERS
a) DATE SERVER
b) CHAT SYSTEM
c) ECHO SERVER
49 / 53
DATE SERVER
PROGRAM DEFINITION:
This is the program that links the server and client and sends the data
from server to the client when client connects.
PROGRAM DESCRIPTION:
Program, when client connects to the server the server its data to the
client and also indication that the connection has been established.
ALGORITHMS:
Server program:
1. start
2. open the socket of client/server by serversocket
serversocket(888);
3. print connection established.
4. enter date into d by
Date d= new Date();
5. send it to client.
Client program
1. open socket using
Socket s= new socket(InetAddress.getLocalHost(),888);
2. print client started.
3. read date from socket
4. else print unable to connect
5. stop.
ss=new
TEST DATA:
[ Run both the program ]
Enter message : Hai
From server : connection established
22.03.06.
bye
50 / 53
CHAT SYSTEM
PROGRAM DEFINITION:
This is the program to connect both server and client and establishing
connection.
PROGRAM DESCRIPTION:
Program initially connects server to client and passes information form
from one system to another and viceversa that by creating / establishing
connection.
ALGORITHMS:
Server Process:
1. start.
2. create a class chats
3. open a socket by
server Socket ss=new ServerSocket(999);
4. print server started
5. accept the input from the client
6. replay for the client by function
pw=new PrintWriter(s.getoutputstream());
7. close the socket.
Client Process:
1. create a class chatc
2. open socket by
Socket s=new socket(InetAddress.getLocalHost(),999);
3. print client started
4. enter message
5. send to server
6. If message=bye
7. break the connection
8. close the socket
9. stop.
TEST DATA:
[ Run both the program ]
Enter message : hai
From Server : hai
At Server
: hai
Enter message at Client : krishna
At Server
: krishna
Enter message at Server : bye
Computer Networks Lab Manual
51 / 53
At Client
: bye
ECHO SERVER
PROGRAM DEFITION:
This program is developed to reply for clients action by establishing
connection.
PROGRAM DESCRIPTION:
Initially program connects the server to client and what ever the
information placed on the server by the client was read out by the server and
same was sent to the client. This causes the echoing system.
CLIENT PROGRAM:
PROGRAM DEFINITON:
This program is developed to send message for server when connection
established.
PROGRAM DESCRIPTION:
Initially program connects client to the server and what ever information
placed on the server by client was sent back to client. This causes the echoing
system.
Algorithm for server program
1.
2.
3.
4.
5.
6.
7.
start
create a class echos
create a socket by serversocket ss=new serversocket(999)
print server started
accept the input from client by function socket s=ss.accept();
print connection established
echo it to the client by using printwriter pw=new printwriter
(s.getOutputStream());
8. close the socket
Algorithm for client program
1. start
2. create a class achoc
3. create/open socket by socket s=new
socket(InetAddress.getLocalHost(),999);
4. print client started
5. enter message
6. it was sent back by server
7. if message=bye it breaks the connection
8. cloase the socket
52 / 53
TEST DATA:
[Run both the client and server programs]
Enter message: Krishna
From server: Krishna
Enter message: Rangaiah
From server: Rangaiah
Enter: bye
Connection sealed.
53 / 53