CN LAB Exercise

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 12

Ex No:6

DATE:

IMPLEMENTATION OF SHORTEST PATH ROUTING


ALGORITHM

AIM:
To find the shortest path from source to destination node in a network using Dijikstra
Algorithm.
ALGORITHM:
Step 1: Read the number of nodes, n.
Step 2: Read the cost matrix from each node to every other node.
Step 3: Initialize SOURCE to be 1 and include 1.
Step 4: Compute D of a node to be the distance from SOURCE to that node.
Step 5: Repeat step 6 to step 8 for n-1 nodes.
Step 6: Choose a node not included whose distance is the minimum and include that node.
Step 7: For every other node not included compare the distance directly from SOURCE with the
distance to reach the node using the newly included node.
Step 8: Take the minimum value as the new distance.
Step 9: Print all the nodes with their distances.
SOURCE CODE:
#include<stdio.h>
#include <conio.h>
int i,j,k,n;
int costarr[10][10];
int procarr[10][10];
int patharr[10][10];
void input()
{
printf("\nENTER THE COST:");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)

{
if(i!=j)
{
printf("\nCOST of %d to %d :",i,j);
scanf("%d",&costarr[i][j]);
}
else
costarr[i][j]=999;
}
}
printf("\nCOST OF VETICES :\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf("%d\t",costarr[i][j]);
}
printf("\n");
}
}
void proc()
{
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
procarr[i][j]=costarr[i][j];
patharr[i][j]=0;
}
}
for(k=1;k<=n;k++)
{
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if((procarr[i][k]+procarr[k][j])<procarr[i][j])
{
procarr[i][j]=procarr[i][k]+procarr[k][j];
patharr[i][j]=k;
}
}
}
}
for(i=1;i<=n;i++)

{
for(j=1;j<=n;j++)
{
if(costarr[i][j]!=999)
{
if(i!=j)
{
if(patharr[i][j]==0)
{
printf("\nTHE SHORTEST PATH FROM %d TO %d IS\t %d",i,j,i);
printf("----->%d",j);
printf(" COST IS-->%d\n",procarr[i][j]);
}
else
{
printf("\nTHE SHORTEST PATH FROM %d TO %d IS %d ",i,j,i);
printf("----->%d ---->%d",patharr[i][j],j);
printf(" COST IS-->%d",procarr[i][j]);
}
}
}
}
}
}
int main()
{
printf("\nSHORTEST PATH ROUTING ALGORITHM:");
printf("\nENTER THE NUMBER OF VERTICES:");
scanf("%d",&n);
input();
proc();
getch();
return 0;
}

OUTPUT
SHORTEST PATH ROUTING ALGORITHM
ENTER THE NUMBER OF VERTICES:5
ENTER THE COST:
COST of 1 to 2 :5
COST of 1 to 3 :4
COST of 1 to 4 :4
COST of 1 to 5 :10
COST of 2 to 1 :5
COST of 2 to 3 :12
COST of 2 to 4 :2
COST of 2 to 5 :4
COST of 3 to 1 :4
COST of 3 to 2 :12
COST of 3 to 4 :3
COST of 3 to 5 :6
COST of 4 to 1 :4
COST of 4 to 2 :2
COST of 4 to 3 :3
COST of 4 to 5 :3
COST of 5 to 1 :10
COST of 5 to 2 :4
COST of 5 to 3 :6
COST of 5 to 4 :3
COST OF VETICES :
999 5
4
4
5
999 12
2
4
12
999 3
4
2
3
999
10
4
6
3

10
4
6
3
999

THE SHORTEST PATH FROM 1 TO 2 IS 1----->2 COST IS-->5


THE SHORTEST PATH FROM 1 TO 3 IS 1----->3 COST IS-->4
THE SHORTEST PATH FROM 1 TO 4 IS 1----->4 COST IS-->4
THE SHORTEST PATH FROM 1 TO 5 IS 1----->4 --->5 COST IS-->7
THE SHORTEST PATH FROM 2 TO 1 IS 2----->1 COST IS-->5
THE SHORTEST PATH FROM 2 TO 3 IS 2----->4 --->3 COST IS-->5

THE SHORTEST PATH FROM 2 TO 4 IS 2----->4 COST IS-->2


THE SHORTEST PATH FROM 2 TO 5 IS 2----->5 COST IS-->4
THE SHORTEST PATH FROM 3 TO 1 IS 3----->1 COST IS-->4
THE SHORTEST PATH FROM 3 TO 2 IS 3----->4 --->2 COST IS-->5
THE SHORTEST PATH FROM 3 TO 4 IS 3----->4 COST IS-->3
THE SHORTEST PATH FROM 3 TO 5 IS 3----->5 COST IS-->6
THE SHORTEST PATH FROM 4 TO 1 IS 4----->1 COST IS-->4
THE SHORTEST PATH FROM 4 TO 2 IS 4----->2 COST IS-->2
THE SHORTEST PATH FROM 4 TO 3 IS 4----->3 COST IS-->3
THE SHORTEST PATH FROM 4 TO 5 IS 4----->5 COST IS-->3
THE SHORTEST PATH FROM 5 TO 1 IS 5----->4 --->1 COST IS-->7
THE SHORTEST PATH FROM 5 TO 2 IS 5----->2 COST IS-->4
THE SHORTEST PATH FROM 5 TO 3 IS 5----->3 COST IS-->6
THE SHORTEST PATH FROM 5 TO 4 IS 5----->4 COST IS-->3

RESULT:
Thus the program to find the shortest path from source to destination node in a
network using Dijikstra Algorithm was written and successfully executed.

Ex No:7

File Transfer using - TCP

Date:

Aim: To write a program to transfer file between client to server using TCP.
ALGORITHM:
Step 1: Start the program
Step 2: Include the import java.net.*;
Import java.io.*;Package
Step 3: Define a class for implementing main method.
Step 4: Using Clint and server sockets, Inetaddres in the main program, which will return
an instance of the class.
Step 5: The TCP Transmission Control Protocol is one the core protocols of the Internet
Protocol Suite. Using TCP, applications on networked hosts can create
connections to one, over which they can exchange data or packets.
Step 6: The data from the client is read by a data input stream and write by data output
stream.
Step 7: The file is transferred by client to server. If the server is ready it sends the
sequence of the requested string to the client.
Step 8: Print out with the necessary details.

SOURCE CODE:
FCLIENT:
import java.net.*;
import java.io.*;
class Fclient
{
public static void main(String args[])throws Exception
{
Socket echosocket=null;
BufferedReader in = null;
try
{
echosocket = new Socket(InetAddress.getLocalHost(),1092);
in= new BufferedReader(new InputStreamReader(echosocket.getInputStream()));
}
catch(UnknownHostException e)
{
System.err.println("could not get io for the connection");
System.exit(1);
}
String UserInput;
while((UserInput=in.readLine())!=null)
{
System.out.println(UserInput);
}
in.close();
echosocket.close();
}
}
FSERVER:
import java.net.*;
import java.io.*;
class Fserver
{
public static void main(String args[])throws Exception
{
ServerSocket serversock=null;
try
{
serversock = new ServerSocket(1092);
}

catch(IOException e)
{
System.err.println("could not listen on port 1092");
System.exit(1);
}
Socket clientsock=null;
try
{
clientsock=serversock.accept();
System.out.println("connected to client sock");
}
catch(IOException e)
{
System.err.println("accept failed");
System.exit(1);
}
PrintWriter outprint=new PrintWriter(clientsock.getOutputStream(),true);
String inputline,outputline;
BufferedReader b=new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter the name of the file");
String s=b.readLine();
File f=new File(s);
if(f.exists())
{
BufferedReader d=new BufferedReader(new FileReader(s));
String line;
while((line=d.readLine())!=null)
{
outprint.write(line);
outprint.flush();
}
d.close();
}
b.close();
outprint.close();
serversock.close();
clientsock.close();
}
}

Ex No:8
Date:

Implementation of PING

Aim: To implement ping command to determine the connectivity of a host in a network.


ALGORITHM:

Server:
Step 1: Create a object for runtime, process classes.
Step 2: Create an object r for runtime.
Step 3: Get the runtime environment by calling getRuntime() method and assign it to r.
Step 4: Execute the ping command using the exec() method of Runtime class.
Step 5: Store the result in p, object of process class, if object value is null
Step 6: Create a string object to get IP address.
Step 7: If it starts with request, the n print there is no replay and connection
Step 6: Stop

Source Code:
import java.io.*;
import java.net.*;
class pingdemo
{
public static void main(String args[])
{
BufferedReader in;
try
{
Runtime r=Runtime.getRuntime();
Process p=r.exec("Ping 127.0.0.1");
if(p==null)
System.out.println("could not connect");
in=new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while((line=in.readLine())!=null)
{
if(line.startsWith("reply"))
System.out.println("this is reply");
else if(line.startsWith("request"))
System.out.println("there is no reply");
else if(line.startsWith("destinator"))
System.out.println("destinator host unreachabl");
else
System.out.println(line);
}
System.out.println(in.readLine());
in.close();
} catch(IOException e)
{System.out.println(e.toString());}
}
}
OUTPUT:

RESULT: Thus, the program for ping was implemented to check the connectivity of a host in a
network.

Ex No:9
Date:

Implementation of Distance Vector Routing

AIM:
ALGORITHM:
#include<stdio.h>
struct node
{
unsigned dist[20];
unsigned from[20];
}rt[10];
int main()
{
int costmat[20][20];
int nodes,i,j,k,count=0;
printf("\nEnter the number of nodes : ");
scanf("%d",&nodes);//Enter the nodes
printf("\nEnter the cost matrix :\n");
for(i=0;i<nodes;i++)
{
for(j=0;j<nodes;j++)
{
scanf("%d",&costmat[i][j]);
costmat[i][i]=0;
rt[i].dist[j]=costmat[i][j];//initialise the distance
equal to cost matrix
rt[i].from[j]=j;
}
}
do
{
count=0;
for(i=0;i<nodes;i++)//We choose arbitary vertex k and
we calculate the direct distance from the node i to k using the
cost matrix
//and add the distance from k to node j
for(j=0;j<nodes;j++)
for(k=0;k<nodes;k++)
if(rt[i].dist[j]>costmat[i][k]+rt[k].dist[j])
{//We calculate the minimum distance
rt[i].dist[j]=rt[i].dist[k]+rt[k].dist[j];
rt[i].from[j]=k;
count++;
}
}while(count!=0);
for(i=0;i<nodes;i++)

{
printf("\n\n For router %d\n",i+1);
for(j=0;j<nodes;j++)
{
printf("\t\nnode %d via %d Distance %d
",j+1,rt[i].from[j]+1,rt[i].dist[j]);
}
}
printf("\n\n");
getch();
return 0;
}
Sample Output:
Enter the number of nodes :
3
Enter the cost matrix :
0 2 7
2 0 1
7 1 0
For router 1
node 1 via 1 Distance 0
node 2 via 2 Distance 2
node 3 via 3 Distance 3
For router 2
node 1 via 1 Distance 2
node 2 via 2 Distance 0
node 3 via 3 Distance 1
For router 3
node 1 via 1 Distance 3
node 2 via 2 Distance 1
node 3 via 3 Distance 0

You might also like