|
Hint
|
|
Answer
|
|
Create registry
|
|
Registry registry = LocateRegistry.createRegistry(port);
|
|
Get registry
|
|
Registry registry = LocateRegistry.getRegistry(host, port);
|
|
Bind RMI object to registry
|
|
Registry.bind(“name”, object);
|
|
Look up remote object on registry
|
|
ObjectClass object = (ObjectClass) registry.lookup(“name”);
|
|
RMI interface
|
|
public interface InterfaceClass extends Remote{
|
|
|
|
void method() throws RemoteException;
|
|
|
|
}end
|
|
Implementing interface
|
|
class ObjectClass implements InterfaceClass {
|
|
|
|
@ Override
|
|
|
|
public void method() throws RemoteExceptionstartend
|
|
|
|
}end
|
|
Handle an error
|
|
Try {
|
|
|
|
ObjectClass.method();
|
|
|
|
} catch (RemoteException e){
|
|
|
|
Handle();
|
|
|
|
}end
|
|
Open server socket
|
|
ServerSocket server;
|
|
|
|
DataOutputStream os;
|
|
|
|
DataInputStream is;
|
|
|
|
server = new ServerSocket(port);
|
|
Wait for client request
|
|
Socket client = server.accept();
|
|
Create IO streams for communication
|
|
is = new DataInputStream(client.getInputStream());
|
|
|
|
os = new DataOutputStream(client.getOutputStream());
|
|
Perform communication
|
|
String line = is.readLine();
|
|
|
|
os.writeBytes("message");
|
|
Create socket object for client
|
|
client = new Socket( server, port);
|
|
Close socket
|
|
client.close();
|
|
|
|
is.close();
|
|
|
|
os.close();
|
|
Handle socket errors
|
|
catch(UnknownHostException e){
|
|
|
|
handle();
|
|
|
|
}end
|
|
|
|
catch(IOException e) {
|
|
|
|
handle();
|
|
|
|
}end
|
|
Extending thread class (MyThread, runThread())
|
|
class MyThread extends Thread{
|
|
|
|
public void run(){
|
|
|
|
runThread();
|
|
|
|
}end
|
|
|
|
}end
|
|
Implementing Runnable (MyThread, runThread())
|
|
class MyThread implements Runnable{
|
|
|
|
public void run(){
|
|
|
|
runThread();
|
|
|
|
}end
|
|
|
|
}end
|
|
Instantiate and start a thread (one line)
|
|
new MyThread().start();
|
|
Set Priority of a thread
|
|
ThreadName.setPriority(priority)
|