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

📄 serverframe.java

📁 Java 聊天程序 有客户端和服务端。
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
package chatserver;

import java.io.*;
import java.net.*;
import java.sql.*;
import java.text.*;
import java.util.*;
import java.util.Date;
import java.awt.*;
import java.awt.List;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import com.borland.jbcl.layout.*;

/**
 * <p>Title: 聊天</p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2005</p>
 *
 * <p>Company: 个人</p>
 *
 * @author Mothz
 * @version 1.0
 */

public class ServerFrame
    extends JFrame {
    JPanel contentPane;
    XYLayout xYLayout1 = new XYLayout();
    JPanel jPanel1 = new JPanel();
    JPanel jPanel2 = new JPanel();
    JButton send = new JButton();
    JButton sysexit = new JButton();
    JButton link = new JButton();
    JTextField jTextField2 = new JTextField();
    BorderLayout borderLayout1 = new BorderLayout();
    JPanel jPanel3 = new JPanel();
    JScrollPane jScrollPane1 = new JScrollPane();
    PaneLayout paneLayout1 = new PaneLayout();
    JLabel jLabel1 = new JLabel();
    JLabel jLabel2 = new JLabel();
    JLabel jLabel3 = new JLabel();
    List list1 = new List();
    BorderLayout borderLayout2 = new BorderLayout();
    JPanel jPanel4 = new JPanel();
    JScrollPane jScrollPane2 = new JScrollPane();
    PaneLayout paneLayout2 = new PaneLayout();
    JLabel jLabel4 = new JLabel();
    JLabel jLabel5 = new JLabel();
    JLabel jLabel6 = new JLabel();
    JTextArea jTextArea1 = new JTextArea();
    JLabel jLabel7 = new JLabel();
    JLabel jLabel8 = new JLabel();
    JLabel runtime = new JLabel();
    JLabel jLabel9 = new JLabel();
    JLabel jLabel10 = new JLabel();
    JLabel jLabel11 = new JLabel();
    JLabel online = new JLabel();
    JLabel jLabel12 = new JLabel();
    JLabel jLabel13 = new JLabel();
    JMenuBar jMenuBar1 = new JMenuBar();
    JMenu jMenu1 = new JMenu();
    JMenuItem jMenuItem2 = new JMenuItem();
    JMenu jMenu2 = new JMenu();
    JMenu jMenu3 = new JMenu();
    JMenuItem jMenuItem5 = new JMenuItem();
    JMenuItem jMenuItem1 = new JMenuItem();
    JMenuItem jMenuItem3 = new JMenuItem();
    JButton jButton1 = new JButton();

    //定义相关成员变量
    ServerSocket serverSocket = null;
    //建立服务器的socket
    Socket socket = null; //用来存储一个连接套接字
    Vector clients = new Vector(20);
    //用vector向量数组存储连接客户变量
    //静态AdName 用于显示当前用户名
    static String AdName;
    //用户号码注册
    int mothzNumber = 0;
    //全局变量notEnd 用于while循环
    boolean notEnd = true;
    Connection conn = null;
    //数据库 初始化变量
    Statement stmt = null;
    ResultSet rs = null;

    //Construct the frame
    public ServerFrame() {
        enableEvents(AWTEvent.WINDOW_EVENT_MASK);
        try {
            this.setIconImage(Toolkit.getDefaultToolkit().createImage(
                "images/1.png"));
            jbInit();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

    //Component initialization
    private void jbInit() throws Exception {
        this.setResizable(false);
        contentPane = (JPanel)this.getContentPane();
        contentPane.setLayout(xYLayout1);
        this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        this.setJMenuBar(jMenuBar1);
        this.setSize(new Dimension(410, 403));
        this.setTitle("网络聊天服务器");
        send.setBorder(null);
        send.setIcon(new ImageIcon("images/send.jpg"));
        send.addActionListener(new ServerFrame_send_actionAdapter(this));
        sysexit.setBorder(null);
        sysexit.setIcon(new ImageIcon("images/exit.jpg"));
        sysexit.addActionListener(new ServerFrame_sysexit_actionAdapter(this));
        link.setBorder(null);
        link.setIcon(new ImageIcon("images/start.jpg"));
        link.addActionListener(new ServerFrame_link_actionAdapter(this));
        jPanel1.setLayout(borderLayout1);
        jPanel3.setLayout(paneLayout1);
        jLabel1.setText("用户名");
        jLabel2.setText("IP地址");
        jLabel3.setText("连接情况");
        jPanel2.setLayout(borderLayout2);
        jPanel4.setLayout(paneLayout2);
        jLabel4.setText("信息类型");
        jLabel5.setText("信息来源");
        jLabel6.setText("信息内容");
        jTextArea1.setFont(new java.awt.Font("宋体", Font.PLAIN, 12));
        jTextArea1.setBorder(BorderFactory.createLoweredBevelBorder());
        jTextArea1.setText("");
        jTextArea1.setCaretPosition(jTextArea1.getDocument().getLength());
        jTextField2.setFont(new java.awt.Font("Serif", 0, 12));
        jTextField2.setText("");
        jPanel2.setBorder(BorderFactory.createEtchedBorder());
        jPanel1.setBorder(BorderFactory.createEtchedBorder());
        jLabel7.setHorizontalAlignment(SwingConstants.RIGHT);
        jLabel7.setText("当前在线:");
        jLabel8.setHorizontalAlignment(SwingConstants.RIGHT);
        jLabel8.setText("服务器运行:");
        runtime.setText("0 : 0");
        jLabel9.setHorizontalAlignment(SwingConstants.RIGHT);
        jLabel9.setText("发送系统消息:");
        jLabel10.setHorizontalAlignment(SwingConstants.RIGHT);
        jLabel10.setText("当前系统时间:");
        jLabel11.setForeground(Color.blue);
        jLabel11.setText("[00]");
        jLabel11.addAncestorListener(new ServerFrame_jLabel11_ancestorAdapter(this));
        online.setText("0");
        jLabel12.setHorizontalAlignment(SwingConstants.RIGHT);
        jLabel12.setText("当前管理员:");
        jLabel13.setText(AdName);
        jMenu1.setText("文件");
        jMenuItem2.setText("退出服务器");
        jMenuItem2.addActionListener(new ServerFrame_jMenuItem2_actionAdapter(this));
        jMenu2.setText("操作");
        jMenu3.setText("帮助");
        jMenuItem5.setText("关于");
        jMenuItem5.addActionListener(new ServerFrame_jMenuItem5_actionAdapter(this));
        jMenuItem1.setText("客户管理");
        jMenuItem1.addActionListener(new ServerFrame_jMenuItem1_actionAdapter(this));
        list1.setEnabled(false);
        jMenuItem3.setText("管理员管理");
        jMenuItem3.addActionListener(new ServerFrame_jMenuItem3_actionAdapter(this));
        jButton1.setIcon(new ImageIcon("images/stop.jpg"));
        jButton1.addActionListener(new ServerFrame_jButton1_actionAdapter(this));
        jButton1.setEnabled(false);
        jButton1.setBorder(null);
        jPanel1.add(jPanel3, BorderLayout.NORTH);
        jPanel1.add(jScrollPane1, BorderLayout.CENTER);
        jScrollPane1.getViewport().add(list1, null);
        jPanel2.add(jPanel4, BorderLayout.NORTH);
        jPanel2.add(jScrollPane2, BorderLayout.CENTER);
        jScrollPane2.getViewport().add(jTextArea1, null);
        jPanel3.add(jLabel2,
                    new PaneConstraints("jLabel2", "jLabel2",
                                        PaneConstraints.ROOT,
                                        0.5f));
        jPanel3.add(jLabel3,
                    new PaneConstraints("jLabel3", "jLabel2",
                                        PaneConstraints.RIGHT,
                                        0.40050375f));
        jPanel3.add(jLabel1,
                    new PaneConstraints("jLabel1", "jLabel2",
                                        PaneConstraints.LEFT,
                                        0.35294116f));
        jPanel4.add(jLabel5,
                    new PaneConstraints("jLabel5", "jLabel5",
                                        PaneConstraints.ROOT,
                                        0.5f));
        jPanel4.add(jLabel6,
                    new PaneConstraints("jLabel6", "jLabel5",
                                        PaneConstraints.RIGHT,
                                        0.46599495f));
        jPanel4.add(jLabel4,
                    new PaneConstraints("jLabel4", "jLabel5",
                                        PaneConstraints.LEFT,
                                        0.43478262f));
        contentPane.add(jLabel10, new XYConstraints(9, 321, -1, -1));
        contentPane.add(jLabel11, new XYConstraints(91, 321, 201, -1));
        contentPane.add(jLabel12, new XYConstraints(21, 212, -1, -1));
        contentPane.add(online, new XYConstraints(91, 238, 74, -1));
        contentPane.add(jLabel7, new XYConstraints(33, 238, -1, -1));
        contentPane.add(runtime, new XYConstraints(91, 263, 109, 16));
        contentPane.add(jLabel8, new XYConstraints(11, 263, 74, 16));
        contentPane.add(jLabel9, new XYConstraints(7, 291, 80, -1));
        contentPane.add(jTextField2, new XYConstraints(91, 287, 201, 20));
        contentPane.add(jLabel13, new XYConstraints(91, 212, 125, 16));
        contentPane.add(send, new XYConstraints(309, 287, -1, 20));
        contentPane.add(sysexit, new XYConstraints(309, 319, 75, 20));
        jMenuBar1.add(jMenu1);
        jMenuBar1.add(jMenu2);
        jMenuBar1.add(jMenu3);
        jMenu1.add(jMenuItem2);
        jMenu2.add(jMenuItem1);
        jMenu2.add(jMenuItem3);
        jMenu3.add(jMenuItem5);
        contentPane.add(jPanel2, new XYConstraints(1, 114, 397, 86));
        contentPane.add(jPanel1, new XYConstraints(2, 17, 397, 95));
        contentPane.add(jButton1, new XYConstraints(262, 250, 130, 28));
        contentPane.add(link, new XYConstraints(262, 210, 130, 35));
        //默认系统消息栏和发送按钮不可操作
        jTextField2.setEnabled(false);
        send.setEnabled(false);
    }

    //Overridden so we can exit when window is closed
    protected void processWindowEvent(WindowEvent e) {
        super.processWindowEvent(e);
        if (e.getID() == WindowEvent.WINDOW_CLOSING) {
            sendClients(new StringBuffer("QUIT:"
                                         + "SERVER"));
            closeAll();
            System.exit(0);
        }
    } //end of processWindowEvent

    //产生随机数
    public void Ramdon() {
        Statement stmt = linkData.getStatement();
        boolean romdons = true;
        /*使用while循环产生随机数,并进行数据库查询
         *若号码存在则继续产生一个随机数,再次进行数据库查询
         *若号码不存在,则将产生的随机数赋值到mothzNumber
         *同时停止while循环*/
        while (romdons) {
            //产生一个随机数,并转为int型,赋值到mothzNumber
            mothzNumber = (int) (Math.random() * 100000);
            try {
                //数据库查询
                rs = stmt.executeQuery(
                    "SELECT * FROM clients WHERE id='" +
                    mothzNumber + "'");
                if (rs.next()) {
                    //号码存在则继续while循环
                    romdons = true;
                }
                else {
                    //号码不存在则停止while循环
                    romdons = false;
                }
            }
            catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
    }

    /*  class file
          extends Thread {
          BufferedReader user;
          int port=6789;
          ServerSocket serverSkt;
          Socket clientSkt;
          BufferedReader fileInfo;
          BufferedInputStream fileReader;
          BufferedOutputStream fileWriter;
          String fileName;
          PrintStream printStream;
          DataInputStream dis;
          //用于实现接受从客户端发送来的数据流
          DataOutputStream dos;
          //用来实现向客户端发送信息的打印流

          public void run(){
          try {
            //  user = new BufferedReader(new InputStreamReader());



              serverSkt = new ServerSocket(port);
              while (true) {
                  System.out.println("文件传输端口倾听中....");
                  clientSkt = serverSkt.accept();
                  printStream = new PrintStream(clientSkt.getOutputStream());

                  //fileInfo = new BufferedReader(new InputStreamReader(clientSkt.getInputStream()));
                  dis = new DataInputStream(clientSkt.getInputStream());
                  //fileName = fileInfo.readLine();

                  System.out.println("与" +
                                     clientSkt.getInetAddress() + "建立连接");
                  System.out.print("接收文件 " + fileName + " (yes/no) ? ");
                  if (user.readLine().equals("no")) {
                      printStream.println("no");
                      clientSkt.close();
                  }
                  else {
                      printStream.println("yes");
                      System.out.print("\nOK! 接收文件 " + fileName);
                      fileReader = new BufferedInputStream(clientSkt.
                          getInputStream());
     fileWriter = new BufferedOutputStream(new FileOutputStream(
                          fileName + "1"));
                      int readin;
                      while ( (readin = fileReader.read()) != -1) {
                          //System.out.print(".");
                          fileWriter.write(readin);
                      }
                      fileReader.close();
                      fileWriter.flush();
                      clientSkt.close();
                      System.out.println("\n文件接收完毕!");
                  }
              }
          }
          catch (IOException e) {
              System.out.println(e.toString());
          }
          }
      }
     */

    //Client类是ServerFrame类的内部类
    class Client
        extends Thread {
        String name;
        //用来存储客户的连接姓名
        String psword;
        //用来存储客户的连接密码
        String ip;
        //用来存储客户的连接IP
        DataInputStream dis;
        //用于实现接受从客户端发送来的数据流
        DataOutputStream dos;
        //用来实现向客户端发送信息的打印流
        //与书上不同是为了能处理中文,不然遇到中文会是乱码
        Socket socket;

        //Client 线程的构造方法
        public Client(Socket s) {
            socket = s;
            try {
                dis = new DataInputStream
                    (s.getInputStream());
                //存储特定客户socket的输入流,
                //接受s客户发送到服务器端的信息
                dos = new DataOutputStream
                    (s.getOutputStream());
                //存储特定客户socket的输出流,
                //发送服务器给s客户的信息
                String info = dis.readUTF();
                //读取接受到的信息
                StringTokenizer stinfo = new
                    StringTokenizer(info, ":");
                //读取用":"分开的分段字符
                String head = stinfo.nextToken();
                //读取第一个分段字符串,用head存储关键字的头信息

⌨️ 快捷键说明

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