You are on page 1of 3

Labset1:

2. Write a program to divide the message into variable length frames and sort them
and display the message at the receiving side.

Program:
#include<iostream>
#include<cmath>
#include<stdlib.h>
using namespace std;

typedef struct
{
string data;
int randNo;
int seqNo;
int size;
}frame;

void insertSort(frame f[],int n,char ch)


{
int i,j;
frame v;
for(i=1;i<n;i++)
{
v=f[i];
j=i-1;

while(j>=0 && ((f[j].randNo>v.randNo &&ch=='r') ||(f[j].seqNo>v.seqNo &&ch=='s')))


{
f[j+1]=f[j];
j--;
}
f[j+1]=v;
}
}

void display(frame f[],int n)


{
int i;
cout<<"----------------------------------------------------------------"<<endl;
cout<<"Sequence_No\tRandom_No\tLength\t\tPacket"<<endl;
for(i=0;i<n;i++)
{
cout<<f[i].seqNo<<"\t\t"<<f[i].randNo<<"\t\t"<<f[i].size<<"\t\t"<<f[i].data<<endl;
}
}
int main()
{
string msg,res="";
int maxFrLen;
int i=0,j=0,n=0;

cout<<"Enter msg : ";cin>>msg;


cout<<"Enter len : ";cin>>maxFrLen;

frame f[msg.length()];

while(j<msg.length())
{
f[n].randNo=rand()%100;
f[n].seqNo=n+1;
f[n].size=rand()%maxFrLen+1;
f[n].data=msg.substr(j,f[n].size);
j+=f[n].size;
n++;
}

cout<<"Frames Created are:"<<endl;


display(f,n);

insertSort(f,n,'r');

cout<<"Frames Received as :"<<endl;


display(f,n);

insertSort(f,n,'s');

cout<<"Frames re-constructed\nReceiver End :"<<endl;


display(f,n);

for(i=0;i<n;i++)
res.append(f[i].data);
cout<<"\nReceived message = "<<res<<endl;
return 0;
}
Output:
Enter msg : helloworld
Enter len : 3
Frames Created are:
----------------------------------------------------------------
Sequence_No Random_No Length Packet
1 83 2 he
2 77 2 ll
3 93 2 ow
4 86 1 o
5 49 2 rl
6 62 2 d
Frames Received as :
----------------------------------------------------------------
Sequence_No Random_No Length Packet
5 49 2 rl
6 62 2 d
2 77 2 ll
1 83 2 he
4 86 1 o
3 93 2 ow
Frames re-constructed
Receiver End :
----------------------------------------------------------------
Sequence_No Random_No Length Packet
1 83 2 he
2 77 2 ll
3 93 2 ow
4 86 1 o
5 49 2 rl
6 62 2 d

Received message = helloworld

You might also like