Experiment no 08
Aim: Write a program for frame sorting technique used in buffers.
Theory:
The data link layer divides the stream of bits received from the network layer into
manageabledata units called frames.If frames are to be distributed to different systems on the
network, the Data link layer adds a header to the frame to define the sender and/or receiver of
the frame.
Each Data link layer has its own frame format. One of the fields defined in the format is the
maximum size of the data field. In other words, when datagram is encapsulated in a frame, the
total size of the datagram must be less than this maximum size, which is defined by restriction
imposed by the hardware and software used in the network.
The value of MTU differs from one physical network to another In order to make IP protocol
portable/independent of the physical network, the packagers decided to make the maximum
length of the IP datagram equal to the largest Maximum Transfer Unit (MTU) defined so far.
However for other physical networks we must divide the datagrams to make it possible to pass
through these networks. This is called fragmentation.
When a datagram is fragmented, each fragmented has its own header. A fragmented
datagrammay itself be fragmented if it encounters a network with an even smaller MTU. In
another words, a datagram may be fragmented several times before it reached the final
destination and also, the datagrams referred to as (frames in Data link layer) may arrives out of
order at destination. Hence sorting of frames need to be done at the destination to recover the
original data.
ALGORITHM/FLOWCHART:
Step 1: Start
Step 2: Enter the Message to be transmitted(read in msg) Step3:Caluculate no of packets
Step4: Goto Step 5,6,7,8 Step5:void shuffle(intNoOfPacket)
Step 5.1: Calculate status,transdata Step 5.2:for i = 0; i <NoOfPacket;
Step 5.2.1: calculate trans
trans = rand()%NoOfPacket; Step 5.2.2: if Status[trans]!=1
Copy read data in transmitted data and goto step 6 Step 6: void receive(intNoOfPacket)
Step 6.1:Print the packets received
Step 6.2: Print the frame in sorted order go to Step 7 Step 7: void sortframes(intNoOfPacket)
Step 7.1 : if transdata[j].SeqNum>transdata[j + 1].SeqNumgoto 7.1.1 Step 7.1.1: Sort the
packets
Step 8: Stop
Program:
#include <stdio.h>
#include <stdlib.h>
#define MAX_FRAMES 50 // Maximum number of frames
// Function to sort frames based on sequence number (Bubble Sort)
void sortFrames(int frames[], int n) {
int i, j, temp;
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (frames[j] > frames[j + 1]) {
temp = frames[j];
frames[j] = frames[j + 1];
frames[j + 1] = temp;
}
}
}
}
int main() {
int frames[MAX_FRAMES], n, i;
// Input: Number of frames
printf("Enter the number of frames received: ");
scanf("%d", &n);
// Input: Frame sequence numbers (unordered)
printf("Enter the sequence numbers of the frames received:\n");
for (i = 0; i < n; i++) {
printf("Frame %d: ", i + 1);
scanf("%d", &frames[i]);
}
// Sort frames in order
sortFrames(frames, n);
// Display sorted frames
printf("\nFrames after sorting (Correct Order):\n");
for(i=0; i<n; i++){
printf("Frame %d received\n", frames[i]);
}
return 0;
}
Output:
Input:
Enter the number of frames received: 5
Enter the sequence numbers of the frames received:
Frame 1: 4
Frame 2: 1
Frame 3: 3
Frame 4: 5
Frame 5: 2
Output (Simulation Example)
Frames after sorting (Correct Order):
Frame 1 received
Frame 2 received
Frame 3 received
Frame 4 received
Frame 5 received