Dis Practical-3


// TimeClient.java
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

public class TimeClient {
    public static void main(String[] args) {
        try {
            // Locate the registry
            Registry registry = LocateRegistry.getRegistry("localhost", 1099);
            // Look up the remote object
            TimeService timeService = (TimeService) registry.lookup("TimeService");
            // Call the remote method
            String currentTime = timeService.getCurrentTime();
            System.out.println(currentTime);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


// TimeServer.java
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

public class TimeServer {
    public static void main(String[] args) {
        try {
            // Create the remote object
            TimeService timeService = new TimeServiceImpl();

            // Create the registry on port 1099
            Registry registry = LocateRegistry.createRegistry(1099);

            // Bind the time service to the registry
            registry.rebind("TimeService", timeService);
            System.out.println("TimeService is running...");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

import java.rmi.*;

public interface TimeService extends Remote {
    String getCurrentTime() throws RemoteException;
}
// TimeServiceImpl.java
import java.rmi.server.UnicastRemoteObject;
import java.rmi.RemoteException;
import java.util.Date;

public class TimeServiceImpl extends UnicastRemoteObject implements TimeService {
    protected TimeServiceImpl() throws RemoteException {
        super();
    }

    @Override
    public String getCurrentTime() throws RemoteException {
        return "Current time is: " + new Date().toString();
    }
}

Leave a Comment

Your email address will not be published. Required fields are marked *

error: Content is protected !!
Scroll to Top