📄 server.java
字号:
package javaapplication2;import java.io.*;import java.net.*;public class Server extends javax.swing.JFrame { public Server() { initComponents(); } public Server(int port) { initComponents(); } // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { jLabel1 = new javax.swing.JLabel(); Port = new javax.swing.JTextField(); Start = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jLabel1.setText("Port"); Port.setText("8111"); Start.setText("Start"); Start.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { StartActionPerformed(evt); } }); 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(jLabel1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(Port, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(Start) .addContainerGap(25, Short.MAX_VALUE)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel1) .addComponent(Start) .addComponent(Port, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); pack(); }// </editor-fold>//GEN-END:initComponents private void StartActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_StartActionPerformed Start.setEnabled(false); Thread c= new StartServer(Integer.parseInt(Port.getText())); c.start(); }//GEN-LAST:event_StartActionPerformed public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new Server().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JTextField Port; private javax.swing.JButton Start; private javax.swing.JLabel jLabel1; // End of variables declaration//GEN-END:variables }//********************************************************************class StartServer extends Thread{ static Socket clientSocket = null; static PrintStream os = null; static DataInputStream is = null; static BufferedReader inputLine = null; static boolean closed = false; int port_number; static ServerSocket serverSocket = null; static clientThread t[] = new clientThread[10]; public StartServer(int port) { port_number=port; } public void run() { try { serverSocket = new ServerSocket(port_number); } catch (IOException e) {System.out.println(e);} while(true){ try { clientSocket = serverSocket.accept(); for(int i=0; i<=9; i++){ if(t[i]==null){ (t[i] = new clientThread(clientSocket,t)).start(); break; } } } catch (IOException e) { System.out.println(e);} } } }//********************************************************************class clientThread extends Thread{ DataInputStream is = null; PrintStream os = null; Socket clientSocket = null; clientThread t[]; public clientThread(Socket clientSocket, clientThread[] t){ this.clientSocket=clientSocket; this.t=t; } public void run() { String line; String name; try{ is = new DataInputStream(clientSocket.getInputStream()); os = new PrintStream(clientSocket.getOutputStream()); os.println("Enter your name."); name = is.readLine(); os.println("Welcome "+name+" to our chat room.\nEnter /quit to leave room"); for(int i=0; i<=9; i++) if (t[i]!=null && t[i]!=this) t[i].os.println("*** A new user "+name+" entered the chat room !!! ***" ); while (true) { line = is.readLine(); if(line.startsWith("/quit")) break; for(int i=0; i<=9; i++) if (t[i]!=null) t[i].os.println("<"+name+"> "+line); } for(int i=0; i<=9; i++) if (t[i]!=null && t[i]!=this) t[i].os.println("*** The user "+name+" is leaving the chat room !!! ***" ); os.println("*** Bye "+name+" ***"); for(int i=0; i<=9; i++) if (t[i]==this) t[i]=null; is.close(); os.close(); clientSocket.close(); } catch(IOException e){}; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -