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

📄 chatserverframe.java~178~

📁 此软件为简单的实时通信软件,带文件传输与语音功能,类似于QQ
💻 JAVA~178~
📖 第 1 页 / 共 2 页
字号:
package chatserver;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.borland.jbcl.layout.*;
import java.net.*;
import java.io.*;
import java.util.Vector;
import java.util.StringTokenizer;
import java.util.Date;
import java.lang.*;
import java.sql.*;
import com.borland.dx.dataset.*;
import com.borland.dx.sql.dataset.*;


/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2007</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */

public class chatServerFrame  extends JFrame  {
  JPanel contentPane;
  XYLayout xYLayout1 = new XYLayout();
  JPanel jPanel1 = new JPanel();
  JPanel jPanel2 = new JPanel();
  JPanel jPanel3 = new JPanel();
  JPanel jPanel4 = new JPanel();
  JPanel jPanel5 = new JPanel();
  PaneLayout paneLayout1 = new PaneLayout();
  JLabel jLabel1 = new JLabel();
  PaneLayout paneLayout2 = new PaneLayout();
  PaneLayout paneLayout3 = new PaneLayout();
  JScrollPane jScrollPane1 = new JScrollPane();
  JButton jButton1 = new JButton();
  XYLayout xYLayout2 = new XYLayout();
  JPanel jPanel6 = new JPanel();
  JPanel jPanel7 = new JPanel();
  JPanel jPanel8 = new JPanel();
  PaneLayout paneLayout4 = new PaneLayout();
  JLabel jLabel2 = new JLabel();
  PaneLayout paneLayout5 = new PaneLayout();
  PaneLayout paneLayout6 = new PaneLayout();
  JScrollPane jScrollPane2 = new JScrollPane();
  JTextField jTextField2 = new JTextField();
  JButton jButton2 = new JButton();
  JRadioButton jRadioButton1 = new JRadioButton();
  JRadioButton jRadioButton2 = new JRadioButton();
  ButtonGroup buttonGroup1 = new ButtonGroup();
  JList jList1 = new JList();
  JTextArea jTextArea1 = new JTextArea();
  JButton jButton3 = new JButton();
  XYLayout xYLayout3 = new XYLayout();
  JButton jButton4 = new JButton();
  JTextField jTextField1 = new JTextField();
  static ServerSocket serverSocket = null;
  static Socket socket = null;
  BufferedReader cin = null;
  PrintWriter cout = null;
  static Vector clients = new Vector(20);
  static int active_connects = 0;
  DefaultListModel   dld =new DefaultListModel();

  //Construct the frame
  public chatServerFrame() {
    enableEvents(AWTEvent.WINDOW_EVENT_MASK);
    try {
      jbInit();
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }
  //Component initialization
  private void jbInit() throws Exception  {
    contentPane = (JPanel) this.getContentPane();
    contentPane.setLayout(xYLayout1);
    this.setSize(new Dimension(407, 331));
    this.setTitle("chatServer");
    this.addWindowListener(new chatServerFrame_this_windowAdapter(this));
    jPanel1.setBackground(Color.white);
    jPanel1.setLayout(paneLayout1);
    jPanel2.setBackground(Color.white);
    jPanel2.setLayout(paneLayout4);
    jLabel1.setText(" 用户信息");
    jPanel5.setLayout(paneLayout2);
    jPanel3.setLayout(paneLayout3);
    jButton1.setText("发送");
    jButton1.addActionListener(new chatServerFrame_jButton1_actionAdapter(this));
    jPanel4.setLayout(xYLayout2);
    jPanel6.setLayout(paneLayout5);
    jLabel2.setText("服务器信息");
    jPanel7.setLayout(paneLayout6);
    jButton2.setText("删除用户");
    jButton2.addActionListener(new chatServerFrame_jButton2_actionAdapter(this));
    jRadioButton1.setText("广播发送");
    jRadioButton2.setDoubleBuffered(false);
    jRadioButton2.setSelected(true);
    jRadioButton2.setText("选择发送");
    jTextField2.setText("");
    jTextArea1.setEnabled(true);
    jTextArea1.setEditable(false);
    jTextArea1.setText("");
    jButton3.setText("启动");
    jButton3.addActionListener(new chatServerFrame_jButton3_actionAdapter(this));
    jPanel8.setLayout(xYLayout3);
    jButton4.setText("退出");
    jButton4.addActionListener(new chatServerFrame_jButton4_actionAdapter(this));
    jTextField1.setEnabled(true);
    jTextField1.setEditable(false);
    jTextField1.setText("");
    contentPane.add(jPanel1,      new XYConstraints(0, 0, 400, 138));
    jPanel1.add(jPanel5,        new PaneConstraints("jPanel5", "jPanel5", PaneConstraints.ROOT, 0.5f));
    jPanel5.add(jLabel1,  new PaneConstraints("jLabel1", "jLabel1", PaneConstraints.ROOT, 1.0f));
    jPanel1.add(jPanel4,        new PaneConstraints("jPanel4", "jPanel5", PaneConstraints.BOTTOM, 0.92424244f));
    jPanel4.add(jTextField2,  new XYConstraints(3, 37, 331, -1));
    jPanel4.add(jButton1,  new XYConstraints(339, 37, -1, 21));
    jPanel4.add(jButton2,     new XYConstraints(16, 6, -1, 24));
    jPanel4.add(jRadioButton2,   new XYConstraints(312, 6, -1, -1));
    jPanel4.add(jRadioButton1, new XYConstraints(225, 7, -1, -1));
    jPanel1.add(jPanel3,        new PaneConstraints("jPanel3", "jPanel4", PaneConstraints.TOP, 0.5488722f));
    jPanel3.add(jScrollPane1, new PaneConstraints("jScrollPane1", "jScrollPane1", PaneConstraints.ROOT, 1.0f));
    contentPane.add(jPanel2,      new XYConstraints(-1, 145, 401, 156));
    jPanel2.add(jPanel8,             new PaneConstraints("jPanel8", "jPanel8", PaneConstraints.ROOT, 0.5f));
    jPanel2.add(jPanel6,             new PaneConstraints("jPanel6", "jPanel8", PaneConstraints.TOP, 0.53846157f));
    jPanel6.add(jLabel2,  new PaneConstraints("jLabel2", "jLabel2", PaneConstraints.ROOT, 1.0f));
    jPanel2.add(jPanel7,             new PaneConstraints("jPanel7", "jPanel6", PaneConstraints.BOTTOM, 0.8333333f));
    jPanel7.add(jScrollPane2,  new PaneConstraints("jScrollPane2", "jScrollPane2", PaneConstraints.ROOT, 1.0f));
    buttonGroup1.add(jRadioButton1);
    buttonGroup1.add(jRadioButton2);
    jScrollPane1.getViewport().add(jList1, null);
    jScrollPane2.getViewport().add(jTextArea1, null);
    jPanel8.add(jTextField1,      new XYConstraints(5, 45, 280, -1));
    jPanel8.add(jButton3, new XYConstraints(329, 5, -1, -1));
    jPanel8.add(jButton4,  new XYConstraints(329, 39, -1, -1));
  }
  //Overridden so we can exit when window is closed
  protected void processWindowEvent(WindowEvent e) {
    super.processWindowEvent(e);
    if (e.getID() == WindowEvent.WINDOW_CLOSING) {
      System.exit(0);
    }
  }

 Statement stmt = linkData.getStatement();
  void jButton3_actionPerformed(ActionEvent e) {
    if (serverSocket == null) {
          try {
            serverSocket = new ServerSocket(4000);
            jTextArea1.append("系统提示:  聊天服务器" +
                              "               系统开始启动......\n");
          }
          catch (IOException ee) {
            jTextArea1.append("系统提示:  聊天服务器"
                              + e.toString() + "\n");
          }
        if (serverSocket != null) {
          jTextArea1.append("系统提示:  聊天服务器" +
                            "                已经启动\n");
          Thread a = new watch();
          a.start();
        }
  }
  }
  class watch extends Thread {
      public void run() {
        int i = 0;
        while (true) {
          if (clients.size() < 20) {
            try {
              socket = serverSocket.accept();
            }
            catch (IOException ee) {
              jTextArea1.append("系统提示:  聊天服务器"
                                + ee.toString() + "用户连接失败\n");
            }
            do {
              Client c = new Client(socket);
              clients.addElement(c);
             if(checkName(c)) {
               c.ps.println("WELCOME");
               c.ps.flush();
             int num = active_connects + 1;
                active_connects++;
                jTextField1.setText("目前已经有" + num + "用户连接");
                Client listdata = (Client)clients.elementAt(i);
                jList1.setModel(dld);
                dld.addElement(listdata.name+": "+listdata.ip+"已经连接");
                c.start();
                notifyRoom();
              }
              else {
                c.ps.println("FAULTNAME");
                c.ps.flush();
                clients.removeElement(c);
                try{
                  c.socket.close();
                  c.stop();
                }
                catch(IOException e)
                 {
                    jTextArea1.append("系统消息:"+"Error"+e);
                 }
            }
              i++;
              break;
            }
            while (i < clients.size());
          }
          else {
            try {
              Thread.sleep(400);
            }
            catch (InterruptedException ee) {
            }
          }
        }
      }
    }
    class Client extends Thread{
    String name;
    String ip;
    DataInputStream dis;
    PrintStream ps;
    Socket socket;
        public Client(Socket s) {
         socket=s;
         try{
           dis=new DataInputStream(s.getInputStream());
           ps=new PrintStream(s.getOutputStream());
           String info=dis.readLine();
           StringTokenizer stinfo=new StringTokenizer(info,":");
           String head=stinfo.nextToken();
           if(stinfo.hasMoreTokens())
            name=stinfo.nextToken();
           if(stinfo.hasMoreTokens())
            ip=stinfo.nextToken();
            jTextArea1.append("系统消息:  "+name+":"+ip+"已经连接\n");
          }
          catch(IOException e){
            jTextArea1.append("系统消息:   聊天服务器"+"Error  "+e+"\n");
          }
        }
        public void run(){
        while(true){
            String line=null;
            try{
                    line=dis.readLine();
            }
            catch(IOException e)
            {
                    jTextArea1.append("系统消息: "+"聊天服务器"+"Error: "+e);
                    disconnect(this);
                    notifyRoom();
                    return;
            }
            if(line==null)
            {
                    disconnect(this);
                    notifyRoom();
                    return;
            }
            StringTokenizer st=new StringTokenizer(line,":");
            String keyword=st.nextToken();
             String broadcast=st.nextToken();
            if(keyword.equalsIgnoreCase("MSG"))
            {
                    String fromcast=st.nextToken();
                    String info=st.nextToken("\0");
                    StringBuffer message=new StringBuffer("MSG:");

⌨️ 快捷键说明

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