Vous êtes sur la page 1sur 53

COMPUTER NETWORKS LAB MANUAL

Prepared by

Department of Computer Science & Engineering RAO & NAIDU ENGINEERING COLLEGE

Computer Networks Lab Manual

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.

The C Programming Language


In this lab you will spend some time getting acquainted with the C programming language. Rather than provide you with a step-by-step tutorial in the language, we will discuss the most significant differences from C++, which you're assumed to know. For the experienced C++ programmer, the C language will look much more lowlevel, as it indeed is. We discuss some of the features of the language in more detail later on in this lab, but point out the most salient differences you will encounter in C coming from C++: There are no classes. C is not an object-oriented language. Comments must be enclosed in /* ... */, single-line comment // is supported only in the more recent versions of C. The nesting of comment blocks is not allowed. Strings are simply null-terminated arrays of char. Type checking for function arguments may be quite lax, meaning that more burden is on the programmer. There isn't stream-based input/output (cin, cout). The available operations for I/O seem crude because they are lower-level.

Computer Networks Lab Manual

2 / 53

Dynamic memory allocation is done via library functions such as malloc, calloc, and free (no new / delete). You allocate and deallocate arrays of bytes instead of "space for a data-type". Parameter passing is exclusively by value. If you need to modify a parameter passed into a function, you must pass the pointer to the parameter. There exist "dialects" of the C language, which define some minor syntactical differences and alternatives to type-checking. The dialect used depends, of course, on the compiler you use. Three of the most common dialects are: Kernighan & Ritchie C (or simply K&R C, named after the creators of the language) . ANSI C (which has is much stricter about type-checking than K&R C).

The BSD Socket API


Sockets are like Unix pipes in that they use file descriptors to define the endpoints of a communication channel. When you make the system call to create a pipe, you pass in the pointer to an integer array with two elements. The successful competion of the pipe call returns 0 and initializes the array with two file descriptors, one to which you can write and one from which you can read. The socket follows a more complex set up process which we describe below. Creating a socket #include <sys/types.h> #include <sys/socket.h> int socket(int domain, int type, int protocol);

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:

Computer Networks Lab Manual

3 / 53

SOCK_DGRAM: provides datagram communication semantics, that is, this is a connectionless protocol that will do its best to deliver the data without making any promises that the service is reliable. There are no guarantees that the datagrams are delivered in the order they are sent. SOCK_STREAM: provices a bidirectional virtual circuit that is reliable and order-preserving (FIFO). SOCK_RAW: provides the ability to send packets directly to a network device driver, enabling user space applications to provide networking protocols that are not implemented in the kernel. The last parameter, protocol, can be left unspecified (value 0) so that the default protocol that supports the protocol family and socket type is used. In case multiple protocols exist for the selected tuple <domain, type>, then a specific protocol must be specified. After the successful completion of the socket call, what you have is a connection endpoint that is not attached anywhere. Before any communication can happen, the socket must be associated to "something". The process of connecting the socket is an asymmetric task, that is, it is performed differently at each endpoint of the socket. Server applications (which run on "infinite loops"), create the socket, get it ready to be hooked up to something, and then wait for someone to request a connection to the socket. Client processes, on the other hand, create a socket, tell the system to which address they want to connect it, and attempt establishing the connection to the server. The server process then accepts the connection request and the socket is finally ready for communication. Binding an address to a socket Before we proceed, you need to understand what an Internet address and how it is represented in Unix. Header file <netinet/in.h> defines a 32-bit address for an Internet host. It actually identifies a specific network interface on a specific system on the Internet using the data structure below:

struct in_addr { __u32 s_addr; };

Computer Networks Lab Manual

4 / 53

Demonstration of How a Header File works:


/****************************************************************** * * * decls.h is a demonstration file to show how header file works.* * * ******************************************************************/ int i; /* a integer variable */ float x; /* a float variable */

**************
/**************************************************************** * * * 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; }

Computer Networks Lab Manual

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. 11. IP-ADDRESSING 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 Computer Networks Lab Manual 6 / 53

SIMULAION OF OSI MODEL

Computer Networks Lab Manual

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.

Computer Networks Lab Manual

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

Computer Networks Lab Manual

9 / 53

IMPLEMTATION OF DATA LINK FRAMING METHODS b) CHARACTER COUNT c) BIT STUFFING AND DE STUFFING d) CHARACTER STUFFING AND DE STUFFING

Computer Networks Lab Manual

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

Computer Networks Lab Manual

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

Computer Networks Lab Manual

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

Computer Networks Lab Manual

13 / 53

PARITY CHECK a) b) SINGLE DIMENSIONAL DATA MULTI DIMENSIONAL DATA

Computer Networks Lab Manual

14 / 53

SINGLE DIMENTIONAL (EVEN / ODD) PARITY


AIM: Implementation of data link framing methods for even and odd parity LOGIC: 0s and 1s are appended to the data according to the parity of the data. PSEUDO CODE: /* for given data */ 1. if data bit is 1 increment count else count is zero /* even parity */ 2. if the count mod 2 is zero then append the next bit as 0 else 1 /* odd parity */ 3. if the count mod 2 is zero then append the next bit as 1 else 0 INPUT: Enter the string 1111101 Enter the choice 1. even parity 2. odd parity 1 OUTPUT: The string is 11111010 INPUT: Enter the string 111101 Enter the choice 1. even parity 2. odd parity 2 OUTPUT: The string is 1111010

Computer Networks Lab Manual

15 / 53

MULTI DIMENSIONAL PARITY CHECK


AIM: Implementation of data link framing method for horizontal row check and vertical column check for multi dimensional data LOGIC: 0s and 1s are appended to the data accessing to the parity of the data. PSEUDO CODE: /* For given data */ 1. if data bit is 1 increment count in row wise else count is zero. 2. if data bit is 1 increment cn in column wise else cn is zero. /* horizontal row check */ 3. if count mod 2 is zero append the next bit as 0 else 1 /*vertical column check */ 4. if cn mod 2 is zero append the next bit as 1 else 0 /* comparing horizontal and vertical parity checks */ 5. if both the horizontal and vertical parity checks are equal then no error else error occurred at certain row and at certain column. INPUT: Enter size of data: 33 enter data: 101 101 111 Horizontal parity: 001 Vertical parity: 111 Enter received data: 111 101 111 Horizontal parity: 101 Vertical parity: 101 error is at 1 row 2 column The corrected data is: 101 101 111

Computer Networks Lab Manual

16 / 53

CYCLIC REDUNDANCY CHECH (CRC)

Computer Networks Lab Manual

17 / 53

IMPLEMENTATION OF CRC POLYNOMIALS

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

Computer Networks Lab Manual

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: Frames after appending zeros: 1101011011000000000000 Remainder: 111101001100 Transmitted data: 1101011011111101001100 Received data: 1101011011111101011100 Remainder: 100 Error

Computer Networks Lab Manual

19 / 53

IMPLEMENTATION OF DATA LINK PROTOCOLS a) UNRESTRICTED SIMPLEX PROTOCOL b) STOP AND WAIT PROTOCOL c) NOISY CHANNEL

Computer Networks Lab Manual

20 / 53

UNRESTRICTED SIMPLEX PROTOCOL


AIM: To implement unrestricted simplex protocol that is sender sends the data from the DLL of source machine to the DLL of the destination machine without any sequence no. or acknowledgement. LOGIC: In this protocol data are transmitted in one direction only. Both the transmitting and receiving network layers are always ready processing time can be ignored. Infinite buffer the data link layers never damages or losses frames. This is an unrestricted protocol. This protocol is also called as Utopia. PSEUDO CODE: void sender1(void) { frame s; packet buffer; while(true) { from_network_layer(&buffer); s.info=buffer; to_physical_layer(&s); } }/* end of sender1*/ void receiver1(void) { frame r; event_type event; while(true) { wait_for_event(&event); from_physical_layer(&r); to_network_layer(&r.info); } }/* end of receiver1*/

Computer Networks Lab Manual

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

Computer Networks Lab Manual

22 / 53

SIMPLEX PROTOCOL FOR A NOISY CHANNEL


AIM: To implement simplex protocol for noisy channel in data link layer LOGIC: The same scenario which we have implemented in the simplex stop and wait protocol is implemented in this protocol with the help of timers PSEUDO CODE: Void sender2(void) { frame s; packet buffer; event_type event; while(true) { from_network_layer(&buffer); s.info=buffer; to_physical_layer(&s); wait_for_event(&event); } } /*end of sender2*/ void receiver2(void) { frame r,s; event_type event; while(true) { wait_for_event(&event); from_physical_layer(&r); to_network_layer(&r.info); to_physical_layer(&s); } } /*end of receiver2*/

Computer Networks Lab Manual

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

Computer Networks Lab Manual

24 / 53

STOP AND WAIT PROTOCOL


AIM: To implement the stop and wait protocol in DLL LOGIC: The sender must never transmit a new frame until undone has been fetched from physical layer, then the new one overwrites the old one. The general solution to this problem is to have the receiver send a little dummy frame to t5he sender which , In effect gives the sender permission to transmit the next frame. Protocol in which in-effect dummy frame back to the sender which in effect gives the sender permission to transmit the next frame. Protocols in which sender sends one frame and then waits for acknowledgement before proceeding are called stop and wait protocols. PSEUDO CODE: void sender3(void) { seq_nr next_frame_to_send; frame s; packet buffer; event_type event; next_frame_to_send=0; from_network_layer(&buffer); while(true) { s.info=buffer; s.seq=next_frame_to_send; to_physical_layer(&s); start_timer(s.seq); wait_for_event(&event); if(event==frame_arrival) { from_network_layer(&buffer); inc(next_frame_to_send); } } } /*end of sender3*/

void receiver3(void) { seq_nr frame_expected; frame s,r;

Computer Networks Lab Manual

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

Computer Networks Lab Manual

26 / 53

IMPLEMENTATION OF SLIDING WINDOW PROTOCOLS a) ONE BIT SLIDING WINDOW PROTOCOL b) GO BACK N SLIDING WINDOW PROTOCOL c)SELECTIVE REPEAT SLIDING WINDOW PROTOCOL

Computer Networks Lab Manual

27 / 53

ONE BIT SLIDING WINDOW PROTOCOL


AIM: To achieve flow control in the data link layer at the receivers side efficiently using one bit sliding window protocol. LOGIC: 1. The starting machine fetches the first packet from its network layer, builds a frame and sent it 2. When this frame arrives the receiving data link layer checks to see if it is duplicate. 3. If the frame is given expected it is passed to the network layer and the receiver window is slid up.

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;

Computer Networks Lab Manual

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

Computer Networks Lab Manual

29 / 53

GO BACK N SLIDING WINDOW PROTOCOL


AIM: Implementation of the Go Back N sliding window protocol LOGIC: Two basic approaches are available foe dealing with errors in the presence of the pipelining. One way, called go back n, is for the receiver simply to discard all subsequent frames, sending no acknowledgements for the discarded frames. This strategy corresponds to a receive window of size 1. In other words, the data link layer refuses to accept any frame except the next one it must give to the network layer. If the senders window fills up before the timer runs out, the pipeline will begin to empty. Eventually, the sender will time out and retransmit all unacknowledged frames in order, starting with the damaged or lost one. This approach can waste a lot of bandwidth if the error rate is high. We see go back n for the case in which the receivers window is large. Frames 0 and 1 are correctly received and acknowledged. Frame 2 however, is damaged or lost. The sender unaware of this problem, continues to send frames until the timer for frame 2 expires. Then it backs up frame 2 and starts all over with it, sending 2, 3, 4, etc all over again. PSEUDO CODE: Void protocol5(void) { MAX3SEQ > 1; used for outbound stream oldest frame as yet unacknowledged next frame expected on inbound stream initialize scratch variable buffers for the outbound stream while(true) { four possibilities: see event_type above the network layer has a packet to send fetch new packet expand the senders window transmit the frame advance senders upper window edge a data or control frame has arrived get incoming frame from physical layer } } /*end of protocol5*/

Computer Networks Lab Manual

30 / 53

INPUT AND OUTPUT: Enter the no. of frames: 2 Enter the msg: ab Transmission in one direction M1=80 Error in transmission re-send all the frames Enter the msg: df Transmission in one direction M1=86 Frame arrived Acknowledgement to network layer Transmission in opposite direction M1=86 Enter thr msg: df Transmission in one direction M1=93 Frame arrived Acknowledgement to network layer Transmission in opposite direction M1=93

Computer Networks Lab Manual

31 / 53

SELECTIVE REPEAT SLIDING WINDOW PROTOCOL


AIM: Implementation of Selective repeat sliding window protocol LOGIC: In this protocol, both sender and receiver maintain a window of acceptable sequence numbers. The senders window size starts out at 0 and grows to some predefined maximum, MAX3SEQ. the receivers window, in contrast, is always fixed in size and equal to MAX3SEQ. the receiver has a buffer reserved for each sequence number within its fixed window. Associated with each buffer is a bit(arrived) telling whether the buffer is full or empty. Whenever a frame arrives, its se4quence number is checkerd by the function between() to see if it falls within the window. If so and if it has not already been received , it is accepted and stored. This action is taken without regard to whether or not it contains the next packet expected bt the network layer. Of course it must be kept within the datalink layer and not passed to the network layer until all the lower numbered frames have already been delivered to the network layer In the correct order. PSEUDO CODE Void protocol6(void) { initialize lower edge of the senders window initialize upper edge of the senders window+1 initialize lower edge of the receivers window initialize lower edge of the receivers window+1 initialize index into buffer pool initialize scratch variable buffers for the outbound stream buffers for the inbound stream inbound bit map how many output buffers currently used initialize next ack expected on the inbound stream number of next outgoing frame initially no packets are buffered while(true) { accept,save, and transmit a new frame expand the window fetch new packet transmit the frame advance upper window edge a data or control frame has arrived fetch incoming frame from the physical layer advance lower edge of senders window

Computer Networks Lab Manual

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.

Computer Networks Lab Manual

33 / 53

IMPLEMENTATION OF ROUTING ALGORITHMS a) DIJKSTRAS ALGORITHM b) DISTANCE VECTOR ROUTING c) HIREARCHICAL ROUTING d) BROADCAST ALGORITHM

Computer Networks Lab Manual

34 / 53

TO COMPUTE SHORTEST PATH USING DIJKSTRAS ALGORITHM


AIM: To implement shortest path in a subnet using dijkstras algorithm from a given node LOGIC: A subnet with number of paths and infinite node is taken and proceeded As algorithm proceeds the labels change with better values and shortest path is found PSEUDO CODE: 1. start. 2. total=0,k=0 3. read the number of nodes 4. read the number of edges. 5. repeat step 6 for i=1 to 10 in steps of 1 6. repeat step 6.1 for j=1 to 10 in steps of 1 dist[i][j]=-1 7. read the distance 8. repeat step 9 for i=1 to e in steps of 1 9. display i repeat for j=0 to 2 in steps of 1 read a[i][j] read dist(a[i][j])(a[i][1]) 10. read the sorce I,e,s 11. read the destination i,e,d 12. x=s 13. repeat for l=0 to e in steps of 1 14. i=s repeat for j=1 to n in steps of 1 if (dist[i][j]!=-1) then if dist[i][j]<min then min=dist[i][j] b=j s=b total=total+min min=1000 path[k++]=b if b==d then display x repeat for i=0 to k in steps of 1 display path[i] display total

Computer Networks Lab Manual

35 / 53

15. read the choice 16. if ch==y then goto step 2 17. stop. INPUT: We first enter the file name as pa.txt and we enter the nodes as 4 Then the distance matrix is 9 0 2 5 0 2 0 3 6 5 3 0 4 0 6 4 6 which is in the file pa.txt we enter the source node as 1 enter the destination node 4 OUTPUT: the path is 1->2->4

Computer Networks Lab Manual

36 / 53

DISTANCE VECTOR ROUTING


AIM: To obtain routing table of desired node with new weight along with the predefined weights of a subnet LOGIC: The program adds the respective new weights of the neighbor of the desired node to the predefined weights of the node in the subnet and chooses the node with least weight and prints the weight and respective neighbor of node PSEUDO CODE: 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 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

Computer Networks Lab Manual

37 / 53

IMPLEMENTATION OF HIERARCHIAL ROUTING ALGORITHM


PROGRAM DEFINITION: This program deals with the implementation of Hierarchical routing algorithm. PROGRAM DESCRIPTION: In this program, we use only one function namely main( ) and we do everything here only. We enter route no and the no of entries in the routing table and we enter each entry, destination router, line and no of hops and we pring full routing table and hierarchial routing table.

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

21.5. print table[pos].dest[0],table[pos].line,table[pos].hps. 21.6. i i+k-1. 22. getch(); 23. stop. TEST DATA Enter the router name :1a. Enter the number of entries :3. Entry : 0. Destination router : 1b. Line : 1a. Number of hops : 1. Entry : 1. Dtestination router : 1d. Line : 1a. Number of hops : 2. Entry : 2. Destination router : 2c. Line : 1b. Number of hops : 4. Full routing table for router 1a. Dest Line Hops 1b 1a 1 1d 1a 2 2c 1b 4

Computer Networks Lab Manual

39 / 53

BROAD CAST ALGORITHM


AIM: Sending a packet to all destinations simultaneously using broadcast algorithm LOGIC: 1. Broad cast refers to sending a packet to all destinations simultaneously 2. One broadcasting methos that requires no special features from subnet is for source to simply send a distinction packet to each destination 3. This broad cast algorithm makes explicit use of sink tree for the router initiating the broadcast, or any other convenient spanning tree. 4. This spanning tree is a subset of subnet includes all routers but contain no loops . PSEUDO CODE: 1. When packet arrives at router, it checks to see if packet arrived on time. 2. If so, there is an excellent chance that broadcast packet followed the best route from router and therefore first copy arrived at router. 3. This being the case, the router forwards copies of it on all lines except the one it arrived on. 4. If however the broadcast packet arrived on a line other than the preferred one for reaching the source the packet is discarded as a likely duplicate. INPUT: 1. We have entered the number of nodes and adjacency matrix for nodes. 2. We have entered the source for broadcasting. OUTPUT: The output is minimal spanning tree for given source node.

Computer Networks Lab Manual

40 / 53

CONGESTION ALGORITHMS a) TOKEN BUCKET ALGORITHM b) LEAKY BUCKET ALGORITHM

Computer Networks Lab Manual

41 / 53

TOKEN BUCKET ALGORITHM


AIM: To implement the Token Bucket Congestion Algorithm. DESCRIPTION: In this algorithm, the leaky bucket holds tokens, generated by a clock at the rate of one token every T sec. the token bucket algorithm does allow saving, up to the maximum size of the bucket,n. LOGIC: The implementation of the token bucket algorithm is just a variable that counts tokens. The counter is incremented by one every T and decremented by one whenever a packet is sent. When the counter hits zero, no packets may be sent. In the byte count variant, the counter is incremented by k bytes every T and decremented by the length of each packet sent.

Computer Networks Lab Manual

42 / 53

LEAKY BUCKET ALGORITHM


AIM: To implement the Leaky Bucket algorithm. DESCRIPTION: This algorithm is a single-server queuing system with constant service time.The host is allowed to put one packet per clock tick onto the network. When the packets are of variable size, it is often better to allow a fixed number of bytes per tick, rather than just one packet. Thus if the rule is 1024 bytes per tick, a single 1024 byte packet can be admitted on a tick, two 512 byte packets, four 256 byte packets, and so on. If the residual count is too low, the next packet must wait until the next tick. LOGIC: The leaky bucket consists of a finite queue. When a packet arrives, if there is room on the queue, it is appended to the queue; otherwise it is discarded. At every clock tick, one packet is transmitted(unless the queue is empty). The byte-counting leaky bucket is implemented in almost the same way. At each tick, a counter is initialized to n. if the first packet on the queue has fewer bytes than the current value of the counter , it is transmitted, and the counter is decremented by that number of bytes. Additional packets may also be sent, as long as the counter is high enough. When the counter drops below the length of the next packet on the queue, transmission stops until the next tick, at which time the residual count is overwritten and lost.

Computer Networks Lab Manual

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.

Computer Networks Lab Manual

44 / 53

ENCRYPTION ALGORITHMS a) SUBSTITUTION CIPHERS b) TRANSPOSITION CIPHERS c) DATA ENCRYPTION STANDARD ALGORITHM

Computer Networks Lab Manual

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

Computer Networks Lab Manual

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

Computer Networks Lab Manual

47 / 53

DATA ENCRYPTION ALGORITHM


AIM: To encrypt and decrypt the given text using data encryption algorithm. LOGIC: 1. DES consists of 2 permutation steps in which all 64 bits are permuted with 16 identical rounds of operation in between. 2. The operation of each round is identical, taking the output of previous round as input. METHOD: 1. A 64 bit plain text block is given as input. 2. This block is subjected to Initial Permutation. 3. At this stage the 64 bit key is used along with the above obtained data to produce an intermediate result. 4. The above step is performed for 16 rounds. 5. In each round , the following steps are performed: a. Expansion/permutation(E Table) of right most 32 bits of data. b. XOR of above data with the key. c. Subject to S Box. d. Permutation. e. XOR of the above block with left 32 bits of data. 6. Then the data is swapped in 32 bit blocks. 7. After the swapping, inverse initial permutation is performed on the swapped block and thus the final encrypted block is obtained. INPUT: Enter a 10 bit key:1010101010 Enter the 8 bit plain text:11111111 LS2=10110 LS-2=00111 K1=1110011 K2=01011111 Concat=1110 e=01000100 We have entered 10 bit key Next, we have entered the 8 bit plain text. OUTPUT: We obtain encrypted data for given 8 bit plain text successfully.

Computer Networks Lab Manual

48 / 53

IMPLEMENTATION OF SERVERS a) DATE SERVER b) CHAT SYSTEM c) ECHO SERVER

Computer Networks Lab Manual

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

Computer Networks Lab Manual

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 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 1. 2. 3. 4. 5. 6. 7.

Computer Networks Lab Manual

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.

Computer Networks Lab Manual

53 / 53