We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,868 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

RMI chat server help

Hi all,
I'm developing a simple RMI chat server.
Im getting following exception when i execute Client.
Server works well.

java.lang.IllegalArgumentException: argument type mismatch
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:601)
        at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:322)
        at sun.rmi.transport.Transport$1.run(Transport.java:177)
        at sun.rmi.transport.Transport$1.run(Transport.java:174)
        at java.security.AccessController.doPrivileged(Native Method)
        at sun.rmi.transport.Transport.serviceCall(Transport.java:173)
        at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:553)
        at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:808)
        at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:667)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
        at java.lang.Thread.run(Thread.java:722)
        at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteCall.java:273)
        at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:251)
        at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:160)
        at ServrImplementation_Stub.joinChatRoom(Unknown Source)
        at ClientImplementation.<init>(ClientImplementation.java:28)
        at ClientImplementation.main(ClientImplementation.java:79)

this is my client implementation

 public ClientImplementation(String name) throws RemoteException
    {
            String arg = "rmi://localhost:2828/chatfun";
            myName=name;
           // myGui=gui;
            try{
            csi=(chatServerInterface)Naming.lookup(arg);
            csi.joinChatRoom(name,(chatClientInterface)this);//this is the line which the exception shows
            this.chatStart();
            }catch(Exception ex)
            {
                //JOptionPane.showMessageDialog(null, "Failed\n"+ex.printStackTrace());
                //System.out.println("Failed to bind\n"+ex.printStackTrace());
                ex.printStackTrace();
                System.exit(0);
            }


    }

this is my server interface

public interface chatServerInterface extends Remote{

    public void joinChatRoom(String clientName,chatClientInterface c) throws RemoteException ;
    public void leaveChatRoom(String clientName) throws RemoteException;
    public void sendToAll(String msg,String name) throws RemoteException;
    public java.util.Iterator getOnlineClients() throws RemoteException;


}

PLZZZZZZZZZZz....help me i cannot find the mistake i made.

3
Contributors
7
Replies
1 Day
Discussion Span
6 Months Ago
Last Updated
8
Views
coroll
Junior Poster in Training
98 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Have you checked what value is assigned to csi before it calls the joinChatRoom()?

Taywin
Posting Maven
2,633 posts since Apr 2010
Reputation Points: 275
Solved Threads: 375
Skill Endorsements: 17

Null is assigned.

public class ClientImplementation{

    chatServerInterface csi = null;

     public ClientImplementation(String name) throws RemoteException
    {
            String arg = "rmi://localhost:2828/chatfun";
            myName=name;
           // myGui=gui;
            try{
            csi=(chatServerInterface)Naming.lookup(arg);
            csi.joinChatRoom(name,(chatClientInterface)this);
            this.chatStart();
            }catch(Exception ex)
            {
                //JOptionPane.showMessageDialog(null, "Failed\n"+ex.printStackTrace());
                //System.out.println("Failed to bind\n"+ex.printStackTrace());
                ex.printStackTrace();
                System.exit(0);
            }


    }
coroll
Junior Poster in Training
98 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

When you cast null with a type, it doesn't really convert it to an object with null value. As a result, it is not the type object that is sent but is still null. Then at the server side, it doesn't know what was coming in (argument). I guess you didn't handle this null value on your server side either? How your server side implement for this part? Even though you may handle this issue in your client side before you attempt to join, it would be better to handle it on your server side.

Taywin
Posting Maven
2,633 posts since Apr 2010
Reputation Points: 275
Solved Threads: 375
Skill Endorsements: 17

This is my server

public synchronized void joinChatRoom (String clientName, chatClientInterface c) throws RemoteException
    {

                    if (clientList.containsKey(clientName))
                    {
                       c.nameAvailable();
                    }
                    else
                    {
                        System.out.println(clientName+" is now joined the chat room!");
                        clientList.put(clientName,c);

                        Collection cc= clientList.keySet();
                        Iterator itt= cc.iterator();

                        while(itt.hasNext())
                        {
                            chatClientInterface client=(chatClientInterface)itt.next();
                            try{
                                client.receiveMessage(clientName+" is now joind the chat room!", clientName);
                            }catch(Exception ex)
                            {
                                System.out.println("failed to join "+clientName+ "into chat room");
                                ex.printStackTrace();
                            }

                    }

                    }


    }

Do i need to remove null?

chatServerInterface csi;
coroll
Junior Poster in Training
98 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Oh sorry, what I mean is this line...

csi=(chatServerInterface)Naming.lookup(arg);

What happen to csi after that line? Did you check it up before you attempt to join the server chat room?

Taywin
Posting Maven
2,633 posts since Apr 2010
Reputation Points: 275
Solved Threads: 375
Skill Endorsements: 17

This is the implementation.

public ClientImplementation(String name) throws RemoteException
    {
            String arg = "rmi://localhost:2828/chatfun";
            myName=name;

            try{
            csi=(chatServerInterface)Naming.lookup(arg);
            csi.joinChatRoom(name,(chatClientInterface)this);
            this.chatStart();
            }catch(Exception ex)
            {

                ex.printStackTrace();
                System.exit(0);
            }
    }
     public void chatStart()
    {
    Scanner in = new Scanner(System.in);
        while (true) {
            try {
                String lastMessage = in.nextLine();
                csi.sendToAll(lastMessage,myName);
            }catch (Exception e) {
                System.out.println("exiting with error...");
                System.err.println(e);
                System.exit(1); //exit
            }
        }
    }

What do you mean by check csi? You mean check for null.
I add this line

if(csi!=null)
   csi.joinChatRoom(name,this);

But still gives me the exception.

I cast this because it gives me a IllegalArgumentException, I thought to cast "this".

(chatClientInterface)this

PLZ help me solve this.

coroll
Junior Poster in Training
98 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

What is not the implementation of what? That post makes little sense.
Have you tried printing csi immediately after the Naming.lookup? You say it's null, but the API documentation says lookup will throw various Execptions rather than just return null if there is a problem.

JamesCherrill
... trying to help
Moderator
8,512 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,455
Skill Endorsements: 30

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0819 seconds using 2.76MB