搜尋此網誌

2009年7月29日 星期三

J2SE-Broadcast

//記得 import java.util.ArrayList;
// 建立對應 client 的 輸出物件

socketWriter = new PrintWriter(clientSocket.getOutputStream(), true);
//將client相關資訊存起來
server.setClientOutputStreams( clientSocket.getInetAddress().getHostAddress(), socketWriter );
//先將每個client的outputStream和ip存起來放到ArrayList中 ArrayList詳細用法請看Library
private static ArrayList clientOutputStreams;
clientOutputStreams = new ArrayList();
public ArrayList getClientOutputStreams(){
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();
}
}

沒有留言:

張貼留言