Question Bank For Computer Network Lab
Question Bank For Computer Network Lab
#include<stdio.h>
#define bucketsize 1000
#define n 5
void bucketoutput(int *bucket,int op)
{
if(*bucket > 0 && *bucket > op)
{
*bucket= *bucket-op;
printf("\n%d-outputed remaining is %d",op,*bucket);
}
else if(*bucket > 0)
{
printf("\nRemaining data output = %d",*bucket);
*bucket=0;
} } int
main() {
int op,newpack,oldpack=0,wt,i,j,bucket=0;
printf("enter output rate");
scanf("%d",&op); for(i=1;i<=n;i+
+)
{
newpack=rand()%500;
printf("\n\n new packet size = %d",newpack);
newpack=oldpack+newpack;
wt=rand()%5; if(newpack<bucketsize)
bucket=newpack;
else {
printf("\n%d = the newpacketand old pack is greater than bucketsize reject",newpack);
bucket=oldpack;
}
printf("\nThe data in bucket = %d",bucket); printf("\n
the next packet will arrive after = %dsec",wt);
for(j=0;j<wt;j++)
{
bucketoutput(&bucket,op);
sleep(1); }
oldpack=bucket;
} while(bucket>0)
bucketoutput(&bucket,op)
;
return 0;
}
Result: Thus the program is written and executed in C language for congestion control using
leaky bucket algorithm.
{
printf("\nSENDER: ACK for Frame %d Received.\n",i-1);
}
printf("\nSENDER: Frame %d sent, Waiting for ACK...\n",i);
ACK=NO;
wait_time= rand() % 4+1;
if (wait_time==TIMEOUT)
{
printf("SENDER: ACK not received for Frame %d=>TIMEOUT Resending Frame...",i);
} else
{ sleep(RT
T);
printf("\nRECEIVER: Frame %d received, ACK sent\n",i);
printf("--------------------------------------------");
ACK=YES;
i++; } }
return 0;
}
4 PING program
import java.util.*; public
class PingDemo
{
public void Ping(String host)
{ try
{
Process p=Runtime.getRuntime().exec("Ping "+host); Scanner scan=new
Scanner(p.getInputStream()); while(scan.hasNextLine())
{
System.out.println(scan.nextLine());
} } catch(Exception
ex)
{
System.out.println("error"+ex);
}}
public static void main(String args[])
{
PingDemo P1=new PingDemo();
P1.Ping(args[0]);
}
}
30
#define IN 99 #define
N6
int dijkstra(int cost[][N],int source,int target); char
interface[6][6][20]={{"0","0","0","0","0","0"},{"0","0","192.1.1.1","0","200.1.1.1"},{"0","0","0
","0",
"198.1.1.2","0"},{"0","192.1.1.3","0","198.1.1.3","0","200.1.1.2"},{"0","0","200.1.1.3","0","200
.1.1.4 ","0"}}; int main() {
int cost[N][N],i,j,w,ch,co;
char ip[10]; int
source,target,x,y;
printf("\t the shortest path algorithm(DIJKSRTRA'S ALGORITHM in c\n\n");
for(i=1;i<N;i++) for(j=1;j<N;j++) cost[i][j]=IN; for(x=1;x<N;x++)
{
for(y=x+1;y<N;y++)
{
printf("enter the weight of the path between node%d and %d:",x,y); scanf("%d",&w);
cost[x][y]=cost[y][x]=w;
} printf("\n");
} for(x=1;x<N;x++)
{ for(y=1;y<N;y++)
{
printf("%s:\t",interface[x][y]); //scanf("%s",&ip);
//interface[x][y][20]=ip;
} printf("\n"); } printf("\nEnter the
source:"); scanf("%d",&source);
printf("\nEnter the target");
scanf("%d",&target);
co=dijsktra(cost,source,target);
printf("\nThe Shortest Path:
%d",co); return 0; }
int dijsktra(int cost[][N],int source,int target)
31
{
int dist[N],prev[N],selected[N]={0},i,m,min,start,d,j,x,y;
char path[N];int path1[N]; for(i=0;i<N;i++)
{ dist[i]=IN; prev[i]=-1; }
start=source;
selected[start]=1;
dist[start]=0;
while(selected[target]==0)
{
min=IN; m=0;
for(i=1;i<N;i++)
{
d=dist[start]+cost[start][i]; if(d<dist[i]&&selected[i]==0)
{ dist[i]=d;
prev[i]=start;
}
if(min>dist[i]&&selected[i]==0)
{ min=dist[
i]; m=i; }
} start=m;
selected[start]=1; }
start=target; j=0;
while(start!=-1)
{ path[j+
+]=start+64;
path1[j++]=start;
start=prev[start]; }
path[j]='\0';
strrev(path);
printf("%s",path);
printf("\n");
for(j=j-1;j>=0;j--)
{
printf("%d\t",path1[j]);
if(j>0) { x=path1[j];
y=path1[j-1];
printf("%s\t%s\n",interface[x][y],interface[y][x]);
}
}
return dist[target];
}
for(i=1;i<=no;i++)
{ router[r].a[i][1]=i;
router[r].a[i][2]=999;
router[r].a[i][3]=nul;
} router[r].a[r][2]=0;
router[r].a[r][3]=r;
} void inp(int
r) {
int i;
printf("\nEnter distance from the node %d to other nodes",r); printf("\nPls
Enter 999 of there is no direct route\n",r); for(i=1;i<=no;i++)
{ if(i!=r)
{
printf("\nEnter dist to node %d:",i);
scanf("%d",&router[r].a[i][2]); router[r].a[i][3]=i;
}
} } void
display(int r)
{
int i,j;
printf("\n\nThe routing table for node %d is as follows:",r); for(i=1;i<=no;i++)
{
if(router[r].a[i][2]>=99)
printf("\n\t\t\t%d\tno link\tno hop",router[r].a[i][1]); else
printf("\n\t\t\t%d\t\t%d",router[r].a[i][1],router[r].a[i][2],router[r].a[i][3]);
} } void
dv_algo(int r)
{ int i,j,z;
for(i=0;i<=no;i++)
{
if(router[r].a[i][2]!=999&&router[r].a[i][2]!=0)
{ for(j=1;j<=no;j++)
{
z=router[r].a[i][2]+router[i].a[j][2];
if(router[r].a[j][2]>z)
{ router[r].a[j][2]=z;
router[r].a[j][3]=i;
}
}
}
} } int
main() { int
i,j,x,y; char
choice;
printf("Enter the no.of nodes required(less than 10 pls):");
scanf("%d",&no); for(i=1;i<=no;i++)
{
init(i);
inp(i); }
printf("\nTne configuration of the nodes after initialization is as follows:");
for(i=1;i<=no;i++) display(i); for(i=1;i<=no;i++) dv_algo(i);
printf("\nThe configuration of the nodes after computation of paths is as follows:");
for(i=1;i<=no;i++) display(i); while(1) {
printf("\n\nWanna continue (y/n):");
scanf("%c",&choice);
if(choice=='n') break;
printf("\nEnter the nodes btn which the shortest path is to be found:\n"); scanf("%d%d",&x,&y);
printf("\nThe length of the shortest path is %d",router[x].a[y][2]);
}
}
catch (Exception e) {
System.out.println(e);
}
}
} Server: import
java.io.*; import
java.net.*; import
java.util.*; class
Serverfile {
public static void main(String args[])
{ try
{
String s;
while((s=b.readLine())!=null)
{
System.out.println(s); dout.writeBytes(s+'\n');
}
f.close(); dout.writeBytes("-1\n");
}}
catch(Exception
e)
{
System.out.println(e);
}
}
}
9 \\ To write a socket program for TRACE ROUTE in Java.
//pingclient.java import
java.io.*; import
java.net.*; import
java.util.Calendar; class
pingclient
{
public static void main(String args[])throws Exception
{ String
str; int
c=0; long
t1,t2;
Socket s=new Socket("127.0.0.1",5555);
DataInputStream dis=new DataInputStream(s.getInputStream());
PrintStream out=new PrintStream(s.getOutputStream());
while(c<4) {
t1=System.currentTimeMillis(); str="Welcome
to network programming world";
out.println(str);
System.out.println(dis.readLine()); t2=System.currentTimeMillis();
System.out.println(";TTL="+(t2-t1)+"ms"); c++;
}
s.close();
}
//pingserver.java
import java.io.*; import
java.net.*; import
java.util.*; import
java.text.*; class
pingserver
{
public static void main(String args[])throws Exception
{
ServerSocket ss=new ServerSocket(5555); Socket
s=ss.accept(); int c=0;
while(c<4)
{
DataInputStream dis=new DataInputStream(s.getInputStream());
PrintStream out=new PrintStream(s.getOutputStream()); String
str=dis.readLine();
out.println("Reply from"+InetAddress.getLocalHost()+";Length"+str.length()); c++;
}
s.close();}}
48
System.out.println(e.getMessage());
}
}
}