// 建立對應 client 的 輸出物件
socketWriter = new PrintWriter(clientSocket.getOutputStream(), true);
//將client相關資訊存起來
server.setClientOutputStreams( clientSocket.getInetAddress().getHostAddress(), socketWriter );
//先將每個client的outputStream和ip存起來放到ArrayList中 ArrayList詳細用法請看Library
private static ArrayList
clientOutputStreams = new ArrayList
public ArrayList
return clientOutputStreams;
}
public void setClientOutputStreams( String ip, PrintWriter w ){
clientOutputStreams.add( new createOutputStreamData(ip,w));
}
public class createOutputStreamData{
String clientIP;
PrintWriter writer;
public createOutputStreamData( String ip, PrintWriter w ){
clientIP = ip;
writer = w;
}
}
//從ArrayList取出之後 分別寫入到個別的outputstream
public void broadcast(char g2) throws IOException {
for( int i=0; i
getClientOutputStreams().get(i).writer.println(g2);
getClientOutputStreams().get(i).writer.flush();
}
}
沒有留言:
張貼留言