Contents ...
udn網路城邦
聊天室-channl_1
2013/05/10 13:18
瀏覽260
迴響0
推薦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);

                }

        }

    }

}
全站分類:心情隨筆 心情日記
自訂分類:程式
下一則: 聊天室-大廳
你可能會有興趣的文章:
發表迴響

會員登入