又是rmi.
给你个简单的示例代码
http://www.flashman.com.cn/UploadFiles/2004-10-12/20041012171027246.rar

解决方案 »

  1.   

    谢谢阿,我有好几份源代码了。可是就是不会编译阿。家里的是jbuilder.说要有个java source perproteis 的选项。我根本找不到。有的网站说是用在dos下运行rmi...什么的,然后出了两个文件,但是具体的还是不懂啊。等我一会把代码帖上来。还有,http://www.flashman.com.cn/UploadFiles/2004-10-12/20041012171027246.rar
    下不了。
      

  2.   

    http://blog.csdn.net/zhangleibbq/archive/2004/01/26/21472.aspx我用的是这个代码。alpha15(奥法)  & jeffzhu(狐狸GG) 我快晕死了。帮我看看成吗?
    编译上述代码: javac (path)\*.java
     rmic -v1.2 ChatViewer
     rmic -v1.2 ServerForChat接着我们启动服务器:
     
     java ServerForChat [port]
    是什么意思啊?
      

  3.   

    先将所有的java程序考在一个目录下
    如:d:\rmitest然后进入dos下键入以下命令:
    d:\rmitest>javac *.java
    d:\rmitest>rmic -v1.2 ChatViewer
    d:\rmitest>rmic -v1.2 ServerForChat
    d:\rmitest>java ServerForChat 1099
    d:\rmitest>java ClientForm不过这个程序错误很多啊
      

  4.   

    rmic -v1.2 ChatViewer
     rmic -v1.2 ServerForChat
    是为了生成桩文件等
    接着你需要在服务器端执行  start rmiregistry 1099 启动远程注册表
    然后启动服务器端即可
    还有一般情况你需要在服务器端创建安全策略文件.java.policy,给予所有的许可
      

  5.   

    修改了一下,应该可以运行了,先贴代码,个别有中文乱码自己改改把,不影响运行。
    1、ServerForChatInterface.java import java.rmi.Remote;
     import java.rmi.RemoteException; public interface ServerForChatInterface extends Remote
     {
        void regist(ChatViewerInterface client) throws RemoteException;
        void chat(String msg) throws RemoteException;
      }2、ServerForChat.javaimport java.rmi.*;
     import java.rmi.server.*;
     import java.util.Vector;
     import java.rmi.registry.*; public class ServerForChat extends UnicastRemoteObject implements ServerForChatInterface
     {
        private static Vector clients = new Vector();
        private int port = 1099;
        public ServerForChat() throws RemoteException
        {
     super();
         }
        public ServerForChat(int port) throws RemoteException
        {
     super(port);
     this.port = port;
         }
        public void regist(ChatViewerInterface client)
        {
     if(clients.contains(client)==false)
     {
        clients.add(client);
      }
         }
        public void chat(String msg)
        {
     for(int i = 0;i<clients.size();i++)
     {
        try{
      ((ChatViewerInterface)(clients.elementAt(i))).appendChatContent(msg);
            }catch(Exception e){
            clients.remove(i);
            continue;
           }
      }
         }
        public static void main(String args[])
        {
     int port = 1099;
     if(args!=null&&args.length==1)
     {
        try{
      port = Integer.parseInt(args[0]);
            }catch(Exception e){}
      }
     try{
          LocateRegistry.createRegistry(port);
          ServerForChat server = new ServerForChat(port);
          Naming.rebind("rmi://127.0.0.1:"+port+"/ChatServer",server);
         }catch(Exception e){
        System.out.println("start sever fail...");
        System.exit(0);
        }
     System.out.println("server started on port: "+port);
         }
      }3、ChatViewerInterface.javaimport java.rmi.Remote;
    import java.rmi.RemoteException;public interface ChatViewerInterface extends Remote
    {
        void appendChatContent(String msg) throws RemoteException;
     }4、ChatViewer.javaimport java.rmi.*;
    import java.rmi.server.*;
    import javax.swing.*;
    import java.io.*;
    import javax.swing.text.*;
    import javax.swing.text.html.*;
    import javax.swing.event.*;
    import java.text.*;
    import java.awt.*;public class ChatViewer extends JComponent implements ChatViewerInterface,Serializable
    {
        JScrollPane scrollpane;
        JTextPane viewer;
        public ChatViewer()
        {
     this.initializedComponent();
         }
        public ChatViewer(String inimsg)
        {
     this.initializedComponent();
     this.viewer.setText(inimsg);
         }
        private void initializedComponent()
        {
        
     this.viewer = new JTextPane();
     this.viewer.setContentType("text/html;charset=gb2312");
     this.viewer.setEditable(false);
     this.viewer.addHyperlinkListener(new LinkListener()); 
     this.scrollpane = new JScrollPane(this.viewer);
     this.setLayout(new BorderLayout());
     this.add(this.scrollpane,BorderLayout.CENTER);
     this.setVisible(true);
     
         }
        public void appendChatContent(String msg)
        {
     HTMLEditorKit kit = (HTMLEditorKit)(this.viewer.getEditorKit());
     Document doc = this.viewer.getDocument();
     StringReader reader = new StringReader(msg);
     try{
      kit.read(reader,doc,doc.getLength());    
      }catch(Exception e){System.out.println("chat content \""+msg+"\" lost..");} if(this.viewer.getSelectedText()==null||this.viewer.getSelectedText().trim().length()==0)
     {   
      this.viewer.select(this.viewer.getText().length(),this.viewer.getText().length());
      }
         }
        public void sendToServer()
        {
     try{
         UnicastRemoteObject.exportObject((ChatViewerInterface)this);
         }catch(Exception e){
               System.out.println("send object to server error: "+e.getMessage());
               }
         }
    class LinkListener implements HyperlinkListener
    {
        public void hyperlinkUpdate(HyperlinkEvent e)
        {       
          if(e.getEventType() == HyperlinkEvent.EventType.ACTIVATED)
          {          
     if(e instanceof HTMLFrameHyperlinkEvent)
     { 
       HTMLFrameHyperlinkEvent evt = (HTMLFrameHyperlinkEvent)e;
       HTMLDocument doc = (HTMLDocument)(viewer.getDocument()); 
       doc.processHTMLFrameHyperlinkEvent(evt);
     }
     else
     { 
        try {
        Runtime.getRuntime().exec("explorer "+e.getURL());              
       }
       catch(Exception ioe)  {
         //MessageDialog.showMessage(null,INFORMATION,UNSUPPORTED_BROWSER);
       }
     } 
          } 
        } 
     } 
    }5、ClientForm.java
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.rmi.*;
    import java.rmi.server.*;public class ClientForm extends JFrame
    {
        private ChatViewer chat;
        private JTextField msgeditor;
        private JButton msgsender;
        private Container contentpane;
        private JPanel panel;
        private ServerForChatInterface server;    public ClientForm(String serveraddress,int port)
        {
     super("简易聊天室"); 
     System.out.println("connect to server...");
     try{
          this.server = (ServerForChatInterface)Naming.lookup("rmi://"+serveraddress+":"+port+"/ChatServer");
         }catch(Exception e){
        System.out.println("不能连接到服务器.");
        System.exit(0);
        }
     this.initializedComponent();
     this.fireEvent();
     this.registChatViewer();
         }
        private void initializedComponent()
        {
     this.chat = new ChatViewer("<font color='blue'>欢迎进入聊天室</font>");
     this.msgeditor = new JTextField();
     this.msgsender = new JButton("发送");
     this.panel = new JPanel();
     this.panel.setLayout(new BorderLayout());
     this.panel.add(this.msgeditor,BorderLayout.CENTER);
     this.panel.add(this.msgsender,BorderLayout.EAST);
     this.contentpane = this.getContentPane();
     this.contentpane.setLayout(new BorderLayout());
     this.contentpane.add(this.chat,BorderLayout.CENTER);
     this.contentpane.add(this.panel,BorderLayout.SOUTH);
     this.setSize(600,400);
     this.setVisible(true);
         }
        private void fireEvent()
        {
            MessageSender ms = new MessageSender();
     this.msgeditor.addActionListener(ms);
     this.msgsender.addActionListener(ms);
     this.msgeditor.grabFocus();
         }
        private void chat()
        {
     String msg = this.msgeditor.getText();
     this.msgeditor.setText("");
     try{
         this.server.chat(msg);
         }catch(Exception e){
        this.chat.appendChatContent("<font color='red'><b>发送消息失败,请检查网络.</b></font>");
        }
         }
        private void registChatViewer()
        {
     try{
         UnicastRemoteObject.exportObject((ChatViewerInterface)(this.chat));
         this.server.regist((ChatViewerInterface)(this.chat));
         }catch(Exception e){
        this.chat.appendChatContent("<font color='red'><b>连接服务器失败,请检查网络.</b></font>");
        }
         }
        class MessageSender implements ActionListener
        {
     public void actionPerformed(ActionEvent e)
     {
         chat();
      }
         }
        public static void main(String args[])
        {
           String serveraddress = "127.0.0.1";
           int port = 1099;
           if(args!=null&&args.length==2)
           {
              serveraddress = args[0];
       try{
            port = Integer.parseInt(args[1]);
           }catch(Exception e){}
            }
           ClientForm client = new ClientForm(serveraddress,port);
         }
     }
      

  6.   

    再说运行:
    首先先将所有java程序放在一个目录下
    如:
    d:\rmitest然后进入cmd,并到d:\rmitest目录下,然后运行:
    d:\rmitest\javac *.java
    d:\rmitest>rmic -v1.2 ChatViewer
    d:\rmitest>rmic -v1.2 ServerForChat
    d:\rmitest>java ServerForChat 1099
    这时服务端就可以运行了为了运行客户端,可以直接执行
    d:\rmitest>java ClientForm为了将服务和客户分开,或者想在另外的机器上运行,则将以下class拷贝到其他目录或者计算机中:
    ChatViewerInterface.class
    ServerForChatInterface.class
    ClientForm.class
    ClientForm$MessageSender.class
    ChatViewer.class
    ChatViewer$LinkListener.class
    ChatViewer_Stub.class
    ServerForChat_Stub.class例如拷入
    d:\rmiclient
    然后运行
    d:\rmiclient>java ClientForm
      

  7.   

    还有个端口和远程访问的问题
    此程序默认端口为1099
    所以
    d:\rmitest>java ServerForChat 1099

    d:\rmitest>java ServerForChat
    是等价的此时客户端:
    d:\rmiclient>java ClientForm

    d:\rmiclient>java ClientForm 127.0.0.1 1099
    也是等价的可以自己设定端口,如
    d:\rmitest>java ServerForChat 10001
    此时客户端需:
    d:\rmiclient>java ClientForm 127.0.0.1 10001如果是远程访问,只要将
    d:\rmiclient>java ClientForm 127.0.0.1 10001
    中的127.0.0.1改为服务器地址就可以了。
      

  8.   

    you are soooooooo kind. thanks a lot
      

  9.   

    是啊,我是按照d:\rmitest>rmic -v1.2 ChatViewer
    d:\rmitest>rmic -v1.2 ServerForChat
    d:\rmitest>java ServerForChat 1099
    然后在客户端运行
    d:\rmiclient>java ClientForm ipaddress port 
    就可以了