0% found this document useful (0 votes)
7 views18 pages

Question Bank For Computer Network Lab

The document is a question bank for a Computer Network Lab, containing programming assignments in C and Java related to various networking protocols and algorithms. It includes tasks such as implementing congestion control using the leaky bucket algorithm, Stop and Wait Protocol, Sliding Window Protocol, and routing algorithms like distance vector and link state. Additionally, it covers socket programming for PING, TRACE ROUTE, file transfer using TCP/IP, and demonstrates the usage of the nslookup command.

Uploaded by

Vkdm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views18 pages

Question Bank For Computer Network Lab

The document is a question bank for a Computer Network Lab, containing programming assignments in C and Java related to various networking protocols and algorithms. It includes tasks such as implementing congestion control using the leaky bucket algorithm, Stop and Wait Protocol, Sliding Window Protocol, and routing algorithms like distance vector and link state. Additionally, it covers socket programming for PING, TRACE ROUTE, file transfer using TCP/IP, and demonstrates the usage of the nslookup command.

Uploaded by

Vkdm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Question Bank for Computer Network Lab

1 Write a program for congestion control using leaky bucket algorithm in C


language and execute the same and display the result
2 Implement Stop and Wait Protocol in a C program and execute the same and
display the result.
3 Implement Sliding Window Protocol in a C program and execute the same
and display the result.
4 To write a socket program for PING in Java.
5 To implement the distance vector routing algorithm.
6 To implement link state routing algorithm.
7 To write a program in java to implement IP address configuration..
8 To Perform File Transfer in Client & Server Using TCP/IP.
9 To write a socket program for TRACE ROUTE in Java.
10 To write a program in java to demonstrate the usage of nslookup command.
1 \\ program for congestion control using leaky bucket algorithm in C language

#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.

2 \\ Implement Stop and Wait Protocol in a C program


Program:
#include <stdio.h>
#include <stdlib.h>
#define RTT 4
#define TIMEOUT 4 #define
TOT_FRAMES 7
enum {NO,YES} ACK;
int main() { int
wait_time,i=1;
ACK=YES;
for(;i<=TOT_FRAMES;)
{
if (ACK==YES && i!=1) CCN LAB 17ECL68
Dept. of E&CE, VSMSRKIT, Nipani

{
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;
}

3 \\ Implement Sliding Window Protocol in a C program


Program:
#include <stdio.h>
#include <stdlib.h> #define
RTT 5 int main() { int
window_size,i,f,frames[50];
printf("Enter window size: ");
scanf("%d",&window_size);
printf("\nEnternumber of frames to transmit: ");
scanf("%d",&f);
printf("\nEnter %d frames: ",f);
for(i=1;i<=f;i++) scanf("%d",&frames[i]);
printf("\nAfter sending %d frames at each stage sender waits for ACK",window_size);
CCN LAB 17ECL68
Dept. of E&CE, VSMSRKIT, Nipani

printf("\nSendingframes in the following manner....\n\n"); for(i=1;i<=f;i++)


{
if(i%window_size!=0)
{
printf("%d",frames[i]);
} else
{
printf(" %d\n",frames[i]); printf("SENDER: waiting
for ACK...\n\n"); sleep(RTT/2); printf("RECEIVER:
Frames Received, ACK Sent\n");
printf("-------------------------------------------\n");
sleep(RTT/2);
printf("SENDER:ACK received, sending next frames\n");
}
}
if(f%window_size!=0)
{
printf("\nSENDER: waiting for ACK...\n");
sleep(RTT/2); printf("\nRECEIVER:Frames Received,
ACK Sent\n");
printf("-------------------------------------------------\n");
sleep(RTT/2);
printf("SENDER:ACK received.");
} 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]);
}
}

5 \\ To implement link state routing algorithm.


LINK STATE ROUTING:
#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<string.h>
#include<math.h>

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];
}

6 \\ To implement the distance vector routing algorithm.


DISTANCE ROUTING ALGORITM:
#include<stdlib.h>
#define nul 1000
#define nodes
10 int no; struct
node { int
a[nodes]
[4]; }router[nod
es]; void init(int
r) {
int i;

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]);
}
}

7 \\To write a program in java to implement IP address configuration..


import java.io.*;
import java.net.*;
class Server
{
public static void main(String args[])
{ try
{
String str;
BufferedReader buf1=new BufferedReader(new
InputStreamReader(System.in));
String ip=buf1.readLine();
Runtime H=Runtime.getRuntime();
Process p=H.exec("ipconfig " + ip);
InputStream in=p.getInputStream();
BufferedReader buf2=new BufferedReader(new
InputStreamReader(in)); while((str=buf2.readLine())!
=null)
{
System.out.println(" " + str);
}}
catch(Exception
e)
{
System.out.println(e.getMessage());
}
}
}

8 \\ To Perform File Transfer in Client & Server Using TCP/IP.


Client:
import java.io.*;
import java.net.*;
import java.util.*; class
Clientfile
{
public static void main(String args[])
{ try
{

BufferedReader in=new BufferedReader(new InputStreamReader(System.in));


Socket clsct=new Socket("18.17.40.27",3128);
DataInputStream din=new DataInputStream(clsct.getInputStream());
DataOutputStream dout=new DataOutputStream(clsct.getOutputStream());
System.out.println("Enter the file name:"); String
str=in.readLine(); dout.writeBytes(str+'\n');
System.out.println("Enter the new file name:");
String str2=in.readLine();
String str1,ss;
FileWriter f=new FileWriter(str2);
char buffer[]; while(true) {
str1=din.readLine(); if(str1.equals("-1"))
break; System.out.println(str1);
buffer=new char[str1.length()];
str1.getChars(0,str1.length(),buffer,0);
f.write(buffer);
}
f.close(); clsct.close();
}

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
{

ServerSocket obj=new ServerSocket(3128); while(true)


{
Socket obj1=obj.accept();
DataInputStream din=new DataInputStream(obj1.getInputStream());
DataOutputStream dout=new DataOutputStream(obj1.getOutputStream());
String str=din.readLine();
FileReader f=new FileReader(str);
BufferedReader b=new BufferedReader(f);

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();}}

10 \\ To write a program in java to demonstrate the usage of nslookup command.


import java.io.*;
import
java.net.*; class
Server
{
public static void main(String args[])
{ tr
y
{ St
ring
str;
BufferedReader buf1=new BufferedReader(new
InputStreamReader(System.in));
String ip=buf1.readLine();
Runtime H=Runtime.getRuntime();
Process p=H.exec("nslookup" + ip);
InputStream in=p.getInputStream();
BufferedReader buf2=new BufferedReader(new
InputStreamReader(in)); while((str=buf2.readLine())!
=null)
{
System.out.println(" " + str);
}}
catch(Exception
e)
{

48

System.out.println(e.getMessage());
}
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy