字體:小 中 大 | |
|
|
2013/05/10 13:23:18瀏覽187|回應0|推薦0 | |
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package chatserver;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Vector;
/**
*
* @author YUKI
*/
public class ch2 extends Thread {
static Vector ch2 = new Vector( 10 );
private Socket socket;
private BufferedReader in;
private PrintWriter out;
public ch2(Socket socket) throws IOException {
this.socket = socket;
in = new BufferedReader(new InputStreamReader(socket.getInputStream(),"BIG5"));
out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream(),"BIG5"));
}
public void run() {
String line3="";
synchronized(ch2) {
ch2.addElement(this);}
// add() not found in Vector class
try
{
while(!(line3 = in.readLine()).equalsIgnoreCase("/back"))
{
if(line3.equalsIgnoreCase("/ch1"))//聊天室一
{
synchronized(ch2)
{
ch2.removeElement(this);
}
ch1 channl1=new ch1(socket);
channl1.run();
break;
}
for(int i = 0; i < ch2.size(); i++)
{
synchronized(ch2) //同步化
{
ch2 chat2 =(ch2)ch2.elementAt(i);
if(chat2!=this)
{
chat2.out.println(line3 + "");
chat2.out.flush();
}
}
}
}
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
finally
{
synchronized(ch2)
{
ch2.removeElement(this);
}
}
}
}
|
|
( 心情隨筆|心情日記 ) |