JAVA RMI PROBLEM

Joined
Aug 8, 2012
Messages
1
Reaction score
0
hey everyone, I have a problem with one project for faculty with java rmi.
This are my classes and when I run the klient1.class there is some errors:

klient1:

import java.rmi.*;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

import java.io.*;

import javax.swing.JOptionPane;

public class klient1
{

public static void main(String[] args) throws IOException
{
Interface s;
Registry registry;
String serverAddress = args[0];
String serverPort = args[1];
try
{
registry = LocateRegistry.getRegistry(serverAddress,(new Integer(serverPort)).intValue());
s = (Interface)(registry.lookup("server"));
String kod = "0";
while(!kod.equals("1"))
{
String a5 = JOptionPane.showInputDialog("1-registracija; 2-vrakanje na pass; 3-promena");

if(a5.equals("1"))
{
String a1 = JOptionPane.showInputDialog("Vnesete ime: ");
JOptionPane.showMessageDialog(null, "Uspesno vnesovte: " +a1, "Ime:", JOptionPane.PLAIN_MESSAGE);
String a2 = JOptionPane.showInputDialog("Vnesete prezime: ");
JOptionPane.showMessageDialog(null, "Uspesno vnesovte: " +a2, "Prezime:", JOptionPane.PLAIN_MESSAGE);
String a3 = JOptionPane.showInputDialog("Vnesete user: ");
JOptionPane.showMessageDialog(null, "Uspesno vnesovte: " +a3, "User:", JOptionPane.PLAIN_MESSAGE);
String a4 = JOptionPane.showInputDialog("Vnesete password: ");
JOptionPane.showMessageDialog(null, "Uspesno vnesovte: " +a4, "Password:", JOptionPane.PLAIN_MESSAGE);
s.registriraj(a1, a2, a3, a4);

}

else if(a5.equals("2"))
{
String a1 = JOptionPane.showInputDialog("Vnesete user: ");
JOptionPane.showMessageDialog(null, "Vasiot pass e: " +s.vrati_pass(a1), "User:", JOptionPane.PLAIN_MESSAGE);
//System.out.println("vasiot pass e: " + s.vrati_pass(a1));
}
else if(a5.equals("3"))
{
String a1 = JOptionPane.showInputDialog("Vnesete user: ");
//JOptionPane.showMessageDialog(null, "Vasiot pass e: " +s.vrati_pass(a1), "naslov", JOptionPane.PLAIN_MESSAGE);
String a2 = JOptionPane.showInputDialog("Vnesete nov pass: ");
JOptionPane.showMessageDialog(null, "Vasiot nov pass e: " +a2, "Nov pass:", JOptionPane.PLAIN_MESSAGE);
s.promeni(a1,a2);
}

}
}
catch(RemoteException e)
{
e.printStackTrace();
}
catch(NotBoundException e)
{
e.printStackTrace();
}
}
}


klient2:

import java.math.*;
import java.rmi.*;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.*;
import java.rmi.Naming;
import java.io.*;
import java.rmi.RMISecurityManager;

import javax.swing.JOptionPane;

public class klient2
{
public static void main(String[] args) throws IOException
{
// sluzi za avtentikacija
Interface server;
Registry registry;
String serverAddress = args[0];
String serverPort = args[1];
try
{
registry = LocateRegistry.getRegistry(serverAddress,(new Integer(serverPort)).intValue());
server = (Interface)(registry.lookup("server"));
String kod = "0";
while(!kod.equals("1"))
{
/*BufferedReader tastatura = new BufferedReader( new InputStreamReader(System.in));
System.out.println("vnesete user");
String a1 = tastatura.readLine();
System.out.println("vnesete pass");
String a2 = tastatura.readLine();
System.out.println(server.avtenticiraj(a1, a2));
System.out.println("1 za kraj");
kod = tastatura.readLine();*/
String a1 = JOptionPane.showInputDialog("Vnesete user: ");
//JOptionPane.showMessageDialog(null, "Uspesno vnesovte: " +a1, "naslov", JOptionPane.PLAIN_MESSAGE);
String a2 = JOptionPane.showInputDialog("Vnesete password: ");
//JOptionPane.showMessageDialog(null, "Uspesno vnesovte: " +a2, "naslov", JOptionPane.PLAIN_MESSAGE);
JOptionPane.showMessageDialog(null, "Uspesno vnesovte: " +server.avtenticiraj(a1, a2), "Uspesno vnesuvanje", JOptionPane.PLAIN_MESSAGE);
kod = "1";
}
}
catch(RemoteException e)
{
e.printStackTrace();
}
catch(NotBoundException e)
{
e.printStackTrace();
}
}
}


server:

import java.math.*;
import java.net.InetAddress;
import java.rmi.*;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.*;
import java.rmi.Naming;
import java.util.LinkedList;
import java.io.*;

interface Interface extends java.rmi.Remote
{
public String vrati_pass(String user) throws RemoteException;
public void registriraj(String ime, String prezime, String user, String pass) throws RemoteException;
public void promeni(String user, String pass) throws RemoteException;
public String avtenticiraj(String user, String pass) throws RemoteException;
}

public class server extends UnicastRemoteObject implements Interface
{
LinkedList<korisnik> lista = new LinkedList<korisnik>();
int thisPort;
String thisAddress;
Registry registry;
public server() throws RemoteException
{
try
{
thisAddress= (InetAddress.getLocalHost()).toString();
}
catch(Exception e)
{
throw new RemoteException("can't get inet address.");
}
thisPort=3434;
System.out.println("this address="+thisAddress+",port="+thisPort);
try
{
registry = LocateRegistry.createRegistry( thisPort );
registry.rebind("server", this);
}
catch(RemoteException e)
{
throw e;
}
}
public synchronized void registriraj(String ime, String prezime, String korisnik, String pass) throws RemoteException
{
lista.add(new korisnik(ime,prezime,korisnik,pass));
}
public synchronized String vrati_pass(String u) throws RemoteException
{
String s = "";
for(int i=0;i<lista.size();i++)
{
if(lista.get(i).user.equals(u))
{
s = lista.get(i).pass.toString();
}
break;
}
return s;
}
public synchronized void promeni(String u, String p) throws RemoteException
{
for(int i=0;i<lista.size();i++)
{
if(lista.get(i).user.equals(u))
{
lista.get(i).pass = p;
break;
}

}
}
public synchronized String avtenticiraj(String u , String p) throws RemoteException
{
String s = "";
for(int i=0;i<lista.size();i++)
{
if(lista.get(i).user.equals(u) && lista.get(i).pass.equals(p))
{
s = "OK";
break;
}
else
s = "not OK";
}
return s;
}
public static void main(String[] args)
{
try
{
server s=new server();
s.registriraj("aaa", "bbb", "ccc", "ddd");
}
catch (Exception e)
{
e.printStackTrace();
System.exit(1);
}
}
}


korisnik:

class korisnik
{
String ime;
String prezime;
String user;
String pass;

public korisnik(String a1, String a2, String a3, String a4)
{
ime = a1;
prezime = a2;
user = a3;
pass = a4;
}


public String getIme() {
return ime;
}


public void setIme(String ime) {
this.ime = ime;
}


public String getPrezime() {
return prezime;
}


public void setPrezime(String prezime) {
this.prezime = prezime;
}


public String getUser() {
return user;
}


public void setUser(String user) {
this.user = user;
}


public String getPass() {
return pass;
}


public void setPass(String pass) {
this.pass = pass;
}


public String toString() {
return "korisnik [ime=" + ime + ", prezime=" + prezime + ", user="
+ user + ", pass=" + pass + "]";
}

}



The errors when I run klient1 or klient2 are :

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at klient1.main(klient1.java:17)

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at klient2.main(klient2.java:18)

What is the problem i can't find out, please help. :/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top