ServerPoint.java
上传用户:qaz007_wsx
上传日期:2022-06-21
资源大小:3k
文件大小:4k
源码类别:

ICQ/即时通讯

开发平台:

Java

  1. //ServerPoint.java
  2. import   java.net.*;   
  3.   import   java.io.*;   
  4.     
  5. public   class   ServerPoint   extends   Thread{   
  6.       static   int   SERVERPORT=8088;  //端口  
  7.       private   Socket   client;   
  8.       public   static   int   i=0;   
  9.       public   static   String[]   vct=new   String[10];       
  10.       public   ServerPoint(){}   //构造方法
  11.       public   void   run(){   
  12.       ClientThread.log("服务器端程序启动...");   
  13.           try{   
  14.             ServerSocket   server=new   ServerSocket(SERVERPORT);   
  15.             while(true){             
  16.                   client=server.accept();                   
  17.                   ClientThread   ct=new   ClientThread(client);   
  18.                 ct.start();   
  19.             }   
  20.         }catch(Exception   e){   
  21.             e.printStackTrace();   
  22.             ClientThread.log("服务器端程序关闭。。。");   
  23.             System.exit(0);   
  24.         }   
  25.       }   
  26.       public   static   void   main(String[]   args){   
  27.           ServerPoint   sp=new   ServerPoint();   
  28.           sp.start();   
  29.       } //主函数  
  30.       public   static   synchronized   void   message(String   msg){   
  31.         vct[i]=msg;   
  32.       i=(i+1)%10;   
  33.       vct[i]="*";   
  34.         
  35.       }   
  36.   }   
  37.   class   ClientThread   extends   Thread{       
  38.   private   static   int   ii=0;   
  39.       private   Socket   s;   
  40.       String   msg=null;   
  41.       ServerSocket   serverSocket=null;   
  42.       Socket   socket=null;   
  43.       BufferedReader   cin=null;   
  44.       PrintWriter   cout=null;       
  45.       public   ClientThread(Socket   s){           
  46.           this.s=s;         
  47.       }   
  48.       public   void   run(){   
  49.           try{           
  50.               cin=new   BufferedReader(   
  51.                new   InputStreamReader(s.getInputStream()));   
  52.               cout=new   PrintWriter(s.getOutputStream());   
  53.               SendToAll   sta=new   SendToAll();   
  54.               sta.start();                   
  55.               msg=cin.readLine();   
  56.               ii++;   
  57.               System.out.println("some   connect   us!聊天室里总共有"+ii+"个人");   //连接登录上的客户数
  58.               while(!msg.equals("exit")){   
  59.               ServerPoint.message(msg);   
  60.               msg=cin.readLine();   
  61.               }   
  62.              if(msg.equals("exit"))   
  63.               {   --ii;   
  64.               System.out.print("someone   exitn聊天室里总共有"+ii+"个人"); 
  65.            //有用户退出后的聊天人数   
  66.             cin.close();   
  67.               cout.close();   
  68.               s.close();               
  69.               }           
  70.           }catch(Exception   e){   
  71.               e.printStackTrace();   
  72.           }   
  73.       }   
  74.       static   void   log(String   strInfo){   
  75.           System.out.println(strInfo);   
  76.            }   
  77.   class   SendToAll   extends   Thread{   
  78.          private   int   j=-1;   
  79.   public   void   run(){   
  80.      while(true)   
  81.       {   
  82.        try{   
  83.              sleep(500);   
  84.                    if(j==-1){   
  85.                          if(!ServerPoint.vct[0].equals("*")){   
  86.                                cout.println(ServerPoint.vct[0]);   
  87.   cout.flush();   
  88.                                j=1;   
  89.               }   
  90.               else   {   
  91.                 cout.println(ServerPoint.vct[1]);   
  92.                 cout.flush();   
  93.                 j=2;   
  94.               }   
  95.     }   
  96.   while(!ServerPoint.vct[j].equals("*"))   
  97.   {   
  98.     cout.println(ServerPoint.vct[j]);   
  99.   cout.flush();   
  100.   j=(j+1)%10;   
  101.      }   
  102.   }catch(Exception   e){}   
  103.   }   
  104.   }   
  105.   }   
  106.   }