Ethan Frome

annuncio pubblicitario
Corso di Sistemi Distribuiti
Esercitazione guidata all’utilizzo di Java RMI
Scrittura di un fileserver che consenta la creazione in remoto di un file con il successivo
inserimento di una stringa letta da tastiera.
1. Attivazione del servizio di registry sull’host che ospita il server:
start rmiregistry 2005 (sotto Windows attiva il servizio sul porto 2005)
2. Scrittura dell’interfaccia applicativa:
package testrmi2;
import java.rmi.*;
public interface fileserver extends Remote {
public void scrivifile(String nomefile, String arg) throws
RemoteException;
}
3. Scrittura del server che implementa l’interfaccia applicativa:
package testrmi2;
import java.io.*;
import java.rmi.*;
import java.rmi.server.*;
public class fileserverimpl extends UnicastRemoteObject
implements fileserver{
public fileserverimpl() throws RemoteException {
super();
}
public void scrivifile(String nomefile, String arg) throws
RemoteException {
try {
FileWriter myfile = new FileWriter(nomefile);
myfile.write(arg);
myfile.close(); }
catch (Exception e) {System.out.println(e);}
}
public static void main (String arg[]) {
try {
fileserverimpl s = new fileserverimpl();
Naming.rebind("//nomehost:2005/fileserverimpl", s);
System.out.println("Server attivato.");
} catch (Exception e) {System.out.println("errore
inizializzazione server");}}}
4. Creazione automatica stub e skeleton:
rmic testrmi2.fileserverimpl (nome della classe server)
verranno generati due file .class Stub e Skel
5. Scrittura del client:
package testrmi2;
import java.rmi.*;
import java.io.*;
public class client {
public static void main (String arg[]) {
fileserver myserver;
String nomefile=" ";
String testo=" ";
System.out.println("Scrivi il nome del file");
nomefile=ReadString();
System.out.println("Scrivi il testo");
testo=ReadString();
try {
myserver = (fileserver)
Naming.lookup("//nomehost:2005/fileserverimpl");
myserver.scrivifile(nomefile, testo);
} catch (Exception e) {System.out.println(e);}
}
public static String ReadString() {
BufferedReader stdIn =new BufferedReader(new
InputStreamReader(System.in));
String s=" ";
try{
s=stdIn.readLine();
}
catch(IOException e)
{System.out.println(e.getMessage()); }
return s;
}
}
6. Attivazione del server che rimane in attesa
7. Attivazione del client
Scarica