serverforchat.java

来自「简单的聊天系统chat,用java编写」· Java 代码 · 共 59 行

JAVA
59
字号
\\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);  
            }  
      }   

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?