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

📄 chatserverframe.java~174~

📁 计算机网络课程设计 基于中心服务器方式即时数据通信平台(IM)1. 能进行用户管理
💻 JAVA~174~
📖 第 1 页 / 共 2 页
字号:
                        try{                          int rsupdate=stmt.executeUpdate("INSERT INTO sc(St,Sf,Sb,Content)"+                                    "VALUES ('"+now+"','"+fromcast+"','所有用户','"+info+"');");                            }                        catch(SQLException  ex){                        }                    }                    else                    {                            sendtoClient(message,broadcast);                            String now=gettime();                            jTextArea1.append("系统消息: "+now+"/"+fromcast+"->"+broadcast+info+"\n");                  try{                 int rsupdate=stmt.executeUpdate("INSERT INTO sc(St,Sf,Sb,Content)"+                   "VALUES ('"+now+"','"+fromcast+"','"+broadcast+"','"+info+"');");                   }                 catch(SQLException  ex){                   }                }            }            else if(keyword.equalsIgnoreCase("QUIT"))             {                   ((DefaultListModel)jList1.getModel()).removeAllElements();                    disconnect(this);                  for(int i=0;i<clients.size();i++)                    {                    Client listdata1=(Client)clients.elementAt(i);                    jList1.setModel(dld);                    dld.addElement(listdata1.name+":"+listdata1.ip+"已经连接");                    }                    notifyRoom();                 this.stop() ;            }            else if(keyword.equalsIgnoreCase("SOU"))            {              StringBuffer message=new StringBuffer("SOU:");              message.append(broadcast+st.nextToken("\0"));              sendtoClient(message,broadcast);            }    }    }public void send(StringBuffer msg){ ps.println(msg); ps.flush();}}      public static synchronized void sendClients(StringBuffer msg)          {                  for(int i=0;i<clients.size();i++){                          Client c=(Client)clients.elementAt(i);                      c.send(msg);                  }          }          public synchronized void sendtoClient(StringBuffer msg,String name)          {                  for(int i=0;i<clients.size();i++){                          Client c=(Client)clients.elementAt(i);                      if(c.name.equalsIgnoreCase(name))                      {                              c.send(msg);                              break;                      }                  }          }          public static void notifyRoom()          {                  StringBuffer people=new StringBuffer("PEOPLE");                  for(int i=0;i<clients.size();i++){                          Client c=(Client)clients.elementAt(i);                          people.append(":"+c.name);                  }                  sendClients(people);          }public static String gettime(){ Date now=new Date();//获取系统当前时间并赋值给变量String time=String.valueOf(now.getYear()+1900)+"."+String.valueOf(now.getMonth()+1)+"."+String.valueOf(now.getDate())+"["+String.valueOf(now.getHours())+":"+String.valueOf(now.getMinutes())+":"+String.valueOf(now.getSeconds()+"]");return(time);}          public static boolean checkName(Client newClient)          {          for(int i=0;i<clients.size();i++){              Client c=(Client)clients.elementAt(i);              if((c!=newClient)&&c.name.equals(newClient.name))              {                return false;              }            }            return true;          }          public synchronized void disconnect(Client c)          {                  try{                          String qn=c.name;                          jTextArea1.append("系统消息:  "+qn+"断开连接\n");                          active_connects--;                          c.socket.close();                      clients.removeElement(c);                      sendClients(new StringBuffer("QUIT"+":"+qn));                      jTextField1.setText("目前已经有"+clients.size()+"用户连接");                      c.socket.close();                  }                  catch(IOException e)                  {                          jTextArea1.append("系统消息:"+"Error"+e+"\n");                  }          }          public void closeAll()          {                  while(clients.size()>0)                  {                          Client c=(Client)clients.firstElement();                          try{                                  c.socket.close();                          }                          catch(IOException e){                                  jTextArea1.append("系统消息:"+"聊天服务器"+"Error"+e+"\n");                          }                          finally{                                  clients.removeElement(c);                          }                  }          }  void jButton1_actionPerformed(ActionEvent e) {    StringBuffer info=new StringBuffer("MSG:");    if(jRadioButton1.isSelected()){            info.append(":"+"BROAD:"+"SERVER"+":");            info.append(jTextField2.getText());            sendClients(info);            String now=gettime();            jTextArea1.append("系统消息: "+now+"/SERVER"+"->"+"所用用户:"+jTextField2.getText()+"\n");try{int rsupdate=stmt.executeUpdate("INSERT INTO sc(St,Sf,Sb,Content)"+ "VALUES ('"+now+"','SERVER','所用用户','"+jTextField2.getText()+"');"); }catch(SQLException  ex){}}    else{      info.append(":"+"SERVER:"+"SERVER"+":");      info.append(jTextField2.getText());      StringTokenizer st=new StringTokenizer((String)jList1.getSelectedValue(),":");      String qn=st.nextToken();      sendtoClient(info,qn);      String now=gettime();      jTextArea1.append("系统消息: "+now+"/SERVER"+"->"+qn+":"+jTextField2.getText()+"\n");      try{      int rsupdate=stmt.executeUpdate("INSERT INTO sc(St,Sf,Sb,Content)"+       "VALUES ('"+now+"','SERVER','"+qn+"','"+jTextField2.getText()+"');");       }      catch(SQLException  ex){}    }    jTextField2.setText("");  }  void jButton4_actionPerformed(ActionEvent e) {    sendClients(new StringBuffer("QUIT:"+"SERVER"));        closeAll();        System.exit(0);  }  void jButton2_actionPerformed(ActionEvent e) {    StringTokenizer st=new StringTokenizer((String)jList1.getSelectedValue(),":");    String qn=st.nextToken();     for(int i=0;i<clients.size();i++){         Client c=(Client)clients.elementAt(i);         if(c.name.equalsIgnoreCase(qn))         {           c.send(new StringBuffer("QUIT:"+"DISC"));            break;          }     } }  void this_windowClosing(WindowEvent e) {    sendClients(new StringBuffer("QUIT:"+"SERVER"));            closeAll();            System.exit(0);  }}class linkData {      public static Statement getStatement() {          try {              //注册驱动              Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");              String URL =                  "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=Server.mdb";              Connection conn = DriverManager.getConnection(URL);              return conn.createStatement();          }          catch (SQLException ex) {              ex.printStackTrace();              return null;          }          catch (ClassNotFoundException ex) {              ex.printStackTrace();              return null;          }      }  }class chatServerFrame_jButton3_actionAdapter implements java.awt.event.ActionListener {  chatServerFrame adaptee;  chatServerFrame_jButton3_actionAdapter(chatServerFrame adaptee) {    this.adaptee = adaptee;  }  public void actionPerformed(ActionEvent e) {    adaptee.jButton3_actionPerformed(e);  }}class chatServerFrame_jButton1_actionAdapter implements java.awt.event.ActionListener {  chatServerFrame adaptee;  chatServerFrame_jButton1_actionAdapter(chatServerFrame adaptee) {    this.adaptee = adaptee;  }  public void actionPerformed(ActionEvent e) {    adaptee.jButton1_actionPerformed(e);  }}class chatServerFrame_jButton4_actionAdapter implements java.awt.event.ActionListener {  chatServerFrame adaptee;  chatServerFrame_jButton4_actionAdapter(chatServerFrame adaptee) {    this.adaptee = adaptee;  }  public void actionPerformed(ActionEvent e) {    adaptee.jButton4_actionPerformed(e);  }}class chatServerFrame_jButton2_actionAdapter implements java.awt.event.ActionListener {  chatServerFrame adaptee;  chatServerFrame_jButton2_actionAdapter(chatServerFrame adaptee) {    this.adaptee = adaptee;  }  public void actionPerformed(ActionEvent e) {    adaptee.jButton2_actionPerformed(e);  }}class chatServerFrame_this_windowAdapter extends java.awt.event.WindowAdapter {  chatServerFrame adaptee;  chatServerFrame_this_windowAdapter(chatServerFrame adaptee) {    this.adaptee = adaptee;  }  public void windowClosing(WindowEvent e) {    adaptee.this_windowClosing(e);  }}

⌨️ 快捷键说明

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