⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cha01ser.java

📁 网络聊天室
💻 JAVA
字号:
/* * cha01ser.java * * Created on 2005年6月17日, 上午9:14 *//** * * @author  wlliu */import java.awt.*;import java.io.*;import java.net.*;import java.util.*;public class cha01ser {        ServerSocket server=null;    Socket you=null;    Hashtable peopleList;    static TextArea textArea1=null;    Frame frame1;    Label label1;    InetAddress LocalIp;    /** Creates a new instance of cha01ser */    public cha01ser() {        peopleList=new Hashtable();         frame1=new Frame();        frame1.setTitle("聊天室服务器");        label1 = new Label();        textArea1 = new TextArea();                try  {                 LocalIp=InetAddress.getLocalHost();              }               catch (IOException e)              {               }        frame1.setBackground(java.awt.Color.lightGray);        frame1.setLayout(null);        //frame1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());                textArea1.setBackground(new Color(255, 220, 255));        textArea1.setEditable(false);        textArea1.append("正在监听\n");        textArea1.select(65536,65536);        label1.setBackground(java.awt.Color.lightGray);        label1.setText("服务器: "+LocalIp+"  端口号:8888");        frame1.add(label1);        label1.setBounds(30, 40, 310, 22);                frame1.add(textArea1);        textArea1.setBounds(30, 80, 320, 180);                frame1.pack();        frame1.setBounds(0,0,400,300);        //frame1.resize(400,300);        frame1.show();        frame1.addWindowListener(new java.awt.event.WindowAdapter() {                public void windowClosing(java.awt.event.WindowEvent evt) {                    exitForm(evt);                }            });        try        {             server=new ServerSocket(8888);                            while(true)             {                try  {                     you=server.accept();                                      InetAddress address=you.getInetAddress();                     textArea1.append("进入的用户IP:"+address+"已连接"+"\n");                     //textArea1.append("正在监听\n");                     //textArea1.select(65536,65536);                     //System.out.println("用户的IP:"+address);                    if(you!=null)                     {Server_thread peopleThread=new Server_thread(you,peopleList);                     peopleThread.start();}                    else {continue;}                    }                catch (IOException e){}            }                  }         catch(IOException e1)               {                 textArea1.append("\n或者:本服务器的8888端口已在运行\n或者:java被防火墙屏蔽\nsocket未打开\n\n停止监听,请关闭\n");                 textArea1.select(65536,65536);                 //System.out.println("正在监听");               }     }    /**     * @param args the command line arguments     */    /** Exit the Application */    private void exitForm(java.awt.event.WindowEvent evt) {        System.exit(0);    }        public static void main(String[] args) {        new cha01ser();    }    }class Server_thread extends Thread{     String name=null,sex=null;                               Socket socket=null;   File file=null;   DataOutputStream out=null;   DataInputStream  in=null;   Hashtable peopleList=null;   Server_thread(Socket t,Hashtable list)       {          peopleList=list;         socket=t;         try {               in=new DataInputStream(socket.getInputStream());               out=new DataOutputStream(socket.getOutputStream());             }         catch (IOException e)             {             }        }   public void run()          {            while(true)      {    String s=null;           try           {            s=in.readUTF();                                   if(s.startsWith("姓名:"))                           {                name=s.substring(s.indexOf(":")+1,s.indexOf("性别"));                  sex=s.substring(s.lastIndexOf(":")+1);                                                         boolean boo=peopleList.containsKey(name);                if(boo==false)                   {                    peopleList.put(name,this);                              out.writeUTF("可以聊天:");                    Enumeration enum=peopleList.elements();                        while(enum.hasMoreElements())                                 {                       Server_thread th=(Server_thread)enum.nextElement();                       th.out.writeUTF("聊天者:"+name+"性别"+sex);                                                                       if(th!=this)                         {                           out.writeUTF("本聊天者:"+th.name+"性别"+th.sex);                        }                      }                    InetAddress address1=socket.getInetAddress();                    cha01ser.textArea1.append("(IP:"+address1+")"+name+"用户进入了\n");                    cha01ser.textArea1.append("正在监听\n");                    cha01ser.textArea1.select(65536,65536);                                                     }                else                  {                    out.writeUTF("不可以聊天:");                  }              }            else if(s.startsWith("公共聊天内容:"))               {                 String message=s.substring(s.indexOf(":")+1);                 Enumeration enum=peopleList.elements();                     while(enum.hasMoreElements())                     {                       ((Server_thread)enum.nextElement()).out.writeUTF("聊天内容:"+message);                     }                }                      else if(s.startsWith("用户离开:"))              {                 Enumeration enum=peopleList.elements();                     while(enum.hasMoreElements())                                { try                     {                        Server_thread th=(Server_thread)enum.nextElement();                        if(th!=this&&th.isAlive())                         {                           th.out.writeUTF("用户离线:"+name);                         }                     }                   catch(IOException eee)                     {                     }                   }                 peopleList.remove(name);                 InetAddress address2=socket.getInetAddress();                socket.close();                                        //System.out.println(name+"用户离开了");                cha01ser.textArea1.append("(IP:"+address2+")"+name+"用户离开了\n");                cha01ser.textArea1.append("正在监听\n");                cha01ser.textArea1.select(65536,65536);                    break;                                               }            else if(s.startsWith("私人聊天内容:"))              {                 String 悄悄话=s.substring(s.indexOf(":")+1,s.indexOf("#"));                 String toPeople=s.substring(s.indexOf("#")+1);                                  Server_thread toThread=(Server_thread)peopleList.get(toPeople);                                                    if(toThread!=null)                   {                     if (this != toThread) {                           this.out.writeUTF("私人聊天内容:"+悄悄话);                        }                    toThread.out.writeUTF("私人聊天内容:"+悄悄话);                   }                 else                     {                     out.writeUTF("私人聊天内容:"+toPeople+"已经离线");                   }              }           }       catch(IOException ee)              {               Enumeration enum=peopleList.elements();                     while(enum.hasMoreElements())                                { try                     {                        Server_thread th=(Server_thread)enum.nextElement();                        if(th!=this&&th.isAlive())                         {                           th.out.writeUTF("用户离线:"+name);                         }                     }                   catch(IOException eee)                     {                     }                   }                 if (name!=null)                {                peopleList.remove(name);                }                InetAddress address1=socket.getInetAddress();                 try                    {                       socket.close();                    }                                    catch(IOException eee)                    {                    }                                            //System.out.println(name+"用户离开了");              if (name!=null){                  cha01ser.textArea1.append("(IP:"+address1+")"+name+"用户离开了\n");              }              cha01ser.textArea1.append("离开的用户IP:"+address1+"已断线\n");              cha01ser.textArea1.append("正在监听\n");              cha01ser.textArea1.select(65536,65536);              break;                                            }                  }   }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -