import java.io.*;
import java.net.*;
public class RemoteCommandServer1 {
public static void main(String[] args) throws IOException {
final int PORT = 12345;
ServerSocket serverSocket = new ServerSocket(PORT);
System.out.println("Server started. Listening on port " + PORT);
System.out.println("Server is waiting for client");
while (true) {
Socket clientSocket = serverSocket.accept();
System.out.println("Client connected: " + clientSocket.getInetAddress().getHostName());
new Thread(new CommandHandler(clientSocket)).start();
}
}
static class CommandHandler implements Runnable {
private final Socket clientSocket;
CommandHandler(Socket socket) {
this.clientSocket = socket;
}
public void run() {
try (BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true)) {
String command = in.readLine();
System.out.println("Received command: " + command);
try {
ProcessBuilder processBuilder = new ProcessBuilder(command.split(" "));
processBuilder.redirectErrorStream(true); // Redirect error stream to output stream
Process process = processBuilder.start();
try (BufferedReader processOutput = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
String line;
while ((line = processOutput.readLine()) != null) {
out.println(line);
}
}
} catch (IOException e) {
out.println("Error executing command: " + e.getMessage());
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
clientSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
import java.io.*;
import java.net.*;
public class RemoteCommandClient1 {
public static void main(String[] args) throws IOException {
final String SERVER_IP = "localhost";
final int PORT = 12345;
try (Socket socket = new Socket(SERVER_IP, PORT);
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()))) {
System.out.println("Connected to server.");
out.println("hostname is pratyusha");
String line;
while ((line = in.readLine()) != null) {
System.out.println("Server response: " + line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.io.*;
import java.net.*;
public class WebPages {
public static void main(String[] args) throws Exception {
URL u = new URL("https://www.mlrit.ac.in");
BufferedReader r=new BufferedReader(new InputStreamReader(u.openStream()));
FileWriter w=new FileWriter("downloaded_page.html");
String line;
while((line=r.readLine())!=null)
w.write(line + "\n");
r.close();
w.close();
System.out.println("Webpage downloaded successfully!");
}
}
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class FileServer {
public static void main(String[] args) {
final int PORT = 12345;
final String fileName = "amaan.txt";
try (ServerSocket serverSocket = new ServerSocket(PORT)) {
System.out.println("Server started. Waiting for a client...");
try (Socket clientSocket = serverSocket.accept()) {
System.out.println("Client connected: " + clientSocket.getInetAddress().getHostName());
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
out.println(fileName);
FileInputStream fis = new FileInputStream(fileName);
BufferedOutputStream bos = new BufferedOutputStream(clientSocket.getOutputStream());
byte[] buffer = new byte[8192];
int bytesRead;
while ((bytesRead = fis.read(buffer)) != -1) {
bos.write(buffer, 0, bytesRead);
}
bos.flush();
fis.close();
System.out.println("File sent: " + fileName);
}
} catch (IOException e) {
System.err.println("Error: " + e.getMessage());
}
}
}
import java.io.*;
import java.net.Socket;
public class FileClient {
public static void main(String[] args) {
final String SERVER_IP = "localhost";
final int PORT = 12345;
String fileName = "amaan.txt";
try (Socket socket = new Socket(SERVER_IP, PORT)) {
System.out.println("Connected to server.");
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String receivedFileName = in.readLine();
System.out.println("Requested file: " + receivedFileName);
InputStream is = socket.getInputStream();
FileOutputStream fos = new FileOutputStream("received_" + receivedFileName);
BufferedOutputStream bos = new BufferedOutputStream(fos);
byte[] buffer = new byte[8192];
int bytesRead;
while ((bytesRead = is.read(buffer)) != -1) {
bos.write(buffer, 0, bytesRead);
}
bos.flush();
bos.close();
fos.close();
System.out.println("File received: received_" + receivedFileName);
} catch (IOException e) {
System.err.println("Error: " + e.getMessage());
}
}
}
import java.io.*;
import java.net.*;
public class EchoServer {
public static void main(String[] args) {
final int portNumber = 5555;
System.out.println("Echo server started on port " + portNumber);
try (
ServerSocket serverSocket = new ServerSocket(portNumber);
Socket clientSocket = serverSocket.accept();
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new
InputStreamReader(clientSocket.getInputStream()));
) {
System.out.println("Client connected");
String inputLine;
while ((inputLine = in.readLine()) != null) { System.out.println("Received from client: " +
inputLine); out.println(inputLine); // Echo back to client
}
} catch (IOException e) {
System.out.println("Exception caught when trying to listen on port "
+ portNumber + " or listening for a connection");
System.out.println(e.getMessage());
}}}
import java.io.*;
import java.net.*;
public class EchoClient {
public static void main(String[] args) {
final String host = "localhost";
final int portNumber = 5555;
try (
Socket echoSocket = new Socket(host, portNumber);
PrintWriter out = new PrintWriter(echoSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in))
) {
String userInput;
System.out.println("Enter messages to send to the server (type 'exit' to quit):");
while ((userInput = stdIn.readLine()) != null) {
if ("exit".equalsIgnoreCase(userInput)) {
System.out.println("Exiting...");
break;
}
out.println(userInput);
System.out.println("Server echoed: " + in.readLine());
}
} catch (IOException e) {
System.err.println("IOException caught: " + e.getMessage());
}
}
}
import java.io.*;
import java.net.*;
public class UDPServer {
public static void main(String[] args) {
final int PORT = 9876;
try (DatagramSocket serverSocket = new DatagramSocket(PORT)) {
byte[] receiveData = new byte[1024];
System.out.println("Server started. Listening on port " + PORT);
while (true) {
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
serverSocket.receive(receivePacket);
String message = new String(receivePacket.getData(), 0, receivePacket.getLength());
System.out.println("Client: " + message);
InetAddress clientAddress = receivePacket.getAddress();
int clientPort = receivePacket.getPort();
String responseMessage = "Message received successfully!";
byte[] sendData = responseMessage.getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, clientAddress, clientPort);
serverSocket.send(sendPacket);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.io.*;
import java.net.*;
public class UDPClient {
public static void main(String[] args) {
final String SERVER_IP = "localhost";
final int SERVER_PORT = 9876;
try (DatagramSocket clientSocket = new DatagramSocket();
BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in))) {
InetAddress serverAddress = InetAddress.getByName(SERVER_IP);
while (true) {
System.out.print("Enter message to send to server: ");
String message = userInput.readLine();
byte[] sendData = message.getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, serverAddress, SERVER_PORT);
clientSocket.send(sendPacket);
byte[] receiveData = new byte[1024];
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
clientSocket.receive(receivePacket);
String responseMessage = new String(receivePacket.getData(), 0, receivePacket.getLength());
System.out.println("Server: " + responseMessage);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.io.*;
import java.net.*;
public class Chatclient{
private static final String SERVER_IP="127.0.0.1";
private static final int SERVER_PORT=9000;
public static void main(String args[]) throws Exception
{
Socket socket=new Socket(SERVER_IP, SERVER_PORT);
System.out.println("Connected to the chat server..");
BufferedReader reader=new BufferedReader(new InputStreamReader (socket.getInputStream()));
PrintWriter writer =new PrintWriter(socket.getOutputStream(), true);
Thread input=new Thread(()->{
BufferedReader userInput=new BufferedReader(new InputStreamReader(System.in));
try{
while(true) {
String line=userInput.readLine();
writer.println(line);}
}
catch(IOException e) {
System.out.println(e);}
});
input.start();
String serverMessage;
try{
while((serverMessage=reader.readLine())!=null)
{
System.out.println("Server: "+serverMessage);
}
}catch(IOException e){
e.printStackTrace();
}
finally{
socket.close();
}}}
import java.util.*; import javax.mail.*;
import javax.mail.internet.*; import javax.activation.*;
public class SendMail
{
public static void main(String [] args){
String to = "chinnasamyponnusamy@gmail.com";//change accordingly String from = "chinna@mlrinstitutions.ac.in";//change accordingly String host = "localhost";//or IP address
//Get the session object
Properties properties = System.getProperties(); properties.setProperty("mail.smtp.host", host);
Session session = Session.getDefaultInstance(properties);
//compose the message try{
MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress(from)); message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
message.setSubject("Ping");
message.setText("Hello, this is example of sending email ");
// Send message Transport.send(message);
System.out.println("message sent successfully. ");
}catch (MessagingException mex) {mex.printStackTrace();}
}
}
//DistanceVectorRouting import java.util.Arrays;
public class DistanceVectorRouting {
static final int INFINITY = Integer.MAX_VALUE;
static final int NUM_NODES = 4; // Number of nodes in the network
public static void main(String[] args) { int[][] distanceMatrix = {
{0, 1, 3, INFINITY}, // Distance from Node 0 to other nodes
{1, 0, INFINITY, 2}, // Distance from Node 1 to other nodes
{3, INFINITY, 0, 2}, // Distance from Node 2 to other nodes
{INFINITY, 2, 2, 0} // Distance from Node 3 to other nodes
};
// Initialize distance vector table
int[][] distanceVectorTable = new int[NUM_NODES][NUM_NODES]; for (int i = 0; i < NUM_NODES; i++) {
distanceVectorTable[i] = Arrays.copyOf(distanceMatrix[i], NUM_NODES);
}
System.out.println("Initial Distance Vector Table:");
printDistanceVectorTable(distanceVectorTable);
// Simulate iterations of the Distance Vector Routing algorithm for (int i = 0; i < NUM_NODES; i++) {
System.out.println("Iteration " + (i + 1) + ":"); for (int j = 0; j < NUM_NODES; j++) {
if (j != i) {
for (int k = 0; k < NUM_NODES; k++) {
if (k != i && distanceVectorTable[j][k] > distanceMatrix[j][i] + distanceVectorTable[i][k]) {
distanceVectorTable[j][k] = distanceMatrix[j][i] + distanceVectorTable[i][k];
}
}
}
}
printDistanceVectorTable(distanceVectorTable);
}
}
static void printDistanceVectorTable(int[][] distanceVectorTable) { for (int i = 0; i < NUM_NODES; i++) {
for (int j = 0; j < NUM_NODES; j++) {
System.out.print((distanceVectorTable[i][j] == INFINITY ? "INF" : distanceVectorTable[i][j]) + "\t");
}
System.out.println();
}
System.out.println();
}
}
To Run
Javac DistanceVectorRouting.java
Java DistanceVectorRouting
//LinkStateRouting import java.util.*;
public class LinkStateRouting {
static final int NUM_NODES = 5; // Number of nodes in the network
public static void main(String[] args) { int[][] adjacencyMatrix = {
{0, 2, 0, 1, 0}, // Adjacency matrix representing links between nodes
{2, 0, 3, 2, 0},
{0, 3, 0, 0, 1},
{1, 2, 0, 0, 4},
{0, 0, 1, 4, 0}
};
// Initialize link state database
int[][] linkStateDatabase = new int[NUM_NODES][NUM_NODES]; for (int i = 0; i < NUM_NODES; i++) {
for (int j = 0; j < NUM_NODES; j++) { if (i == j) {
linkStateDatabase[i][j] = 0;
} else if (adjacencyMatrix[i][j] != 0) { linkStateDatabase[i][j] = adjacencyMatrix[i][j];
} else {
linkStateDatabase[i][j] = Integer.MAX_VALUE;
}
}
}
System.out.println("Initial Link State Database:"); printLinkStateDatabase(linkStateDatabase);
// Simulate iterations of the Link State Routing algorithm for (int i = 0; i < NUM_NODES; i++) {
System.out.println("Iteration " + (i + 1) + ":");
for (int j = 0; j < NUM_NODES; j++) { if (j != i) {
for (int k = 0; k < NUM_NODES; k++) {
if (k != i && linkStateDatabase[j][i] != Integer.MAX_VALUE && linkStateDatabase[i][k] != Integer.MAX_VALUE &&
linkStateDatabase[j][k] > linkStateDatabase[j][i] + linkStateDatabase[i][k]) {
linkStateDatabase[j][k] = linkStateDatabase[j][i] + linkStateDatabase[i][k];
}
}
}
}
printLinkStateDatabase(linkStateDatabase);
}
}
static void printLinkStateDatabase(int[][] linkStateDatabase) { for (int i = 0; i < NUM_NODES; i++) {
for (int j = 0; j < NUM_NODES; j++) {
System.out.print((linkStateDatabase[i][j] == Integer.MAX_VALUE ? "INF" : linkStateDatabase[i][j]) + "\t");
}
System.out.println();
}
System.out.println();
}
}
//To Run
//Javac LinkStateRouting.java
//Java LinkStateRouting
GENERAL COMMANDS:
Start the docker daemon
docker -d
Get help with Docker. Can also use –help on all subcommands
docker --help
Display system-wide information
docker info
IMAGES:
Docker images are a lightweight, standalone, executable package of software that includes
everything needed to run an application: code, runtime, system tools, system libraries and
settings.
Build an Image from a Dockerfile
docker build -t
Build an Image from a Dockerfile without the cachedocker build -t . –no-cache
List local images
docker images
Delete an Image
docker rmi
Remove all unused images
docker image prune
CONTAINERS:
A container is a runtime instance of a docker image. A container will always run the same,
regardless of the infrastructure. Containers isolate software from its environment and
ensure that it works uniformly despite differences for instance between development and
staging.
Create and run a container from an image, with a custom name:
docker run –name
Run a container with and publish a container’s port(s) to the host.
docker run -p :
Run a container in the background
docker run -d
Start or stop an existing container:
docker start|stop (or )
Remove a stopped container:
docker rm
Open a shell inside a running container:
docker exec -it sh
Fetch and follow the logs of a container:
docker logs -f
To inspect a running container:
docker inspect (or )
To list currently running containers:
docker ps
List all docker containers (running and stopped):
60
docker ps --all
View resource usage stats
docker container stats