Prog 4 and 5
Prog 4 and 5
Develop a program to find the shortest path between vertices using the
bellman-ford and Path vector routing algorithm
import java.util.*; // Importing the necessary utilities from the Java library (Scanner for input, etc.)
class bellman
{ // Define the main class "Bellman" to represent the Bellman-Ford algorithm.
// Define a static nested class "Node" to represent each entry in the routing table.
// Each "Node" will have a "distance" (the shortest path) and a "phop" (the previous hop node).
public static class Node
{
int distance; // Distance to the destination node
char phop; // Previous hop node (used to trace the path)
}
// Character array representing the node labels (a, b, c, ... for up to 12 nodes)
char[] ch = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l'};
// Prompt the user to enter the adjacency matrix (connections between nodes)
System.out.println("Enter the adjacency matrix:");
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
g[i][j] = in.nextInt(); // Read the adjacency matrix (1 for direct connection, 0 for no connection)
}
}
// Ask the user to input the distance for each edge (between directly connected nodes)
System.out.println("Enter the distance:");
for (i = 0; i < n; i++) {
System.out.println("The distance between node " + ch[i] + " and others:");
for (j = 0; j < n; j++) {
if (g[i][j] == 1) { // If there's a direct connection between node i and node j
System.out.println("node " + ch[j] + " is:");
rt[i][j].distance = in.nextInt(); // Read the distance from the user and assign it to the routing table
} else {
rt[i][j].distance = 999; // If no direct connection, set distance to 999 (infinity)
}
rt[i][j].phop = ch[i]; // The previous hop for any node initially is itself
}
}
// Start a do-while loop to repeatedly display the menu and process user input
do {
adc = 0; // Reset the adjacent nodes count to 0 for each new iteration
System.out.println("1. Routing table information\n2. Routing table\n3. Exit"); // Display
menu options
choice = in.nextInt(); // Read the user's menu choice
// Identify all neighboring nodes for the given node (adjacent nodes)
for (i = 0; i < n; i++) {
if (g[id][i] == 1) { // If there's a direct connection between node 'id' and node 'i'
adjacentNodes[adc++] = i; // Store the neighboring node in the adjacentNodes array
System.out.println(ch[i]); // Print the neighbor's label
}
}
case 2: // If the user selects option 2, display the entire routing table
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
System.out.print(rt[i][j].distance + "," + rt[i][j].phop + "\t"); // Print each entry in the routing table
}
System.out.println(); // New line after each row of the table
}
break;
}
} while (choice != 3); // Continue the loop until the user chooses to exit (choice 3)
}
}
OUTPUT:
(base) ksit@cnlab4:~/lab$ javac bellman.java
(base) ksit@cnlab4:~/lab$ java bellman
Enter the number of nodes
4
Enter the adjacency matrix:
0110
1001
1001
0110
Enter the distance:
The distance between node a and
node b is:
2
node c is:
6
The distance between node b and
node a is:
2
node d is:
1
The distance between node c and
node a is:
6
node d is:
2
The distance between node d and
node b is:
1
node c is:
2
// Read all lines of the file specified by 'fname' using the UTF-8 charset
List<String> lines = Files.readAllLines(Paths.get(fname), cs); // 'Paths.get(fname)' creates a
Path object for the filename
// Loop through each line in the file and send it to the client
for (int i = 0; i < lines.size(); i++) {
dout.writeUTF(lines.get(i)); // Write each line to the output stream
dout.flush(); // Ensure that the data is sent immediately
}
// After sending all lines, send a "stop" message to signal the end of the file transmission
dout.writeUTF("stop");
// Output a message indicating the file has been successfully sent
System.out.println("File sent successfully!");
// Close the input and output streams and the connections with the client and server
din.close(); // Close the input stream
dout.close(); // Close the output stream
ns.close(); // Close the client connection
ss.close(); // Close the server socket (this stops the server)
}
}
class myclient {
public static void main(String[] args) throws Exception {
// Create a new Socket to connect to the server at localhost on port 8000
Socket cs = new Socket("localhost", 8000);
String msg;
// Use a try-catch block to handle any exceptions, like file not existing
try {
// Read the lines sent by the server until the "stop" message is received
do {
msg = din.readUTF(); // Read a line from the server
System.out.println(msg); // Print the line to the console
} while (!msg.equals("stop")); // Continue until "stop" message is received
} catch (Exception e) {
// If an exception occurs (e.g., file doesn't exist), print this message
System.out.println("File doesn't exist");
} finally {
// Close the input stream and the socket connection, regardless of whether an exception
occurs
din.close();
cs.close();
}
}
}
Java Platform Enhancement Proposal (JEP) 400 introduced UTF-8(UTF-8 stands for Unicode
Transformation Format-8. It's a character encoding system that's used to represent characters in
electronic communication.) as the default charset for standard Java APIs across all operating sys-
tems, starting with Java 18, except for the console input and output encoding. In IBM Semeru
Runtime Certified Edition for z/OS 21 (Semeru 21), UTF-8 is the default value of the file.
A socket is a connection point that allows data to be sent or received between two devices on a net-
work. It's made up of an IP address and a port number.
How sockets work
• Sockets allow different processes to communicate over a network.
• They enable applications and devices to exchange data over a network, such as the internet or
a local network.
• Sockets are often used for client and server interaction.
Different types of sockets
• Stream sockets: Allow processes to communicate using TCP.
• Datagram sockets: Allow processes to communicate using UDP.
• Raw sockets: Allow access to ICMP. They can be used to transport serial data through an IP
network.
Socket use cases
• Datagram sockets are commonly used in real-time applications like streaming audio or video.
• Raw sockets are used for building transport-layer protocols that aren't natively supported by
the kernel. They're also used for routing protocols like OSPF and ICMP.
Socket classes
• Socket classes are used to represent the connection between a server program and a client
program.
There are 65,535 possible port numbers in networking, but not all are in common use. Port numbers
are used to identify applications and services on a network.
Port number ranges
• Well-known ports: Range from 0 to 1023 and are assigned to services
• Registered ports: Range from 1024 to 49,151
• Dynamic/private ports: Range from 49,152 to 65,535
Commonly used ports
• Port 80: Hypertext Transfer Protocol (HTTP)
• Port 123: Network Time Protocol (NTP)
• Port 179: Border Gateway Protocol (BGP)
• Port 443: HTTP Secure (HTTPS)
• Port 500: Internet Security Association and Key Management Protocol (ISAKMP)
• Port 3389: Remote Desktop Protocol (RDP)
• Port 20 and 21: FTP
• Port 22: Secure Shell
• Port 53: Domain name system (DNS)
The Java DataInputStream readUTF() method reads in a string that has been encoded using a
modified UTF-8 format. The string of character is decoded from the UTF and returned as String.