Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Assignment No 6-Q11 Write a program to implement RPC (Remote procedure call)
- *Server Code (RPCServer.java)*
- ```
- import java.io.*;
- import java.net.*;
- import java.util.*;
- public class RPCServer {
- private ServerSocket serverSocket;
- public RPCServer(int port) throws IOException {
- serverSocket = new ServerSocket(port);
- System.out.println("RPC Server started on port " + port);
- }
- public void start() throws IOException {
- while (true) {
- Socket clientSocket = serverSocket.accept();
- System.out.println("Client connected");
- // Read request from client
- ObjectInputStream in = new ObjectInputStream(clientSocket.getInputStream());
- String methodName = (String) in.readObject();
- Object[] args = (Object[]) in.readObject();
- // Invoke method
- Object result = invokeMethod(methodName, args);
- // Send response back to client
- ObjectOutputStream out = new ObjectOutputStream(clientSocket.getOutputStream());
- out.writeObject(result);
- out.flush();
- clientSocket.close();
- }
- }
- private Object invokeMethod(String methodName, Object[] args) {
- // Simulate method invocation
- if (methodName.equals("add")) {
- int a = (int) args[0];
- int b = (int) args[1];
- return a + b;
- } else if (methodName.equals("subtract")) {
- int a = (int) args[0];
- int b = (int) args[1];
- return a - b;
- } else {
- return "Method not found";
- }
- }
- public static void main(String[] args) throws IOException {
- RPCServer server = new RPCServer(8080);
- server.start();
- }
- }
- ```
- *Client Code (RPCClient.java)*
- ```
- import java.io.*;
- import java.net.*;
- import java.util.*;
- public class RPCClient {
- private Socket clientSocket;
- public RPCClient(String host, int port) throws UnknownHostException, IOException {
- clientSocket = new Socket(host, port);
- System.out.println("Connected to RPC Server");
- }
- public Object invokeMethod(String methodName, Object... args) throws IOException {
- // Send request to server
- ObjectOutputStream out = new ObjectOutputStream(clientSocket.getOutputStream());
- out.writeObject(methodName);
- out.writeObject(args);
- out.flush();
- // Read response from server
- ObjectInputStream in = new ObjectInputStream(clientSocket.getInputStream());
- Object result = null;
- try {
- result = in.readObject();
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- }
- return result;
- }
- public static void main(String[] args) throws UnknownHostException, IOException {
- RPCClient client = new RPCClient("localhost", 8080);
- // Invoke methods
- Object result1 = client.invokeMethod("add", 10, 20);
- System.out.println("Result: " + result1);
- Object result2 = client.invokeMethod("subtract", 30, 15);
- System.out.println("Result: " + result2);
- }
- }
- ```
- *Socket Server Code (NameServer.java)*
- ```
- import java.io.*;
- import java.net.*;
- import java.util.*;
- public class NameServer {
- private ServerSocket serverSocket;
- public NameServer(int port) throws IOException {
- serverSocket = new ServerSocket(port);
- System.out.println("Name Server started on port " + port);
- }
- public void start() throws IOException {
- while (true) {
- Socket clientSocket = serverSocket.accept();
- System.out.println("Client connected");
- // Read request from client (not actually used in this example)
- BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
- String request = in.readLine();
- System.out.println("Received request: " + request);
- // Send full name back to client
- PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
- out.println("Yih-Feng Hwang");
- clientSocket.close();
- }
- }
- public static void main(String[] args) throws IOException {
- NameServer server = new NameServer(8080);
- server.start();
- }
- }
- ```
- *Socket Client Code (NameClient.java)*
- ```
- import java.io.*;
- import java.net.*;
- import java.util.*;
- public class NameClient {
- private Socket clientSocket;
- public NameClient(String host, int port) throws UnknownHostException, IOException {
- clientSocket = new Socket(host, port);
- System.out.println("Connected to Name Server");
- }
- public void communicate() throws IOException {
- // Send any request to the server (in this case, just "Hello")
- PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
- out.println("Hello");
- // Read full name from server
- BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
- String fullName = in.readLine();
- System.out.println("Received full name: " + fullName);
- clientSocket.close();
- }
- public static void main(String[] args) throws UnknownHostException, IOException {
- NameClient client = new NameClient("localhost", 8080);
- client.communicate();
- }
- }
- ```
- To run the example:
- 1. Start the NameServer by running `java NameServer` in one terminal window.
- 2. Start the NameClient by running `java NameClient` in another terminal window.
- The client will send a request to the server, and the server will respond with the full name "Yih-Feng Hwang".
- Assignment No 6 Q 12 The socket server will return your full name (e.g., Yih-Feng Hwang) to a client
- when the socket server receives any request from the client.
- Write a program in Java to implement EchoClient and Echoserver program using TCP and UDP
- sockets.
- *TCP EchoServer Program (TCPEchoServer.java)*
- ```
- import java.io.*;
- import java.net.*;
- public class TCPEchoServer {
- private ServerSocket serverSocket;
- public TCPEchoServer(int port) throws IOException {
- serverSocket = new ServerSocket(port);
- System.out.println("TCP Echo Server started on port " + port);
- }
- public void start() throws IOException {
- while (true) {
- Socket clientSocket = serverSocket.accept();
- System.out.println("Client connected");
- // Create input and output streams
- BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
- PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
- // Echo client messages
- String message;
- while ((message = in.readLine()) != null) {
- System.out.println("Received message: " + message);
- out.println(message); // Echo message back to client
- }
- clientSocket.close();
- }
- }
- public static void main(String[] args) throws IOException {
- TCPEchoServer server = new TCPEchoServer(8080);
- server.start();
- }
- }
- ```
- *TCP EchoClient Program (TCPEchoClient.java)*
- ```
- import java.io.*;
- import java.net.*;
- import java.util.*;
- public class TCPEchoClient {
- private Socket clientSocket;
- public TCPEchoClient(String host, int port) throws UnknownHostException, IOException {
- clientSocket = new Socket(host, port);
- System.out.println("Connected to TCP Echo Server");
- }
- public void communicate() throws IOException {
- // Create input and output streams
- BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
- PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
- Scanner scanner = new Scanner(System.in);
- // Send messages to server and echo responses
- while (true) {
- System.out.print("Enter message: ");
- String message = scanner.nextLine();
- out.println(message);
- String response = in.readLine();
- System.out.println("Server response: " + response);
- }
- }
- public static void main(String[] args) throws UnknownHostException, IOException {
- TCPEchoClient client = new TCPEchoClient("localhost", 8080);
- client.communicate();
- }
- }
- ```
- *UDP EchoServer Program (UDPEchoServer.java)*
- ```
- import java.io.*;
- import java.net.*;
- public class UDPEchoServer {
- private DatagramSocket serverSocket;
- public UDPEchoServer(int port) throws SocketException {
- serverSocket = new DatagramSocket(port);
- System.out.println("UDP Echo Server started on port " + port);
- }
- public void start() throws IOException {
- while (true) {
- // Receive message from client
- byte[] buffer = new byte[1024];
- DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
- serverSocket.receive(packet);
- String message = new String(packet.getData(), 0, packet.getLength());
- System.out.println("Received message: " + message);
- // Echo message back to client
- packet = new DatagramPacket(packet.getData(), packet.getLength(), packet.getAddress(), packet.getPort());
- serverSocket.send(packet);
- }
- }
- public static void main(String[] args) throws SocketException {
- UDPEchoServer server = new UDPEchoServer(8080);
- server.start();
- }
- }
- ```
- *UDP EchoClient Program (UDPEchoClient.java)*
- ```
- import java.io.*;
- import java.net.*;
- import java.util.*;
- public class UDPEchoClient {
- private DatagramSocket clientSocket;
- public UDPEchoClient(String host, int port) throws SocketException {
- clientSocket = new DatagramSocket();
- System.out.println("Connected to UDP Echo Server");
- }
- public void communicate() throws IOException {
- Scanner scanner = new Scanner(System.in);
- while (true) {
- System.out.print("Enter message: ");
- String message = scanner.nextLine();
- // Send message to server
- byte[] buffer = message.getBytes();
- DatagramPacket packet = new DatagramPacket(buffer, buffer.length, InetAddress.getByName("localhost"), 8080);
- clientSocket.send(packet);
- // Receive response from server
- buffer = new byte[1024];
- packet = new DatagramPacket(buffer, buffer.length);
- clientSocket.receive(packet);
- String response = new String(packet.getData(), 0, packet.getLength());
- System.out.println("Server response: " + response);
- }
- }
- public static void main(String[] args) throws SocketException {
- UDPEchoClient client = new UDPEchoClient("localhost", 8080);
- client.communicate();
- }
- }```
- [3/21, 4:00 PM] Prateek: Lab Assign 8, Q14-Create a socket program for HTTP web page upload and download
- [3/21, 4:00 PM] Prateek: *Server Code (HTTPServer.java)*
- ```
- import java.io.*;
- import java.net.*;
- import java.util.*;
- public class HTTPServer {
- private ServerSocket serverSocket;
- public HTTPServer(int port) throws IOException {
- serverSocket = new ServerSocket(port);
- System.out.println("HTTP Server started on port " + port);
- }
- public void start() throws IOException {
- while (true) {
- Socket clientSocket = serverSocket.accept();
- System.out.println("Client connected");
- // Create input and output streams
- BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
- PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
- // Read HTTP request
- String request = in.readLine();
- System.out.println("Received request: " + request);
- // Parse request
- String[] parts = request.split(" ");
- String method = parts[0];
- String url = parts[1];
- // Handle GET request
- if (method.equals("GET")) {
- String fileName = url.substring(1); // Remove leading '/'
- File file = new File(fileName);
- if (file.exists()) {
- // Send HTTP response
- out.println("HTTP/1.1 200 OK");
- out.println("Content-Type: text/html");
- out.println();
- // Send file contents
- FileReader fileReader = new FileReader(file);
- BufferedReader fileBufferedReader = new BufferedReader(fileReader);
- String line;
- while ((line = fileBufferedReader.readLine()) != null) {
- out.println(line);
- }
- fileBufferedReader.close();
- } else {
- // Send 404 error
- out.println("HTTP/1.1 404 Not Found");
- out.println();
- }
- }
- // Handle POST request (file upload)
- else if (method.equals("POST")) {
- // Read file contents from request body
- StringBuilder fileContents = new StringBuilder();
- String line;
- while ((line = in.readLine()) != null) {
- if (line.isEmpty()) {
- break; // End of headers
- }
- }
- while ((line = in.readLine()) != null) {
- fileContents.append(line).append("\n");
- }
- // Save file contents to local file
- String fileName = url.substring(1); // Remove leading '/'
- File file = new File(fileName);
- FileWriter fileWriter = new FileWriter(file);
- fileWriter.write(fileContents.toString());
- fileWriter.close();
- // Send HTTP response
- out.println("HTTP/1.1 200 OK");
- out.println();
- }
- clientSocket.close();
- }
- }
- public static void main(String[] args) throws IOException {
- HTTPServer server = new HTTPServer(8080);
- server.start();
- }
- }
- ```
- *Client Code (HTTPClient.java)*
- ```
- import java.io.*;
- import java.net.*;
- import java.util.*;
- public class HTTPClient {
- private Socket clientSocket;
- public HTTPClient(String host, int port) throws UnknownHostException, IOException {
- clientSocket = new Socket(host, port);
- System.out.println("Connected to HTTP Server");
- }
- public void downloadFile(String url) throws IOException {
- // Send HTTP GET request
- PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
- out.println("GET " + url + " HTTP/1.1");
- out.println("Host: localhost:8080");
- out.println();
- out.flush();
- // Read HTTP response
- BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
- String response = in.readLine();
- System.out.println("Received response: " + response);
- // Read file contents from response body
- StringBuilder fileContents = new StringBuilder();
- String line;
- while ((line = in.readLine()) != null) {
- fileContents.append(line).append("\n");
- }
- // Save file contents to local file
- String fileName = url.substring(1); // Remove leading '/'
- File file = new File(fileName);
- FileWriter fileWriter = new FileWriter(file);
- fileWriter.write(fileContents.toString());
- fileWriter.close();
- }
- public void uploadFile(String url, String fileContents) throws IOException {
- // Send HTTP POST request
- PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
- out.println("POST " + url + " HTTP/1.1");
- out.println("Host: localhost:8080");
- out.println("Content-Type: text/plain");
- out.println("Content-Length: " + fileContents.length());
- out.println();
- out.println(fileContents);
- out.flush();
- // Read HTTP response
- BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
- String response = in.readLine();
- System.out.println("Received response: " + response);
- }
- public static void main(String[] args) throws UnknownHostException, IOException {
- HTTPClient client = new HTTPClient("localhost", 8080```)
- [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.
- [3/21, 4:00 PM] Prateek: *IPTranslator.java*
- ```
- import java.util.Scanner;
- public class IPTranslator {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- System.out.print("Enter a dotted decimal IP address: ");
- String ipAddress = scanner.nextLine();
- String[] parts = ipAddress.split("\\.");
- if (parts.length != 4) {
- System.out.println("Invalid IP address format.");
- return;
- }
- StringBuilder binaryAddress = new StringBuilder();
- for (String part : parts) {
- int decimalValue = Integer.parseInt(part);
- String binaryValue = Integer.toBinaryString(decimalValue);
- // Pad with leading zeros to ensure 8-bit binary representation
- while (binaryValue.length() < 8) {
- binaryValue = "0" + binaryValue;
- }
- binaryAddress.append(binaryValue);
- }
- System.out.println("32-bit Binary IP Address: " + binaryAddress.toString());
- }
- }
- 1. The program prompts the user to enter a dotted decimal IP address.
- 2. It splits the input string into four parts using the dot (`.`) as a delimiter.
- 3. It checks if the input IP address has exactly four parts. If not, it displays an error message.
- ```Enter a dotted decimal IP address: 192.168.1.1
- 32-bit Binary IP Address: 11000000101010000000000100000001```
Advertisement
Add Comment
Please, Sign In to add comment