亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

求大神幫忙?。。£P于java問題

求大神幫忙?。。£P于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 關注
  • 1428 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號