修改了一下,应该可以运行了,先贴代码,个别有中文乱码自己改改把,不影响运行。
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.java
import 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.java
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface ChatViewerInterface extends Remote
{
void appendChatContent(String msg) throws RemoteException;
}
4、ChatViewer.java
import 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);
}
}
再说运行:
首先先将所有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
还有个端口和远程访问的问题
此程序默认端口为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改为服务器地址就可以了。
按照d:/rmitest>rmic -v1.2 ChatViewer
d:/rmitest>rmic -v1.2 ServerForChat
d:/rmitest>java ServerForChat 1099
然后在客户端运行
d:/rmiclient>java ClientForm ipaddress port
就可以了
先将所有的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