| RMI编程实例 |
|
| 作者是 Administrator | |
| 2008-04-23 07:45:05 | |
|
//客户端接口: import java.net.InetAddress; import java.rmi.Remote; import java.rmi.RemoteException; public interface IChatClient extends Remote{ public InetAddress getInetAddress()throws RemoteException; public void showMessage(InetAddress Ip,String message)throws RemoteException; } //客户端 import java.awt.*; import java.net.*; import java.rmi.*; import javax.swing.*; public class ChatClient extends UnicastRemoteObject implements IChatClient{ private String name=null; private JFrame clientFrame=null; private Container con=null; private JScrollPane scroll=null; private JTextArea showArea=null; private JTextArea sendArea=null; private JLabel label=null; private JScrollBar sb=null; private JButton sendbut=null; private static IChatServer server=null; protected ChatClient() throws RemoteException { super(); try { this.name=""+InetAddress.getLocalHost(); } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); } clientFrame=new JFrame("聊天室----欢迎"+name+"!"); con=clientFrame.getContentPane(); con.setLayout(new BoxLayout(con,BoxLayout.Y_AXIS)); showArea=new JTextArea(10,40); scroll=new JScrollPane(showArea); showArea.setEditable(false); showArea.setLineWrap(true); label=new JLabel("发送信息栏"); sendArea=new JTextArea(5,40); sendArea.setLineWrap(true); con.add(scroll); JPanel panel1=new JPanel(); panel1.add(label); con.add(panel1); con.add(sendArea); ButEvent butevent=new ButEvent(); sendbut=new JButton("发送"); sendbut.addActionListener(butevent); JPanel panel2=new JPanel(); panel2.add(sendbut); con.add(panel2); WinEvent winevent=new WinEvent(); clientFrame.pack(); clientFrame.addWindowListener(winevent); clientFrame.setLocation(500,350); clientFrame.setVisible(true); clientFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setServer(); sb=scroll.getVerticalScrollBar(); } public class WinEvent extends WindowAdapter{ public void windowClosed(WindowEvent e) { try { server.removeChatUser(InetAddress.getLocalHost()); } catch (RemoteException e1) { // TODO Auto-generated catch block System.exit(0); } catch (UnknownHostException e1) { // TODO Auto-generated catch block System.exit(0); } } public void windowClosing(WindowEvent e) { try { if(server!=null) server.removeChatUser(InetAddress.getLocalHost()); else System.exit(0); } catch (RemoteException e1) { // TODO Auto-generated catch block System.exit(0); } catch (UnknownHostException e1) { // TODO Auto-generated catch block System.exit(0); } } } public class ButEvent implements ActionListener{ public void actionPerformed(ActionEvent e) { if(e.getSource().equals(sendbut)){ try { if(sendArea.getText().trim().equals("")){ JOptionPane.showMessageDialog(clientFrame, "不能发送空信息!"); } else{ //System.out.println(InetAddress.getLocalHost()+" said :"+sendArea.getText()); server.castMessage(InetAddress.getLocalHost(), sendArea.getText()); sendArea.setText(""); } } catch (RemoteException e1) { e1.printStackTrace(); } catch (UnknownHostException e1) { e1.printStackTrace(); } } } } public void setServer(){ try { server=(IChatServer) Naming.lookup("//192.168.152.54:9000/chatServer"); } catch (MalformedURLException e) { showArea.setText("连接报务器起时!"); } catch (RemoteException e) { showArea.setText("连接报务器起时!"); } catch (NotBoundException e) { showArea.setText("连接报务器起时!"); } } public InetAddress getInetAddress() throws RemoteException { return null; } public void showMessage(InetAddress Ip, String message)throws RemoteException { System.out.println(Ip+" said :"+message); StringBuffer show=new StringBuffer(showArea.getText().trim()); show.append("\n"); show.append(Ip+" said :"+message); showArea.setText(show.toString()); clientFrame.repaint(); sb.setValue(sb.getMaximum()); } public static void main(String[] args) { try { ChatClient client=new ChatClient(); LocateRegistry.createRegistry(9999); Naming.rebind("//localhost:9999/chatClient",client); try { server.addChatUser(InetAddress.getLocalHost()); } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (RemoteException e) { e.printStackTrace(); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } //服务器端接口 import java.net.InetAddress; import java.rmi.Remote; import java.rmi.RemoteException; public interface IChatServer extends Remote{ public void addChatUser(InetAddress Ip) throws RemoteException; public void removeChatUser(InetAddress Ip) throws RemoteException; public void castMessage(InetAddress Ip,String message) throws RemoteException;//向客户端广播. } //服务器端 import java.net.InetAddress; import java.net.MalformedURLException; import java.rmi.Naming; import java.rmi.NotBoundException; import java.rmi.RemoteException; import java.rmi.registry.LocateRegistry; import java.rmi.server.UnicastRemoteObject; import java.util.Vector; public class ChatServer extends UnicastRemoteObject implements IChatServer{ private Vector chatMemeber=null; protected ChatServer() throws RemoteException { super(); chatMemeber=new Vector(); } public IChatClient getClient(InetAddress Ip){ try { System.out.println(Ip); return (IChatClient) Naming.lookup("//"+Ip.getHostAddress()+":9999/chatClient"); } catch (MalformedURLException e) { e.printStackTrace(); } catch (RemoteException e) { e.printStackTrace(); } catch (NotBoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } public synchronized void addChatUser(InetAddress Ip) throws RemoteException { chatMemeber.add(Ip); String message="加入聊天室"; for(int i=0;i<chatMemeber.size();i++) { IChatClient client=getClient((InetAddress) chatMemeber.get(i)); System.out.println("ss"+": +"+i+" :"+client); client.showMessage(Ip, message); } } public synchronized void castMessage(InetAddress Ip, String message)throws RemoteException { System.out.println(Ip+" said :"+message); for(int i=0;i<chatMemeber.size();i++) { IChatClient client=getClient((InetAddress) chatMemeber.get(i)); System.out.println("chatMemeber:"+chatMemeber.get(i)); client.showMessage(Ip, message); } } public synchronized void removeChatUser(InetAddress Ip) throws RemoteException { chatMemeber.remove(Ip); String message="离开聊天室"; for(int i=0;i<chatMemeber.size();i++) { IChatClient client=getClient((InetAddress) chatMemeber.get(i)); client.showMessage(Ip, message); } } public static void main(String[] args) { try { ChatServer server=new ChatServer(); LocateRegistry.createRegistry(9000); Naming.rebind("//localhost:9000/chatServer",server); } catch (RemoteException e) { e.printStackTrace(); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } |
|
| 最近更新 ( 2008-04-23 07:45:05 ) |

