字體:小 中 大 | |
|
|
2013/05/10 13:18:39瀏覽211|回應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 ch1 extends Thread {
static Vector ch1 = new Vector( 10 );
private Socket socket;
private BufferedReader in;
private PrintWriter out;
public ch1(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 line2="";
synchronized(ch1) {
ch1.addElement(this);}
// add() not found in Vector class
try
{
while(!(line2 = in.readLine()).equalsIgnoreCase("/back"))
{
if(line2.equalsIgnoreCase("/ch2"))//聊天室二
{
synchronized(ch1)
{
ch1.removeElement(this);
}
ch2 channl2=new ch2(socket);
channl2.run();
break;
}
for(int i = 0; i < ch1.size(); i++)
{
synchronized(ch1) //同步化
{
ch1 chat1 =(ch1)ch1.elementAt(i);
if(chat1!=this)
{
chat1.out.println(line2 + "");
chat1.out.flush();
}
}
}
}
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
finally
{
synchronized(ch1)
{
ch1.removeElement(this);
}
}
}
}
|
|
( 心情隨筆|心情日記 ) |