新学rmi,无法通讯,不理解!!
接口::
package server;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface Chat_server extends Remote {
public void show(String message) throws RemoteException;
}package client;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface Chat_client extends Remote {
public void show(String message) throws RemoteException;
}
接口实现::
package server;import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import javax.swing.JButton;import javax.swing.JFrame;
import javax.swing.JTextArea;import client.Chat_client;public class Server extends UnicastRemoteObject implements Chat_server{ private JTextArea send;
private JTextArea show;
private JFrame frame;
private Chat_client client; /**
 * Launch the application
 * @param args
 */
public static void main(String args[]) {
try {
Server window = new Server();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
} /**
 * Create the application
 */
public Server() throws RemoteException{
initialize();
} /**
 * Initialize the contents of the frame
 */
private void initialize() {
frame = new JFrame();
frame.getContentPane().setBackground(new Color(128, 128, 255));
frame.getContentPane().setLayout(null);
frame.setBounds(100, 100, 301, 375);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final JButton button = new JButton();
button.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
try{
register();
}catch(Exception ee){
}
}
});
button.setText("注册");
button.setBounds(10, 10, 99, 23);
frame.getContentPane().add(button); show = new JTextArea();
show.setBounds(10, 50, 269, 128);
frame.getContentPane().add(show); send = new JTextArea();
send.setBounds(10, 214, 269, 88);
frame.getContentPane().add(send); final JButton button_1 = new JButton();
button_1.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
try{
send();
}catch(Exception eee){
}
}
});
button_1.setText("发送");
button_1.setBounds(10, 308, 99, 23);
frame.getContentPane().add(button_1);
}

public void send(){
String message=send.getText();
try{
show.append("server:"+message+"\n");
client.show(message);
}catch(Exception e){

}
}
public void show(String message) throws RemoteException{
show.append(message);
}
public void register()throws Exception{
// System.setSecurityManager(new RMISecurityManager());
Server server=new Server();
Naming.rebind("Chat_server", server);
show.append("ready to do\n");
}}
package client;import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import javax.swing.JButton;import javax.swing.JFrame;
import javax.swing.JTextArea;import server.Chat_server;public class Client extends UnicastRemoteObject implements Chat_client{ private JTextArea send;
private JTextArea show;
private JFrame frame;
private Chat_server server; /**
 * Launch the application
 * @param args
 */
public static void main(String args[]) {
try {
Client window = new Client();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
} /**
 * Create the application
 */
public Client() throws RemoteException{
initialize();
} /**
 * Initialize the contents of the frame
 */
private void initialize() {
frame = new JFrame();
frame.getContentPane().setBackground(new Color(128, 128, 255));
frame.getContentPane().setLayout(null);
frame.setBounds(100, 100, 317, 375);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final JButton button = new JButton();
button.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
try{
connect();
}catch(Exception ee){

}
}
});
button.setText("连接");
button.setBounds(10, 12, 99, 23);
frame.getContentPane().add(button); final JButton button_1 = new JButton();
button_1.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
try{
send();
}catch(Exception eee){

}
}
});
button_1.setText("发送");
button_1.setBounds(10, 291, 99, 23);
frame.getContentPane().add(button_1); show = new JTextArea();
show.setBounds(10, 41, 281, 92);
frame.getContentPane().add(show); send = new JTextArea();
send.setBounds(10, 206, 281, 79);
frame.getContentPane().add(send);
}

public void send(){
String message=send.getText();
try{
show.append("client:"+message+"\n");
    server.show(message);
}catch(Exception e){

}
}
public void show(String message) throws RemoteException{
show.append(message);
}
public void connect() throws Exception{
// System.setSecurityManager(new RMISecurityManager());
server=(Chat_server)Naming.lookup("Chat_server");
show.append("successfull\n");
server.show("hello");
}}

解决方案 »

  1.   

    在Naming.rebind("Chat_server", server)前面加上LocateRegistry.createRegistry(1099);
    就不用再使用rmi命令启动了
      

  2.   

    我是使用 eclipse的rmi控制的,所有的rmi启动都是正常的。应该就是程序本身有问题!!
      

  3.   

    stub部署了,做java的这些都是要写异常的阿!!看看我的程序把!!我的程序有问题的!
      

  4.   

    我用你的代码Naming.rebind没有执行
    可能咱俩环境不一样
      

  5.   

    看看我的程序吧!!看看我的程序对不??我刚学rmi所以还不知道对不对
      

  6.   

    我觉得服务器实现类里的private Chat_client client;这是一个客户端的远程对象,而在客户端没有rebind它,所以在服务器里是不能调用的,即client.show(message);是有问题的。这个程序应该是服务器也是一个客户端,客户端也是一个服务器。