又是rmi.
给你个简单的示例代码
http://www.flashman.com.cn/UploadFiles/2004-10-12/20041012171027246.rar
给你个简单的示例代码
http://www.flashman.com.cn/UploadFiles/2004-10-12/20041012171027246.rar
解决方案 »
- socket获对等点的信息
- Tomcat布置时出的红色字的问题!
- 初学者,Myeclipse中加载的Struts Validator校验器的问题。
- 新手求助,jboss4简单问题!
- 有力出力,无力顶一下,捧场的俺就给分。谢谢!
- 誰有jsp 访问sql server 的各种例程?使用microsoft driver for jdbc
- 我就不相信CSDN上就我一人碰见这种该死的问题(UP 有分)
- ●[200分]在CMP中如何利用bean类中的方法ejbRemove()、利用home接口中的remove(Handle)和remove(Object)来删除数据库中相应的记录??
- Java(TM) 2 Runtime Environment和java虚拟机一样吗?
- 又送分
- 请问在JBuilder2005中使用JBoss还需要插件吗
- struts action class的设计(请大家多提意见,UP不要)
下不了。
编译上述代码: javac (path)\*.java
rmic -v1.2 ChatViewer
rmic -v1.2 ServerForChat接着我们启动服务器:
java ServerForChat [port]
是什么意思啊?
如: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不过这个程序错误很多啊
rmic -v1.2 ServerForChat
是为了生成桩文件等
接着你需要在服务器端执行 start rmiregistry 1099 启动远程注册表
然后启动服务器端即可
还有一般情况你需要在服务器端创建安全策略文件.java.policy,给予所有的许可
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);
}
}
首先先将所有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 ServerForChat
d:\rmitest>java ServerForChat 1099
然后在客户端运行
d:\rmiclient>java ClientForm ipaddress port
就可以了