1

我正在使用 RMI 创建一个简单的聊天应用程序,但我无法识别出确切的问题在哪里?这是我的代码:

聊天服务器.java

import java.awt.Point;
import java.net.MalformedURLException;
import java.rmi.AlreadyBoundException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author nick
 */
public class ChatServer extends UnicastRemoteObject implements IChat{

    ArrayList<String> list = new ArrayList<String>();

    public ChatServer() throws RemoteException{
        try {
            Naming.bind("//localhost:1099/chat", this);
        } catch (Exception ex) {

            ex.printStackTrace();

        }

        System.out.println("Chat Server Ready");
    }




    public void put(Point p) throws RemoteException{

        p.getX();
        p.getY();

    }
 /*
    public ArrayList<String> get() throws RemoteException{

        return list;

    } */
    public ArrayList<String> get() throws RemoteException
    {
        return list;
    }

    public static void main(String[] args) {
        try {
            new ChatServer();
        } catch (RemoteException ex) {
            Logger.getLogger(ChatServer.class.getName()).log(Level.SEVERE, null, ex);
        }
    }


}

聊天客户端.java

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.rmi.Naming;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author nick
 */
public class ChatClient extends JFrame implements Runnable, ActionListener, MouseListener, MouseMotionListener {

    String TAG = "kushal ";
    IChat ic;
    int oldx, oldy;
    JTextField jtf;
    JTextArea jta;

    public ChatClient() {
        try {
            Remote remote = Naming.lookup("//localhost:1099/chat");
            ic = (IChat) remote;
        } catch (Exception ex) {
            ex.printStackTrace();
        }



        setSize(300, 300);
        setVisible(true);

        Thread t = new Thread(this);
        t.start();
    }

    public void paint(Graphics g) {
    }

    public void run() {
        while (true) {
            try {
                ArrayList<Point> list = ic.get();
                Thread.sleep(1000);
            } catch (Exception ex) {
                ex.printStackTrace();

            }

        }
    }

    public static void main(String[] args) {
        new ChatClient();
    }

    public void actionPerformed(ActionEvent e) {
      /*  try {
            ic.put(TAG + jtf.getText());
            jtf.setText("");
        } catch (RemoteException ex) {
            Logger.getLogger(ChatClient.class.getName()).log(Level.SEVERE, null, ex);
        }*/

    }

    public void mouseClicked(MouseEvent e) {
    }

    public void mousePressed(MouseEvent e) {
        oldx = e.getX();
        oldy = e.getY();


    }

    public void mouseReleased(MouseEvent e) {
    }

    public void mouseEntered(MouseEvent e) {
    }

    public void mouseExited(MouseEvent e) {
    }

    public void mouseDragged(MouseEvent e) {
        Graphics g = getGraphics();

        int x = e.getX();
        int y = e.getY();
        g.drawLine(oldx, oldy, x, y);
        oldx = x;
        oldy = y;


    }

    public void mouseMoved(MouseEvent e) {
    }
}

IChat.java

import java.awt.Point;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.util.ArrayList;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author new
 */
public interface  IChat extends Remote{

    public void put(Point p) throws RemoteException;
    public ArrayList<Point> get() throws RemoteException;
}

ChatServer.java 中的错误:ChatServer 不是抽象的,并且不会覆盖 IChat 中的抽象方法 get()

ChatServer 中的 get() 无法实现 IChat 中的 get() 返回类型 ArrayList 与 ArrayList 不兼容 ----

任何帮助都会非常有用。

提前致谢。

4

2 回答 2

1

-尚未为. _ Implementation _Interface

例如:

    IChatImpl implements IChat{

    public void put(Point p) throws RemoteException{



    }

    public ArrayList<Point> get() throws RemoteException{




    }
}

///////////////////////////编辑部分////////////// /////////////

我认为你没有覆盖这个方法......

public ArrayList<String>

它应该是....

public ArrayList<Point>

于 2012-11-30T07:31:15.397 回答
0

这里还有其他问题,但是出现connect异常的原因是你没有在服务器主机上启动RMI Registry。

于 2012-11-30T11:31:24.897 回答