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

📄 lserver.java

📁 实现简单聊天、群聊以及点对点文件传输,有兴趣的朋友可以拿去
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * LSever.java * * Created on 2008年6月2日, 下午3:46 */package LServer;import java.awt.event.KeyEvent;import java.io.*;import java.net.*;import java.util.*;import java.util.logging.Level;import java.util.logging.Logger;import javax.swing.ComboBoxModel;import javax.swing.DefaultComboBoxModel;import javax.swing.JOptionPane;/** * * @author  Administrator */public class LServer extends javax.swing.JFrame {        /** Creates new form LSever */    public LServer() {        initComponents();    }        /** This method is called from within the constructor to     * initialize the form.     * WARNING: Do NOT modify this code. The content of this method is     * always regenerated by the Form Editor.     */    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents    private void initComponents() {        jPanel1 = new javax.swing.JPanel();        jScrollPane1 = new javax.swing.JScrollPane();        jTextArea1 = new javax.swing.JTextArea();        jButton1 = new javax.swing.JButton();        jButton2 = new javax.swing.JButton();        jPanel2 = new javax.swing.JPanel();        jScrollPane2 = new javax.swing.JScrollPane();        jTextArea2 = new javax.swing.JTextArea();        jComboBox1 = new javax.swing.JComboBox();        jPanel3 = new javax.swing.JPanel();        jTextField1 = new javax.swing.JTextField();        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);        setTitle("服务器");        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("用户状态"));        jTextArea1.setColumns(20);        jTextArea1.setRows(5);        jTextArea1.setEnabled(false);        jScrollPane1.setViewportView(jTextArea1);        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);        jPanel1.setLayout(jPanel1Layout);        jPanel1Layout.setHorizontalGroup(            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 364, Short.MAX_VALUE)        );        jPanel1Layout.setVerticalGroup(            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 153, Short.MAX_VALUE)        );        jButton1.setText("关闭服务器");        jButton1.setEnabled(false);        jButton1.addActionListener(new java.awt.event.ActionListener() {            public void actionPerformed(java.awt.event.ActionEvent evt) {                jButton1ActionPerformed(evt);            }        });        jButton2.setText("开启服务器在端口:");        jButton2.addActionListener(new java.awt.event.ActionListener() {            public void actionPerformed(java.awt.event.ActionEvent evt) {                jButton2ActionPerformed(evt);            }        });        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("系统消息"));        jTextArea2.setColumns(20);        jTextArea2.setRows(5);        jTextArea2.setEnabled(false);        jTextArea2.addKeyListener(new java.awt.event.KeyAdapter() {            public void keyPressed(java.awt.event.KeyEvent evt) {                jTextArea2KeyPressed(evt);            }        });        jScrollPane2.setViewportView(jTextArea2);        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);        jPanel2.setLayout(jPanel2Layout);        jPanel2Layout.setHorizontalGroup(            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 364, Short.MAX_VALUE)        );        jPanel2Layout.setVerticalGroup(            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)            .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 86, Short.MAX_VALUE)        );        jComboBox1.setModel(getItem());        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("在线人数"));        jTextField1.setEnabled(false);        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);        jPanel3.setLayout(jPanel3Layout);        jPanel3Layout.setHorizontalGroup(            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)            .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 364, Short.MAX_VALUE)        );        jPanel3Layout.setVerticalGroup(            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)            .addGroup(jPanel3Layout.createSequentialGroup()                .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 31, Short.MAX_VALUE)                .addContainerGap())        );        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());        getContentPane().setLayout(layout);        layout.setHorizontalGroup(            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)            .addGroup(layout.createSequentialGroup()                .addContainerGap()                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)                .addContainerGap())            .addGroup(layout.createSequentialGroup()                .addGap(20, 20, 20)                .addComponent(jButton2)                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)                .addComponent(jComboBox1, 0, 115, Short.MAX_VALUE)                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)                .addComponent(jButton1)                .addGap(19, 19, 19))            .addGroup(layout.createSequentialGroup()                .addContainerGap()                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)                .addContainerGap())            .addGroup(layout.createSequentialGroup()                .addContainerGap()                .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)                .addContainerGap())        );        layout.setVerticalGroup(            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()                .addContainerGap()                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)                    .addComponent(jButton2)                    .addComponent(jButton1)                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))                .addContainerGap())        );        pack();    }// </editor-fold>//GEN-END:initComponents    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed        // TODO add your handling code here:          try {  Enumeration ren = peopleList.elements();            while(ren.hasMoreElements())                {                    Server_thread th = (Server_thread) ren.nextElement();                    th.out.writeUTF("系统消息:服务器已经关闭!");                }               } catch (IOException ex) {                 Logger.getLogger(LServer.class.getName()).log(Level.SEVERE, null, ex);                     }finally                {                    System.exit(0);                 }                    // System.exit(0);                }//GEN-LAST:event_jButton1ActionPerformedprivate ComboBoxModel getItem() {        String[] item = new String[64511];        for (Integer i = 1; i < 64511; i++) {            item[i-1] = String.valueOf(i + 1024);        }        return new DefaultComboBoxModel(item);    }public void Message(String io){       JOptionPane.showMessageDialog(this, io,"注意!",JOptionPane.INFORMATION_MESSAGE);}        static int i = 0;    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed        // TODO add your handling code here:        new Thread(new Runnable(){            public void run(){                   ServerSocket server = null;            Socket you = null;            Hashtable peopleList = new Hashtable();            while (true)            {                    jComboBox1.setEnabled(false);                    jTextArea1.setEnabled(true);                    jTextArea2.setEnabled(true);                               jButton1.setEnabled(true);                    jButton2.setEnabled(false);                    jTextField1.setEnabled(true);                    try {                        int port = Integer.parseInt((String) jComboBox1.getSelectedItem());                        server = new ServerSocket(port);                        jTextArea1.append("服务器运行在" + port + "端口..." + "\n");                    } catch (IOException ex) {                        jTextArea1.append("正在监听..." + "\n");                      }                        jTextField1.setText("当前在线用户数为:" + i + "人!");

⌨️ 快捷键说明

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