khatta_cornetto

DC

Mar 26th, 2025
359
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 17.84 KB | None | 0 0
  1. Assignment No 6-Q11 Write a program to implement RPC (Remote procedure call)
  2.  *Server Code (RPCServer.java)*
  3. ```
  4. import java.io.*;
  5. import java.net.*;
  6. import java.util.*;
  7.  
  8. public class RPCServer {
  9.     private ServerSocket serverSocket;
  10.  
  11.     public RPCServer(int port) throws IOException {
  12.         serverSocket = new ServerSocket(port);
  13.         System.out.println("RPC Server started on port " + port);
  14.     }
  15.  
  16.     public void start() throws IOException {
  17.         while (true) {
  18.             Socket clientSocket = serverSocket.accept();
  19.             System.out.println("Client connected");
  20.  
  21.             // Read request from client
  22.             ObjectInputStream in = new ObjectInputStream(clientSocket.getInputStream());
  23.             String methodName = (String) in.readObject();
  24.             Object[] args = (Object[]) in.readObject();
  25.  
  26.             // Invoke method
  27.             Object result = invokeMethod(methodName, args);
  28.  
  29.             // Send response back to client
  30.             ObjectOutputStream out = new ObjectOutputStream(clientSocket.getOutputStream());
  31.             out.writeObject(result);
  32.             out.flush();
  33.  
  34.             clientSocket.close();
  35.         }
  36.     }
  37.  
  38.     private Object invokeMethod(String methodName, Object[] args) {
  39.         // Simulate method invocation
  40.         if (methodName.equals("add")) {
  41.             int a = (int) args[0];
  42.             int b = (int) args[1];
  43.             return a + b;
  44.         } else if (methodName.equals("subtract")) {
  45.             int a = (int) args[0];
  46.             int b = (int) args[1];
  47.             return a - b;
  48.         } else {
  49.             return "Method not found";
  50.         }
  51.     }
  52.  
  53.     public static void main(String[] args) throws IOException {
  54.         RPCServer server = new RPCServer(8080);
  55.         server.start();
  56.     }
  57. }
  58. ```
  59.  
  60. *Client Code (RPCClient.java)*
  61. ```
  62. import java.io.*;
  63. import java.net.*;
  64. import java.util.*;
  65.  
  66. public class RPCClient {
  67.     private Socket clientSocket;
  68.  
  69.     public RPCClient(String host, int port) throws UnknownHostException, IOException {
  70.         clientSocket = new Socket(host, port);
  71.         System.out.println("Connected to RPC Server");
  72.     }
  73.  
  74.     public Object invokeMethod(String methodName, Object... args) throws IOException {
  75.         // Send request to server
  76.         ObjectOutputStream out = new ObjectOutputStream(clientSocket.getOutputStream());
  77.         out.writeObject(methodName);
  78.         out.writeObject(args);
  79.         out.flush();
  80.  
  81.         // Read response from server
  82.         ObjectInputStream in = new ObjectInputStream(clientSocket.getInputStream());
  83.         Object result = null;
  84.         try {
  85.             result = in.readObject();
  86.         } catch (ClassNotFoundException e) {
  87.             e.printStackTrace();
  88.         }
  89.  
  90.         return result;
  91.     }
  92.  
  93.     public static void main(String[] args) throws UnknownHostException, IOException {
  94.         RPCClient client = new RPCClient("localhost", 8080);
  95.  
  96.         // Invoke methods
  97.         Object result1 = client.invokeMethod("add", 10, 20);
  98.         System.out.println("Result: " + result1);
  99.  
  100.         Object result2 = client.invokeMethod("subtract", 30, 15);
  101.         System.out.println("Result: " + result2);
  102.     }
  103. }
  104. ```
  105. *Socket Server Code (NameServer.java)*
  106. ```
  107. import java.io.*;
  108. import java.net.*;
  109. import java.util.*;
  110.  
  111. public class NameServer {
  112.     private ServerSocket serverSocket;
  113.  
  114.     public NameServer(int port) throws IOException {
  115.         serverSocket = new ServerSocket(port);
  116.         System.out.println("Name Server started on port " + port);
  117.     }
  118.  
  119.     public void start() throws IOException {
  120.         while (true) {
  121.             Socket clientSocket = serverSocket.accept();
  122.             System.out.println("Client connected");
  123.  
  124.             // Read request from client (not actually used in this example)
  125.             BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
  126.             String request = in.readLine();
  127.             System.out.println("Received request: " + request);
  128.  
  129.             // Send full name back to client
  130.             PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
  131.             out.println("Yih-Feng Hwang");
  132.  
  133.             clientSocket.close();
  134.         }
  135.     }
  136.  
  137.     public static void main(String[] args) throws IOException {
  138.         NameServer server = new NameServer(8080);
  139.         server.start();
  140.     }
  141. }
  142. ```
  143.  
  144. *Socket Client Code (NameClient.java)*
  145. ```
  146. import java.io.*;
  147. import java.net.*;
  148. import java.util.*;
  149.  
  150. public class NameClient {
  151.     private Socket clientSocket;
  152.  
  153.     public NameClient(String host, int port) throws UnknownHostException, IOException {
  154.         clientSocket = new Socket(host, port);
  155.         System.out.println("Connected to Name Server");
  156.     }
  157.  
  158.     public void communicate() throws IOException {
  159.         // Send any request to the server (in this case, just "Hello")
  160.         PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
  161.         out.println("Hello");
  162.  
  163.         // Read full name from server
  164.         BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
  165.         String fullName = in.readLine();
  166.         System.out.println("Received full name: " + fullName);
  167.  
  168.         clientSocket.close();
  169.     }
  170.  
  171.     public static void main(String[] args) throws UnknownHostException, IOException {
  172.         NameClient client = new NameClient("localhost", 8080);
  173.         client.communicate();
  174.     }
  175. }
  176. ```
  177.  
  178. To run the example:
  179.  
  180. 1. Start the NameServer by running `java NameServer` in one terminal window.
  181. 2. Start the NameClient by running `java NameClient` in another terminal window.
  182.  
  183. The client will send a request to the server, and the server will respond with the full name "Yih-Feng Hwang".
  184.  
  185.  Assignment No 6 Q 12 The socket server will return your full name (e.g., Yih-Feng Hwang) to a client
  186. when the socket server receives any request from the client.
  187.  
  188.  Write a program in Java to implement EchoClient and Echoserver program using TCP and UDP
  189. sockets.
  190.  
  191.  *TCP EchoServer Program (TCPEchoServer.java)*
  192. ```
  193. import java.io.*;
  194. import java.net.*;
  195.  
  196. public class TCPEchoServer {
  197.     private ServerSocket serverSocket;
  198.  
  199.     public TCPEchoServer(int port) throws IOException {
  200.         serverSocket = new ServerSocket(port);
  201.         System.out.println("TCP Echo Server started on port " + port);
  202.     }
  203.  
  204.     public void start() throws IOException {
  205.         while (true) {
  206.             Socket clientSocket = serverSocket.accept();
  207.             System.out.println("Client connected");
  208.  
  209.             // Create input and output streams
  210.             BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
  211.             PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
  212.  
  213.             // Echo client messages
  214.             String message;
  215.             while ((message = in.readLine()) != null) {
  216.                 System.out.println("Received message: " + message);
  217.                 out.println(message); // Echo message back to client
  218.             }
  219.  
  220.             clientSocket.close();
  221.         }
  222.     }
  223.  
  224.     public static void main(String[] args) throws IOException {
  225.         TCPEchoServer server = new TCPEchoServer(8080);
  226.         server.start();
  227.     }
  228. }
  229. ```
  230.  
  231. *TCP EchoClient Program (TCPEchoClient.java)*
  232. ```
  233. import java.io.*;
  234. import java.net.*;
  235. import java.util.*;
  236.  
  237. public class TCPEchoClient {
  238.     private Socket clientSocket;
  239.  
  240.     public TCPEchoClient(String host, int port) throws UnknownHostException, IOException {
  241.         clientSocket = new Socket(host, port);
  242.         System.out.println("Connected to TCP Echo Server");
  243.     }
  244.  
  245.     public void communicate() throws IOException {
  246.         // Create input and output streams
  247.         BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
  248.         PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
  249.         Scanner scanner = new Scanner(System.in);
  250.  
  251.         // Send messages to server and echo responses
  252.         while (true) {
  253.             System.out.print("Enter message: ");
  254.             String message = scanner.nextLine();
  255.             out.println(message);
  256.             String response = in.readLine();
  257.             System.out.println("Server response: " + response);
  258.         }
  259.     }
  260.  
  261.     public static void main(String[] args) throws UnknownHostException, IOException {
  262.         TCPEchoClient client = new TCPEchoClient("localhost", 8080);
  263.         client.communicate();
  264.     }
  265. }
  266. ```
  267.  
  268. *UDP EchoServer Program (UDPEchoServer.java)*
  269. ```
  270. import java.io.*;
  271. import java.net.*;
  272.  
  273. public class UDPEchoServer {
  274.     private DatagramSocket serverSocket;
  275.  
  276.     public UDPEchoServer(int port) throws SocketException {
  277.         serverSocket = new DatagramSocket(port);
  278.         System.out.println("UDP Echo Server started on port " + port);
  279.     }
  280.  
  281.     public void start() throws IOException {
  282.         while (true) {
  283.             // Receive message from client
  284.             byte[] buffer = new byte[1024];
  285.             DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
  286.             serverSocket.receive(packet);
  287.             String message = new String(packet.getData(), 0, packet.getLength());
  288.             System.out.println("Received message: " + message);
  289.  
  290.             // Echo message back to client
  291.             packet = new DatagramPacket(packet.getData(), packet.getLength(), packet.getAddress(), packet.getPort());
  292.             serverSocket.send(packet);
  293.         }
  294.     }
  295.  
  296.     public static void main(String[] args) throws SocketException {
  297.         UDPEchoServer server = new UDPEchoServer(8080);
  298.         server.start();
  299.     }
  300. }
  301. ```
  302.  
  303. *UDP EchoClient Program (UDPEchoClient.java)*
  304. ```
  305. import java.io.*;
  306. import java.net.*;
  307. import java.util.*;
  308.  
  309. public class UDPEchoClient {
  310.     private DatagramSocket clientSocket;
  311.  
  312.     public UDPEchoClient(String host, int port) throws SocketException {
  313.         clientSocket = new DatagramSocket();
  314.         System.out.println("Connected to UDP Echo Server");
  315.     }
  316.  
  317.     public void communicate() throws IOException {
  318.         Scanner scanner = new Scanner(System.in);
  319.  
  320.         while (true) {
  321.             System.out.print("Enter message: ");
  322.             String message = scanner.nextLine();
  323.  
  324.             // Send message to server
  325.             byte[] buffer = message.getBytes();
  326.             DatagramPacket packet = new DatagramPacket(buffer, buffer.length, InetAddress.getByName("localhost"), 8080);
  327.             clientSocket.send(packet);
  328.  
  329.             // Receive response from server
  330.             buffer = new byte[1024];
  331.             packet = new DatagramPacket(buffer, buffer.length);
  332.             clientSocket.receive(packet);
  333.             String response = new String(packet.getData(), 0, packet.getLength());
  334.             System.out.println("Server response: " + response);
  335.         }
  336.     }
  337.  
  338.     public static void main(String[] args) throws SocketException {
  339.         UDPEchoClient client = new UDPEchoClient("localhost", 8080);
  340.         client.communicate();
  341.     }
  342. }```
  343. [3/21, 4:00 PM] Prateek: Lab Assign 8, Q14-Create a socket program for HTTP web page upload and download
  344. [3/21, 4:00 PM] Prateek: *Server Code (HTTPServer.java)*
  345. ```
  346. import java.io.*;
  347. import java.net.*;
  348. import java.util.*;
  349.  
  350. public class HTTPServer {
  351.     private ServerSocket serverSocket;
  352.  
  353.     public HTTPServer(int port) throws IOException {
  354.         serverSocket = new ServerSocket(port);
  355.         System.out.println("HTTP Server started on port " + port);
  356.     }
  357.  
  358.     public void start() throws IOException {
  359.         while (true) {
  360.             Socket clientSocket = serverSocket.accept();
  361.             System.out.println("Client connected");
  362.  
  363.             // Create input and output streams
  364.             BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
  365.             PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
  366.  
  367.             // Read HTTP request
  368.             String request = in.readLine();
  369.             System.out.println("Received request: " + request);
  370.  
  371.             // Parse request
  372.             String[] parts = request.split(" ");
  373.             String method = parts[0];
  374.             String url = parts[1];
  375.  
  376.             // Handle GET request
  377.             if (method.equals("GET")) {
  378.                 String fileName = url.substring(1); // Remove leading '/'
  379.                 File file = new File(fileName);
  380.  
  381.                 if (file.exists()) {
  382.                     // Send HTTP response
  383.                     out.println("HTTP/1.1 200 OK");
  384.                     out.println("Content-Type: text/html");
  385.                     out.println();
  386.  
  387.                     // Send file contents
  388.                     FileReader fileReader = new FileReader(file);
  389.                     BufferedReader fileBufferedReader = new BufferedReader(fileReader);
  390.                     String line;
  391.                     while ((line = fileBufferedReader.readLine()) != null) {
  392.                         out.println(line);
  393.                     }
  394.                     fileBufferedReader.close();
  395.                 } else {
  396.                     // Send 404 error
  397.                     out.println("HTTP/1.1 404 Not Found");
  398.                     out.println();
  399.                 }
  400.             }
  401.  
  402.             // Handle POST request (file upload)
  403.             else if (method.equals("POST")) {
  404.                 // Read file contents from request body
  405.                 StringBuilder fileContents = new StringBuilder();
  406.                 String line;
  407.                 while ((line = in.readLine()) != null) {
  408.                     if (line.isEmpty()) {
  409.                         break; // End of headers
  410.                     }
  411.                 }
  412.                 while ((line = in.readLine()) != null) {
  413.                     fileContents.append(line).append("\n");
  414.                 }
  415.  
  416.                 // Save file contents to local file
  417.                 String fileName = url.substring(1); // Remove leading '/'
  418.                 File file = new File(fileName);
  419.                 FileWriter fileWriter = new FileWriter(file);
  420.                 fileWriter.write(fileContents.toString());
  421.                 fileWriter.close();
  422.  
  423.                 // Send HTTP response
  424.                 out.println("HTTP/1.1 200 OK");
  425.                 out.println();
  426.             }
  427.  
  428.             clientSocket.close();
  429.         }
  430.     }
  431.  
  432.     public static void main(String[] args) throws IOException {
  433.         HTTPServer server = new HTTPServer(8080);
  434.         server.start();
  435.     }
  436. }
  437. ```
  438.  
  439. *Client Code (HTTPClient.java)*
  440. ```
  441. import java.io.*;
  442. import java.net.*;
  443. import java.util.*;
  444.  
  445. public class HTTPClient {
  446.     private Socket clientSocket;
  447.  
  448.     public HTTPClient(String host, int port) throws UnknownHostException, IOException {
  449.         clientSocket = new Socket(host, port);
  450.         System.out.println("Connected to HTTP Server");
  451.     }
  452.  
  453.     public void downloadFile(String url) throws IOException {
  454.         // Send HTTP GET request
  455.         PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
  456.         out.println("GET " + url + " HTTP/1.1");
  457.         out.println("Host: localhost:8080");
  458.         out.println();
  459.         out.flush();
  460.  
  461.         // Read HTTP response
  462.         BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
  463.         String response = in.readLine();
  464.         System.out.println("Received response: " + response);
  465.  
  466.         // Read file contents from response body
  467.         StringBuilder fileContents = new StringBuilder();
  468.         String line;
  469.         while ((line = in.readLine()) != null) {
  470.             fileContents.append(line).append("\n");
  471.         }
  472.  
  473.         // Save file contents to local file
  474.         String fileName = url.substring(1); // Remove leading '/'
  475.         File file = new File(fileName);
  476.         FileWriter fileWriter = new FileWriter(file);
  477.         fileWriter.write(fileContents.toString());
  478.         fileWriter.close();
  479.     }
  480.  
  481.     public void uploadFile(String url, String fileContents) throws IOException {
  482.         // Send HTTP POST request
  483.         PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
  484.         out.println("POST " + url + " HTTP/1.1");
  485.         out.println("Host: localhost:8080");
  486.         out.println("Content-Type: text/plain");
  487.         out.println("Content-Length: " + fileContents.length());
  488.         out.println();
  489.         out.println(fileContents);
  490.         out.flush();
  491.  
  492.         // Read HTTP response
  493.         BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
  494.         String response = in.readLine();
  495.         System.out.println("Received response: " + response);
  496.     }
  497.  
  498.     public static void main(String[] args) throws UnknownHostException, IOException {
  499.         HTTPClient client = new HTTPClient("localhost", 8080```)
  500. [3/21, 4:00 PM] Prateek: Lab Assign 8 Q No 15- Write a program to translate dotted decimal IP address into 32 bit address.
  501. [3/21, 4:00 PM] Prateek: *IPTranslator.java*
  502. ```
  503. import java.util.Scanner;
  504.  
  505. public class IPTranslator {
  506.     public static void main(String[] args) {
  507.         Scanner scanner = new Scanner(System.in);
  508.  
  509.         System.out.print("Enter a dotted decimal IP address: ");
  510.         String ipAddress = scanner.nextLine();
  511.  
  512.         String[] parts = ipAddress.split("\\.");
  513.  
  514.         if (parts.length != 4) {
  515.             System.out.println("Invalid IP address format.");
  516.             return;
  517.         }
  518.  
  519.         StringBuilder binaryAddress = new StringBuilder();
  520.  
  521.         for (String part : parts) {
  522.             int decimalValue = Integer.parseInt(part);
  523.             String binaryValue = Integer.toBinaryString(decimalValue);
  524.  
  525.             // Pad with leading zeros to ensure 8-bit binary representation
  526.             while (binaryValue.length() < 8) {
  527.                 binaryValue = "0" + binaryValue;
  528.             }
  529.  
  530.             binaryAddress.append(binaryValue);
  531.         }
  532.  
  533.         System.out.println("32-bit Binary IP Address: " + binaryAddress.toString());
  534.     }
  535. }
  536.  
  537. 1. The program prompts the user to enter a dotted decimal IP address.
  538. 2. It splits the input string into four parts using the dot (`.`) as a delimiter.
  539. 3. It checks if the input IP address has exactly four parts. If not, it displays an error message.
  540.  
  541. ```Enter a dotted decimal IP address: 192.168.1.1
  542. 32-bit Binary IP Address: 11000000101010000000000100000001```
Advertisement
Add Comment
Please, Sign In to add comment