📄 server.java
字号:
package com.cnu.cie.olts.server;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowEvent;import java.io.IOException;import java.net.DatagramPacket;import java.net.DatagramSocket;import java.net.InetAddress;import java.net.ServerSocket;import java.net.Socket;import java.net.SocketAddress;import java.sql.Connection;import java.sql.ResultSet;import java.sql.Statement;import java.util.Vector;import javax.swing.BorderFactory;import javax.swing.ImageIcon;import javax.swing.JButton;import javax.swing.JLabel;import javax.swing.JMenu;import javax.swing.JMenuBar;import javax.swing.JMenuItem;import javax.swing.JOptionPane;import javax.swing.JScrollPane;import javax.swing.JTable;import javax.swing.JTextArea;import javax.swing.JTextPane;import javax.swing.JToolBar;import javax.swing.WindowConstants;import javax.swing.border.LineBorder;import javax.swing.table.DefaultTableModel;import javax.swing.table.TableModel;import javax.swing.SwingUtilities;/*** This code was edited or generated using CloudGarden's Jigloo* SWT/Swing GUI Builder, which is free for non-commercial* use. If Jigloo is being used commercially (ie, by a corporation,* company or business for any purpose whatever) then you* should purchase a license for each developer using Jigloo.* Please visit www.cloudgarden.com for details.* Use of Jigloo implies acceptance of these licensing terms.* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.*/public class Server extends javax.swing.JFrame { private JTable ActiveClientInfoTable; private JLabel loglabel; private JMenuItem aboutmenuitem; private JMenuItem exitmenuitem; private JScrollPane tableScrollPane; private JToolBar oltsToolBar; private JButton startbutton; private JButton stopbutton; private JButton helpbutton; private JMenuItem stopmenuitem; private JMenuItem startmenuitem; private JMenu fileMenu; private JMenu helpmenu; private JMenuBar serverMenuBar; private static JTextArea LogTextArea; private JLabel usersinfolabel; //以下为网络相关变量 //用vector向量数组存储连接客户变量 static Vector clients=new Vector(10); static ServerSocket server=null;//建立服务器socket static int active_connects=0;//用来存储目前连接的客户数 static Socket socket=null;//用来存储一个套接字连接 static DefaultTableModel model=null; /** * Auto-generated main method to display this JFrame */ public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { Server inst = new Server(); inst.setLocationRelativeTo(null); inst.setVisible(true); } }); } public Server() { super(); initGUI(); } //实现关闭服务器程序要进行的操作 protected void processWindowEvent(WindowEvent e) { super.processWindowEvent(e); if (e.getID() == WindowEvent.WINDOW_CLOSING) { exitmenuitemActionPerformed(null); } } /*以下实现各种方法*/ //用来监视连接信息,不断刷新clients数组并刷新客户端用户列表信息 /* 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方法向客户端发送信息 sendClients(people); }*/ //实现sendClients方法专用来向每个连接的客户端发送信息 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 void closeAll() { //遍历clients数组删除所有连接客户信息 while(clients.size()>0) { Client c=(Client)clients.firstElement(); try{ if(c.socket!=null&&!c.socket.isClosed()) c.socket.close(); } catch(IOException e) { System.out.println("Error:"+e); WriteToLog("Error:"+e+"\n"); } finally { clients.removeElement(c); } } try { if(server!=null&&!server.isClosed()){ server.close(); server=null; } } catch (IOException e) { // TODO Auto-generated catch block WriteToLog("Error:"+e+"\n"); e.printStackTrace(); } } //实现检查连接客户的socket信息是否合法 public static boolean legalCheck(Client newclient) { String sql="select username from user_info where username='"+newclient.name+ "' and password='"+newclient.password+"' and status='"+newclient.status+"'"; boolean returnvalue=false; try { ConnectionOLTS connect=new ConnectionOLTS(); Connection con=connect.getConnection(); Statement stmt=connect.getStatement(); ResultSet result=stmt.executeQuery(sql); if(result.next()) returnvalue=true; else returnvalue=false; ConnectionOLTS.closeConn(con); ConnectionOLTS.closeStatement(stmt); ConnectionOLTS.closeResultSet(result); } catch (Exception e) { // TODO Auto-generated catch block WriteToLog("ERR:"+e.getMessage()); e.printStackTrace(); returnvalue=false; } return returnvalue; } //实现检查连接客户的socket信息是否合法 public static boolean checkName(Client newclient) { for(int i=0;i<clients.size();i++) { Client c=(Client)clients.elementAt(i); if((c!=newclient)&&c.equals(newclient.name)) return false; } return(true); } //实现断开单个客户的方法 public static synchronized void WriteToLog(String msg) { LogTextArea.append(msg); } //实现断开单个客户的方法 public static synchronized void disconnect(Client c) { try { //在服务器端程序的LogTextArea框中显示断开信息 WriteToLog("与 "+c.name+" 断开连接\n"); //向客户发送断开连接信息 c.send(new StringBuffer("CLOSE")); c.socket.close(); } catch(IOException e) { System.out.println("Error:"+e); WriteToLog("Error:"+e+"\n"); } finally { //从clients数组中删除此客户的相关socket等信息 clients.removeElement(c); int index=usertablecontain(c); if(index!=-1) model.removeRow(index); } } //实现断开单个客户只socket关闭的方法 public static synchronized void socketdisconnect(Client c) { try { //在服务器端程序的LogTextArea框中显示断开信息 WriteToLog(c.name+"断开连接\n"); //向客户发送断开连接信息 c.send(new StringBuffer("CLOSE")); c.socket.close(); } catch(IOException e) { System.out.println("Error:"+e); WriteToLog("Error:"+e+"\n"); } finally { //从clients数组中删除此客户的相关socket等信息 clients.removeElement(c); } } private static synchronized int usertablecontain(Client c){ Vector tablevc=model.getDataVector(); String myinfo="["+c.name+", "+c.status+"]"; for(int i=0;i<tablevc.size();i++){ String user=tablevc.get(i).toString(); if(user.equals(myinfo)){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -