0

I wrote a small rmi client server program to convert Celsius to Fahrenheit. When I create a console client to connect to the server, it works fine. But when I create a GUI based client that extends JApplet class, the client is not running fine. Each time when I run the client, it is throwing an Exception with the message "Error: access denied ("java.net.SocketPermission" "127.0.0.1:1099" "connect, resolve"). How to solve this problem?

Server Interface:

import java.rmi.*;
public interface ConvServerInterface extends Remote{
    public double celToFah(double cel)throws RemoteException;
}

Server Implementation:

import java.rmi.*;
import java.rmi.server.*;
public class ConvServerMainClass extends UnicastRemoteObject implements ConvServerInterface {

public ConvServerMainClass() throws Exception{}
@Override
public double celToFah(double cel) throws RemoteException {
    return ((cel*(9.0/5.0)+32));
}

public static void main(String[] args) throws Exception
{
    ConvServerMainClass serverObj = new ConvServerMainClass();
    Naming.rebind("Conversion",serverObj);
    System.out.println("Server is ready to receiver request!");
}

}

Client

import java.rmi.*;
import java.net.*;
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class ConvClient extends JApplet implements ActionListener{

JLabel lblCel, lblFah;
JTextField txtCel, txtFah;
JButton btnConv;
JPanel pnl;

public void init()
{
    initComp();
    setActionListner();
    addComponents();
}

public void initComp()
{
    lblCel = new JLabel("Celsius: ");
    lblFah = new JLabel("Fahrenheit: ");
    txtCel = new JTextField(13);
    txtFah = new JTextField(13);
    btnConv = new JButton("Convert");
    pnl = new JPanel();
}

public void setActionListner()
{
    btnConv.addActionListener(this);
}

public void addComponents()
{
    pnl.setLayout(new GridLayout(3,2));
    pnl.add(lblCel); pnl.add(txtCel);
    pnl.add(lblFah); pnl.add(txtFah);
    pnl.add(btnConv);

    getContentPane().setLayout(new FlowLayout());
    getContentPane().add(pnl);
}

@Override
public void actionPerformed(ActionEvent e) {

    double cel, fah;
    try{
        if(System.getSecurityManager() == null)
        {
            System.setSecurityManager(new RMISecurityManager());
        }
        ConvServerInterface serobj =  (ConvServerInterface) Naming.lookup("rmi://localhost/Conversion");
        if(e.getSource()==btnConv)
        {
            cel = Double.parseDouble(txtCel.getText());
            fah = serobj.celToFah(cel);
            txtFah.setText(String.valueOf(fah));
        }
    }
    catch(Exception ex){
        txtFah.setText("Error: "+ ex.getMessage());
    }

}
}

First, I compiled both Server side Interface and Class file. Then, I have create a Stub Class by

    _rmic ConvServerMainClass_  

Then, I start my registry by

   _rmiregistry_  

Then, I run my Server Class File by

   _rmiregistry_  

Finally, I copied both Server Interface Class file and Stub Class file to the Client Side and I compile and run my client by

   _javac ConvClient.java_  
   _appletviewer ConvClient.java_  
user207421
  • 305,947
  • 44
  • 307
  • 483
Sai
  • 23
  • 7

0 Answers0