Você está na página 1de 9

SIMULATION OF ROUTING PROTOCOLS USING C

#include<stdio.h>
#include<ctype.h>
int graph[12][12];
int e[12][12];
int ad[12];
int no,id,adc,small,chosen,i,j,ch1,ch2;
char nodes[12]={"abcdefghijkl"};
int main()
{
adc=0;
printf("Enter The Number Of Nodes: ");
scanf("%d",&no);
printf("\nEnter The Values For Adjacency Matrix\n");
for(i=0;i <no;i++)
{
for(j=0;j <no;j++)
{
printf("Enter The Values For %d,%d Position: ",(i+1),(j+1));
scanf("%d",&graph[i][j]);
}
}
printf("\nEnter The Initial Estimates\t");
for(i=0;i <no;i++)
{
printf("\nEstimate For Node %c:\n",nodes[i]);
for(j=0;j <no;j++)
{
printf("To Node %c : ",nodes[j]);
scanf("%d",&e[i][j]);
}
}
do
{
printf("\nMENU:\n1.ROUTING INFO FOR NODE");
printf("\n2.ESTIMATED TABLE\n");
printf("Enter Your Choice: ");
scanf("%d",&ch1);
switch(ch1)
{
case 1:
printf("\nWhich Node Should Routing Table Be Built? (1-a)(2-b)...");
scanf("%d",&id);
id--;
adc=0;
printf("\nNeighbours For Particular Node ");
for(i=0;i <no;i++)
{
if(graph[id][i]==1)
{
ad[adc]=i;
adc++;
printf("%c",nodes[i]);
}
}
for(i=0;i <no;i++)
{
if(id!=i)
{
small=100;
chosen=1;
for(j=0;j <no;j++)
{
int total=e[ad[j]][i]+e[id][ad[j]];
if(total <100)
{
small=total;
chosen=j;
}
}
e[id][i]=small;
printf("\nShortest Estimate To %c is %d",nodes[i],small);
printf("\nNext Hop Is %c",nodes[ad[chosen]]);
}
else
e[id][i]=0;
}
break;
case 2:
printf("\n");
for(i=0;i <no;i++)
{
for(j=0;j <no;j++)
printf("%d ",e[i][j]);
printf("\n");
}
break;
}
printf("\nDo You Want To Continue?(1-YES) (2-NO): ");
scanf("%d",&ch2);
}
while(ch2==1);
return 0;
}
OUTPUT
$ cc simulation.c
$ ./a.out
Enter The Number Of Nodes: 2
Enter The Values For Adjacency Matrix
Enter The Values For 1,1 Position: 1
Enter The Values For 1,2 Position: 2
Enter The Values For 2,1 Position: 3
Enter The Values For 2,2 Position: 4
Enter The Initial Estimates
Estim ate For Node a:
To Node a : 1
To Node b : 2
Estimate For Node b:
To Node a : 1
To No de b : 2
MENU:
1.ROUTING INFO FOR NODE
2.ESTIMATED TABLE
Enter Your Choice: 1
Which Node Should Routing Table Be Built? (1-a)(2-b)...1
Neighbours For Particular Nodea
Shortest Estimate To b is 2
Next Hop Is a
Do You Want To Continue?(1-YES) (2-NO): 1
MENU:
1.ROUTING INFO FOR NODE
2.ESTIMATED TABLE
Enter Your Choice: 2
02
12
Do You Want To Continue?(1-YES) (2-NO): 2
Ping server and Client in Java using Sockets
Server listens the incoming connections in port. Client sends a string to server. Server
echoes the string received from the client and shows the client address. Client receives the
echoed string and calculates the round trip time and data loss if any.
Ping Server
import java.io.*;
import java.net.*;
import java.util.*;
import java.text.*;
class pings {
public static void main(String args[]) throws IOException
{
ServerSocket s = new ServerSocket(2000);
while(true)
{
Socket c = s.accept();
InputStream in = c.getInputStream();
InputStreamReader inr = new InputStreamReader(in);
BufferedReader br = new BufferedReader(inr);
String str = br.readLine();
System.out.println("Ping command received from : "+c.getInetAddress() +"
with string "+str);
PrintStream ps = new PrintStream(c.getOutputStream());
ps.println(str);
}
}
}

Ping Client
import java.io.*;
import java.net.*;
public class pingc {
public static void main(String args[]) throws IOException
{
long t1, t2;
while(true)
{
try{
Socket soc = new Socket("localhost",2000);
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
System.out.println("Type a string to ping : ");
String str = br.readLine();
OutputStream os = soc.getOutputStream();
PrintWriter pw = new PrintWriter(os,true);
InputStream in = soc.getInputStream();
InputStreamReader inr = new InputStreamReader(in);
BufferedReader br1 = new BufferedReader(inr);
t1 = System.currentTimeMillis();
pw.println(str);

String str1 = br1.readLine();


t2 = System.currentTimeMillis();
System.out.println("Pinging "+soc.getInetAddress()+" with string "+str );
System.out.println("Reply from "+soc.getInetAddress() +" String "+str1+"
Length : "+str1.length());
System.out.println("Sent : "+str.length()+" Received : "+str1.length()+" Lost :
"+(str.length()-str1.length()));
System.out.println("Approx. Time in Milliseconds = "+(t2-t1));
}
catch(Exception e)
{
System.out.println("Error : "+e.getMessage());
}
}

Output :
Compile and run server program first and then client program

Server Window
Ping command received from : /127.0.0.1 with string hello
Ping command received from : /127.0.0.1 with string test

Client Window
Type a string to ping :
hello
Pinging localhost/127.0.0.1 with string hello
Reply from localhost/127.0.0.1 String hello Length : 5
Sent : 5 Received : 5 Lost : 0
Approx. Time in Milliseconds = 5

Type a string to ping :


test
Pinging localhost/127.0.0.1 with string test
Reply from localhost/127.0.0.1 String test Length : 4
Sent : 4 Received : 4 Lost : 0
Approx. Time in Milliseconds = 4
Type a string to ping :
Distance-vector routing ( DVR ) algorithm in java:
import java.io.*;
public class DVR
{
static int graph[][];
static int via[][];
static int rt[][];
static int v;
static int e;

public static void main(String args[]) throws IOException


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

System.out.println("Please enter the number of Vertices: ");


v = Integer.parseInt(br.readLine());

System.out.println("Please enter the number of Edges: ");


e = Integer.parseInt(br.readLine());

graph = new int[v][v];


via = new int[v][v];
rt = new int[v][v];
for(int i = 0; i < v; i++)
for(int j = 0; j < v; j++)
{
if(i == j)
graph[i][j] = 0;
else
graph[i][j] = 9999;
}

for(int i = 0; i < e; i++)


{
System.out.println("Please enter data for Edge " + (i + 1) + ":");
System.out.print("Source: ");
int s = Integer.parseInt(br.readLine());
s--;
System.out.print("Destination: ");
int d = Integer.parseInt(br.readLine());
d--;
System.out.print("Cost: ");
int c = Integer.parseInt(br.readLine());
graph[s][d] = c;
graph[d][s] = c;
}

dvr_calc_disp("The initial Routing Tables are: ");

System.out.print("Please enter the Source Node for the edge whose cost has changed:
");
int s = Integer.parseInt(br.readLine());
s--;
System.out.print ("Please enter the Destination Node for the edge whose cost has
changed: ");
int d = Integer.parseInt(br.readLine());
d--;
System.out.print("Please enter the new cost: ");
int c = Integer.parseInt(br.readLine());
graph[s][d] = c;
graph[d][s] = c;

dvr_calc_disp("The new Routing Tables are: ");


}

static void dvr_calc_disp(String message)


{
System.out.println();
init_tables();
update_tables();
System.out.println(message);
print_tables();
System.out.println();
}

static void update_table(int source)


{
for(int i = 0; i < v; i++)
{
if(graph[source][i] != 9999)
{
int dist = graph[source][i];
for(int j = 0; j < v; j++)
{
int inter_dist = rt[i][j];
if(via[i][j] == source)
inter_dist = 9999;
if(dist + inter_dist < rt[source][j])
{
rt[source][j] = dist + inter_dist;
via[source][j] = i;
}
}
}
}
}

static void update_tables()


{
int k = 0;
for(int i = 0; i < 4*v; i++)
{
update_table(k);
k++;
if(k == v)
k = 0;
}
}

static void init_tables()


{
for(int i = 0; i < v; i++)
{
for(int j = 0; j < v; j++)
{
if(i == j)
{
rt[i][j] = 0;
via[i][j] = i;
}
else
{
rt[i][j] = 9999;
via[i][j] = 100;
}
}
}
}

static void print_tables()


{
for(int i = 0; i < v; i++)
{
for(int j = 0; j < v; j++)
{
System.out.print("Dist: " + rt[i][j] + " ");
}
System.out.println();
}
}

}
output:-
Please enter the number of Vertices:
4
Please enter the number of Edges:
5
Please enter data for Edge 1:
Source: 1
Destination: 2
Cost: 1
Please enter data for Edge 2:
Source: 1
Destination: 3
Cost: 3
Please enter data for Edge 3:
Source: 2
Destination: 3
Cost: 1
Please enter data for Edge 4:
Source: 2
Destination: 4
Cost: 1
Please enter data for Edge 5:
Source: 3
Destination: 4
Cost: 4

The initial Routing Tables are:


Dist: 0 Dist: 1 Dist: 2 Dist: 2
Dist: 1 Dist: 0 Dist: 1 Dist: 1
Dist: 2 Dist: 1 Dist: 0 Dist: 2
Dist: 2 Dist: 1 Dist: 2 Dist: 0

Please enter the Source Node for the edge whose cost has changed: 2
Please enter the Destination Node for the edge whose cost has changed: 4
Please enter the new cost: 10

The new Routing Tables are:


Dist: 0 Dist: 1 Dist: 2 Dist: 6
Dist: 1 Dist: 0 Dist: 1 Dist: 5
Dist: 2 Dist: 1 Dist: 0 Dist: 4
Dist: 6 Dist: 5 Dist: 4 Dist: 0
--------------------------------

Você também pode gostar