J
JP Martin
All,
I am trying to use RMI to get to java programs to talk to each other
in a simpler way than sending messages over sockets. In my case, the
two programs already use sockets for some of their communication, so
it seems that I should not have to bother with the RMI Registry... or
should I?
Here's my client code:
public void hit() throws Exception {
Socket socket = new Socket("localhost",9943);
ObjectInputStream in = new
ObjectInputStream(socket.getInputStream());
Object o = in.readObject();
System.out.println("Received: "+o);
((HitMe)o).hit();
}
public static void main(String argv[]) throws Exception {
new TestRMIClient().hit();
}
The HitMe interface extends Remote and only has the hit() method. I
wrote an implementation for it that just displays a message.
The server does mostly the same thing, sends the object to the
client. The relevant lines are:
// in the constructor; MyHitMe extends UnicastRemoteObject
// and implements the HitMe interface.
hitme = new MyHitMe();
// in response to a connection from a client
out.writeObject( hitme );
Here's the output I get from the client:
Received: MyHitMe[RemoteStub [ref:
[endpoint:[<my-ip-here>:46846](local),objID:[0]]]]
I'm hit!
This shows that the passed object was (as I was expecting) not of the
implementation type (MyHitMe) but instead of the stub type generated
by
rmic (RemoteStub). Now, I'm expecting the stub to forward method calls
to the server process, so then the "I'm hit" message should be
displayed
at the server.
But it's not. The message gets displayed on the client, and I don't
understand why.
Does anyone here understand what's happening? Will I have to use the
RMI Registry even though I have no need for discovery since my app
already runs at a well-known location on a well-known port?
Thanks in advance,
JP
(please reply to the thread since I don't check that email often)
I am trying to use RMI to get to java programs to talk to each other
in a simpler way than sending messages over sockets. In my case, the
two programs already use sockets for some of their communication, so
it seems that I should not have to bother with the RMI Registry... or
should I?
Here's my client code:
public void hit() throws Exception {
Socket socket = new Socket("localhost",9943);
ObjectInputStream in = new
ObjectInputStream(socket.getInputStream());
Object o = in.readObject();
System.out.println("Received: "+o);
((HitMe)o).hit();
}
public static void main(String argv[]) throws Exception {
new TestRMIClient().hit();
}
The HitMe interface extends Remote and only has the hit() method. I
wrote an implementation for it that just displays a message.
The server does mostly the same thing, sends the object to the
client. The relevant lines are:
// in the constructor; MyHitMe extends UnicastRemoteObject
// and implements the HitMe interface.
hitme = new MyHitMe();
// in response to a connection from a client
out.writeObject( hitme );
Here's the output I get from the client:
Received: MyHitMe[RemoteStub [ref:
[endpoint:[<my-ip-here>:46846](local),objID:[0]]]]
I'm hit!
This shows that the passed object was (as I was expecting) not of the
implementation type (MyHitMe) but instead of the stub type generated
by
rmic (RemoteStub). Now, I'm expecting the stub to forward method calls
to the server process, so then the "I'm hit" message should be
displayed
at the server.
But it's not. The message gets displayed on the client, and I don't
understand why.
Does anyone here understand what's happening? Will I have to use the
RMI Registry even though I have no need for discovery since my app
already runs at a well-known location on a well-known port?
Thanks in advance,
JP
(please reply to the thread since I don't check that email often)