ServerFrame.java
上传用户:hbgsjx
上传日期:2022-06-25
资源大小:3k
文件大小:8k
源码类别:

ICQ/即时通讯

开发平台:

Java

  1. import java.io.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.net.*;
  5. import javax.swing.*;
  6. import javax.swing.event.*;
  7. import javax.swing.border.*;
  8. //=================主框架=================================
  9. public class ServerFrame extends JFrame 
  10. {
  11. private ServerSocket serverSocket;
  12. private UserList userlist;
  13. private ServerThread serverThread;
  14. Node n;
  15.     public ServerFrame()
  16. {
  17. try
  18.                 {
  19.          serverSocket=new ServerSocket(8000,10);
  20.          System.out.println("server start at 8000 port......n");
  21.         
  22.         
  23.          userlist=new UserList();
  24.          while(true){
  25.     n=new Node();
  26.     n.usersocket=serverSocket.accept();
  27.     userlist.add(n);
  28.     serverThread=new ServerThread(n.usersocket,userlist,n);
  29.                         serverThread.start();
  30.          }
  31.                }catch(Exception e){}
  32. }//-----------------------ServerFrame(主框架)里的一些方法-------------------
  33.    
  34.     public class ServerThread extends Thread
  35.     {
  36.      Socket usersocket;
  37.      UserList userlist;
  38.      boolean k;
  39.         Node n;
  40.      public ServerThread(Socket usersocket,UserList userlist,Node n)
  41.      {
  42.      this.usersocket=usersocket;
  43.      this.n=n;
  44.      this.userlist=userlist;
  45.      }
  46.      public void run()
  47.      {
  48.      try
  49.      {
  50.      k=true;
  51.      n.output=new ObjectOutputStream(n.usersocket.getOutputStream());
  52.      n.output.flush();
  53.      n.input=new ObjectInputStream(n.usersocket.getInputStream());
  54.      n.username=(String)n.input.readObject();
  55.      System.out.println(n.username+"上线"+"n");
  56.      System.out.println("在线人数"+userlist.getCount()+"人");
  57.      String tosombody;
  58.      String message;
  59.      String msg;
  60.      while(this.k)
  61.      {
  62.       
  63.     
  64.           tosombody=(String)n.input.readObject();
  65.       message=(String)n.input.readObject();
  66.           msg=n.username+"对"+tosombody+"说:"+message+"n";
  67.           if(tosombody.equalsIgnoreCase("closed"))
  68.           {
  69.           k=false;
  70.           Node node=userlist.find(n.username);
  71.           node.input.close();
  72.           node.output.close();
  73.           node.usersocket.close();
  74.           System.out.println(n.username+"已下线");
  75.           userlist.del(n);
  76.           System.out.println("在线人数"+userlist.getCount()+"人");
  77.           }
  78.           else
  79.           {
  80.           
  81.           if(tosombody.equalsIgnoreCase("all"))
  82.                        {
  83.                 int count=userlist.getCount();
  84.                              int i=0;
  85.                              while(i<count)
  86.                              {
  87.                        Node node=userlist.find(i);
  88.                        if(node!=null)
  89.                        {
  90.                    try
  91.                        {
  92.                        node.output.writeObject("list");
  93.                                                         node.output.flush();
  94.                                                         int j=0;
  95.                                                         String ss="";
  96.                                                         while(j<count)
  97.                                                         {
  98.                                                           Node nn=userlist.find(j);
  99.                                                           if(nn==null)
  100.                                                           {
  101.                                                              j++;continue;
  102.                                                           } 
  103.                                                            ss+=nn.username;
  104.                                                            ss+="n";
  105.                                                            j++;
  106.                                                         }
  107.                                                         node.output.writeObject(ss);
  108.                                                         node.output.writeObject("message");
  109.                                                         node.output.flush();
  110.                                                         node.output.writeObject(msg);
  111.                        node.output.flush();
  112.                        }catch(Exception e){}
  113.                    i++;
  114.                         }
  115.                   
  116.              }
  117.          
  118.                     }
  119.                         else
  120.                         {
  121.                     Node node=userlist.find(tosombody);
  122.                           if(node!=null)
  123.                           {
  124.                      node.output.writeObject("list");
  125.                                                   node.output.flush();
  126.                                                   int j=0;
  127.                                                   String ss=""; 
  128.                                                   int count=userlist.getCount();
  129.                                                   while(j<count)
  130.                                                   {
  131.                                                     Node nn=userlist.find(j);
  132.                                                     if(nn==null)
  133.                                                     {
  134.                                                        j++;continue;
  135.                                                     }  
  136.                                                     ss+=nn.username;
  137.                                                     ss+="n";
  138.                                                     j++;
  139.                                                   }
  140.                                                   node.output.writeObject(ss);
  141.                                                   node.output.writeObject("message");
  142.                                                   node.output.flush();
  143.                                                   node.output.writeObject(msg);
  144.                          }
  145.                         }
  146.           }
  147.           
  148.         
  149.      }
  150.     
  151.      }catch(Exception e){}
  152.      }
  153.         }
  154.     public static void main(String args[])
  155.     {
  156.      new ServerFrame();
  157.     }
  158. }
  159. //-------------------方法结束--------------------------------
  160. //====================主框架结束=========================================
  161. //======================构造客户类,以及编写一些方法===================
  162. class Node
  163. {
  164. String username;
  165. Socket usersocket;
  166. ObjectOutputStream output;
  167. ObjectInputStream input;
  168. Node next=null;
  169. }
  170. class UserList
  171. {
  172. Node root;
  173. Node pointer;
  174. int count;
  175. UserList()
  176. {
  177. root=new Node();
  178. root.next=null;
  179. pointer=null;
  180. count=0;
  181. }
  182.     void add(Node n)
  183.     {
  184.      pointer=root;
  185.      while(pointer.next!=null)
  186.      {
  187.      pointer=pointer.next;
  188.      }
  189.      pointer.next=n;
  190.      n.next=null;
  191.      count++;
  192.     }
  193.     void del(Node n)
  194.     {
  195.      pointer=root;
  196.      while(pointer.next!=null)
  197.      {
  198.      if(pointer.next==n)
  199.      {
  200.      pointer.next=n.next;
  201.      count--;
  202.      break;
  203.      }
  204.      pointer=pointer.next;
  205.      }
  206.     }
  207.     int getCount()
  208.     {
  209.      return count;
  210.     }
  211.     Node find(String username)
  212.     {
  213.      if(count==0)return null;
  214.      pointer=root;
  215.      while(pointer.next!=null)
  216.      {
  217.      pointer=pointer.next;
  218.      if(pointer.username.equalsIgnoreCase(username))
  219.      {
  220.      return pointer;
  221.      }
  222.      }
  223.      return null;
  224.     }
  225.     Node find(int index)
  226.     {
  227.      if(count==0)return null;
  228.      if(index<0)return null;
  229.      pointer=root;
  230.      int i=0;
  231.      while(i<index+1)
  232.      {
  233.      if(pointer.next!=null)
  234.      {
  235.      pointer=pointer.next;
  236.      }
  237.      else
  238.      {
  239.      return null;
  240.      }
  241.      i++;
  242.      }
  243.      return pointer;
  244.     }
  245. }//=====================客户类结束===============================