0

This program should send a text message from client to the server. i finished compiled the file. the server.java runs successfully but the client.java shows error.
the error list is too long so i only screenshot few lines. enter image description here

this is the code
ReceiverMessageInterface.java

package com.mycompany.dsa3;
import java.rmi.*;

public interface ReceiveMessageInterface extends Remote{
  void receiveMessage(String x) throws RemoteException;
}

RmiServer.java

package com.mycompany.dsa3;
import java.rmi.*;
import java.rmi.registry.*;
import java.rmi.server.*;
import java.net.*;

public class RmiServer extends 
  java.rmi.server.UnicastRemoteObject implements ReceiveMessageInterface{
  String address;
  Registry registry; 

  public void receiveMessage(String x) throws RemoteException{
  System.out.println(x);
  }
  
  public RmiServer() throws RemoteException{
  try{  
  address = (InetAddress.getLocalHost()).toString();
  }
  catch(Exception e){
  System.out.println("can't get inet address.");
  }
  int port=3232; 
  System.out.println("this address=" + address +  ",port=" + port);
  try{
  registry = LocateRegistry.createRegistry(port);
  registry.rebind("rmiServer", this);
  }
  catch(RemoteException e){
  System.out.println("remote exception"+ e);
  }
  }
  static public void main(String args[]){
  try{
  RmiServer server = new RmiServer();
  }
  catch (Exception e){
  e.printStackTrace();
  System.exit(1);
  }
  }
}

RmiClient.java

package com.mycompany.dsa3;
import java.rmi.*;
import java.rmi.registry.*;
import java.net.*;

public class RmiClient{
  static public void main(String args[]){
  ReceiveMessageInterface rmiServer;
  Registry registry;
  String serverAddress=args[0];
  String serverPort=args[1];
  String text=args[2];
  System.out.println    ("sending " + text + " to " +serverAddress + ":" + serverPort);
  try{
  registry=LocateRegistry.getRegistry
  (serverAddress,(new Integer(serverPort)).intValue());
  rmiServer=(ReceiveMessageInterface)(registry.lookup("rmiServer"));
  // call the remote method
  rmiServer.receiveMessage(text);
  }
  catch(RemoteException e){
  e.printStackTrace();
  }
  catch(NotBoundException e){
  System.err.println(e);
  }
  }
}  

please help me to correct the code. thanks

1
  • This has nothing to do with rmi. Commented Jan 24, 2022 at 1:03

1 Answer 1

0

It seems error occurred while reading element from args array. Please check length of argument you are passing. We should also always check length of args[] before reading element from it. Refer below link and answers What is "String args[]"? parameter in main method Java

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.