Κατανεμημένα Συστήματα - Βοηθητική κόλλα Α4

3
//Client rmi static final int AuthenticationPort = 1099; static String ConnectionStringAuthentication = "//localhost:" + AuthenticationPort + "/" + "AuthenticationServer"; Authentication look_op =(Authentication)Naming.lookup(ConnectionStringAuthentication); look_op.setNum(15,20); //rmi server import java.rmi.*; static final int AuthenticationPort = 1099; ServerImplemetation server = new ServerImplemetation(); LocateRegistry.createRegistry(AuthenticationPort); String url_auth = "//localhost:" + AuthenticationPort + "/" + "AuthenticationServer"; Naming.rebind(url_auth, server); //server implementation public class ServerImplemetation extends UnicastRemoteObject implements Authentication{ public ServerImplemetation() throws RemoteException { ... } private boolean Authentication(String username, String Password) { ... } } //server interface import java.rmi.*; public interface Authentication extends Remote{ public boolean login(User user)throws RemoteException; public String register(User user)throws RemoteException; public void logout(User user) throws RemoteException; } ::Compile Storage Server cd ..\Storage\src\ javac -encoding UTF8 *.java rmic ProceduresStorageImpl copy ProceduresStorageImpl_Stub.class ..\..\"Authentication Server"\src ::start server cd ..\Storage\src\ java -Djava.security.policy=storage.policy Storage pause //client socket ObjectOutputStream OutStream; ObjectInputStream InStream; Socket _client; _client = new Socket(); _client.connect(new InetSocketAddress(InetAddress.getLocalHost(), 6666)); if (_client.isConnected()) { System.out.println("Connected to the server..."); } OutStream = new ObjectOutputStream(_client.getOutputStream()); InStream = new ObjectInputStream(_client.getInputStream()); OutStream.writeObject(..) InStream.readObject()

Upload: kostas-chasiotis

Post on 20-Jul-2016

10 views

Category:

Documents


2 download

DESCRIPTION

Σημειώσεις πάνω σε sockets και rmi. Γλώσσα προγραμματισμού: Java

TRANSCRIPT

Page 1: Κατανεμημένα Συστήματα - Βοηθητική κόλλα Α4

//Client rmistatic final int AuthenticationPort = 1099;static String ConnectionStringAuthentication = "//localhost:" + AuthenticationPort + "/" + "AuthenticationServer";Authentication look_op =(Authentication)Naming.lookup(ConnectionStringAuthentication);look_op.setNum(15,20);

//rmi serverimport java.rmi.*;static final int AuthenticationPort = 1099;

ServerImplemetation server = new ServerImplemetation();LocateRegistry.createRegistry(AuthenticationPort);String url_auth = "//localhost:" + AuthenticationPort + "/" + "AuthenticationServer";Naming.rebind(url_auth, server);

//server implementationpublic class ServerImplemetation extends UnicastRemoteObject implements Authentication{ public ServerImplemetation() throws RemoteException { ... } private boolean Authentication(String username, String Password) { ... }}

//server interfaceimport java.rmi.*;public interface Authentication extends Remote{ public boolean login(User user)throws RemoteException; public String register(User user)throws RemoteException; public void logout(User user) throws RemoteException;}

::Compile Storage Servercd ..\Storage\src\javac -encoding UTF8 *.javarmic ProceduresStorageImplcopy ProceduresStorageImpl_Stub.class ..\..\"Authentication Server"\src

::start servercd ..\Storage\src\java -Djava.security.policy=storage.policy Storage pause

//client socketObjectOutputStream OutStream;ObjectInputStream InStream;Socket _client;

_client = new Socket();_client.connect(new InetSocketAddress(InetAddress.getLocalHost(), 6666));if (_client.isConnected()) { System.out.println("Connected to the server...");}OutStream = new ObjectOutputStream(_client.getOutputStream());InStream = new ObjectInputStream(_client.getInputStream());

OutStream.writeObject(..)InStream.readObject()

//Get clients' connectionsServerSocket myServer;myServer = new ServerSocket(6666, 50);

while (true) { Socket myClient = myServer.accept(); if (myClient.isConnected()) { System.out.println("Connected to the server..."); }

Page 2: Κατανεμημένα Συστήματα - Βοηθητική κόλλα Α4

new ClientHandler(UserDatabase, _client).start(); //Read-write to clientOutStreamUser = new ObjectOutputStream(client.getOutputStream());InStreamUser = new ObjectInputStream(client.getInputStream());Object obj = InStreamUser.readObject();OutStreamUser.writeObject("Connected");

//Classpublic class ClientHandler extends Thread {..}

//examplepublic synchronized int get() {while (available == false) { try { wait(); } catch (InterruptedException e) { } }available = false;System.out.println("in get");notifyAll();return contents;}public synchronized void put(int value) { while (available == true) { try { wait(); }catch (InterruptedException e) { } }contents = value;available = true;System.out.println("in put");notifyAll();}//security managerRMISecurityManager security = new RMISecurityManager();System.setSecurityManager(security);

grant {permission java.io.FilePermission "filePath", "permissionList";permission java.io.FilePermission "/home/eric/-", "read";permission java.io.FilePermission "/home/eric/temp/*", "read, write, delete";permission java.net.SocketPermission "*.example.com:1024-", "connect,accept";permission java.net.SocketPermission "*. example.com:80", "connect";};

java -Djava.security.policy=EUStats.policy EUStatsClient//helpfullScanner read= new Scanner(System.in)BufferedReader in = new BufferedReader(new FileReader("<Filename>"));String line; while((line = in.readLine()) != null){System.out.println(line);} in.close();//readObjectInputStream inputStream = new ObjectInputStream(new FileInputStream(filename));//Read objObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream("output")); oos.writeObject(MenuArray);//Πρέπει να είναι serializableRunnable, Thread