为了账号安全,请及时绑定邮箱和手机立即绑定

求大神帮忙!!!关于java问题

求大神帮忙!!!关于java问题

ZXJ03 2016-12-11 18:00:07
ChatHandler.java package pro4; import java.net.*; import java.io.*; import java.util.*; public class ChatHandler implements Runnable{     protected Socket socket;     protected ObjectInputStream dataIn;     protected ObjectOutputStream dataOut;     protected Thread listener;     protected static Vector handlers=new Vector();           private boolean keepListening=true;     public ChatHandler(Socket socket){         this.socket=socket;     }     public synchronized void start(){         if(listener==null){             try{                 dataIn=new ObjectInputStream(socket.getInputStream());                 dataOut=new ObjectOutputStream(socket.getOutputStream());                 listener=new Thread(this);                 listener.start();             }catch(IOException ioException){                 ioException.printStackTrace();             }         }     }     public synchronized void stop(){         if(listener!=null){             try{                 if(listener!=Thread.currentThread())                     listener.interrupt();                 listener=null;                 dataOut.close();                 socket.close();             }catch(IOException ignored){                               }         }     }     public void run(){         String message="";         try{             handlers.addElement(this);             while(keepListening){                 message=(String)dataIn.readObject();                 if(message.equals("DISCONNECT")){                     dataOut.writeObject(message);                     dataOut.flush();                     stopListening();                 }else                     broadcast(message);             }         }catch(ClassNotFoundException classNotFoundException){                       }catch(EOFException ignored){                       }catch(IOException ex){             if(listener==Thread.currentThread())                 ex.printStackTrace();         }finally{             handlers.removeElement(this);         }         try{             dataIn.close();         }catch(IOException ioException){             ioException.printStackTrace();         }         stop();     }     protected void broadcast(String message){         synchronized (handlers){             Enumeration enumer=handlers.elements();             while(enumer.hasMoreElements()){                 ChatHandler handler=(ChatHandler)enumer.nextElement();                 try{                     handler.dataOut.writeObject(message);                     handler.dataOut.flush();                 }catch(IOException ex){                     handler.stop();                 }                           }         }     }     public void stopListening(){         keepListening=false;     } } 可以解释一下run()方法和broadcast()方法吗?看不懂,,,
查看完整描述

目前暂无任何回答

  • 0 回答
  • 0 关注
  • 1374 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信