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

📄 servermain.java

📁 实现PKI/CA的数字签名部分
💻 JAVA
字号:
package dssserver;

import java.awt.AWTEvent;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Vector;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;

public class ServerMain extends JFrame {
	
	//图形界面变量
	JPanel contentPane;
	JMenuBar jMenuBar1 = new JMenuBar();     //定义菜单栏
	JMenu jMenuserver = new JMenu();         //定义菜单
	JMenuItem jMenuserverExit = new JMenuItem();
	static JLabel statusBar = new JLabel();  //状态
	BorderLayout borderLayout1 = new BorderLayout();
	JPanel jPanel1 = new JPanel();
	BorderLayout borderLayout2 = new BorderLayout();
	JLabel jLabel1 = new JLabel();
	static java.awt.List jList1 = new java.awt.List(5);
	JScrollPane scrollpane=new JScrollPane(jList1);
	

	static ServerSocket server = null;    //建立服务器socket
	static int active_connects = 0;       //用来存储目前连接的客户数
	static Socket socket = null;          //用来存储一个套接字连接
	
	Vector clientName = new Vector();     // Vector 类 提供了可以可变数组的功能
	Vector clients = new Vector();

	public ServerMain() {
		enableEvents(AWTEvent.WINDOW_EVENT_MASK);
		try {
			JbInit();
		}
		catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	public  void NewSocket() {
		ServerMain frameSocket = new ServerMain();
		SwingUtilities.updateComponentTreeUI(frameSocket);
		frameSocket.show();
		statusBar.setText("服务器监听中...");
		try{
			server = new ServerSocket(8080);      
		}
		catch(IOException e){
			System.out.println("网络I/O错误:"+e);
		}
		int i = 0;	
		while(true){
			if(frameSocket.clients.size() < 100){
				try{
					socket=server.accept();                    //用来存储连接上的客户socket
					if(socket != null){
						System.out.println(socket+"连接\n");    //在控制台打印客户连接信息
					}
				}
				catch(IOException e){
					System.out.println("错误:"+e);
					statusBar.setText("错误:"+e);
				}
				do{    
					Client newc = new Client(socket,frameSocket);               //定义并实例化一个Client线程类,一个就对应一个客户连接	    
					if(newc.checkName()){                                       //调用checkName方法验证c的合法性
						active_connects++;
						statusBar.setText("目前有"+active_connects+"客户相连");
						ServerMain.jList1.addItem(newc.ip+"连接");  			//将客户socket信息写入list框
						newc.start();                                 		    //启动线程
					}
					else{
				        //如果名字不合法
						newc.send("ERROR:已经被占有,请再换一个试试");  
						frameSocket.disconnect(newc);
						i--;
					}
					i++;
				}while(i < frameSocket.clients.size());
			}
			else{
				try{
					Thread.sleep(200);
				}
				catch(InterruptedException e){
				}
			}
		}		
	}
	
	private void JbInit() throws Exception {
		contentPane = (JPanel) this.getContentPane();
		contentPane.setLayout(borderLayout1);
		this.setSize(new Dimension(219, 200));
		this.setTitle("数字签名服务器端");

		this.setJMenuBar(jMenuBar1);
		jMenuBar1.add(jMenuserver);
		jMenuserver.setText("服务");
		jMenuserver.add(jMenuserverExit);
		jMenuserverExit.setText("退出服务");
		jMenuserverExit.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				jMenuserverExit_actionPerformed(e);
			}
		});
		jPanel1.setLayout(borderLayout2);
		jLabel1.setText("连接客户信息:");
		statusBar.setText("目前的连接数为:0");
		contentPane.add(statusBar, BorderLayout.SOUTH);
		contentPane.add(jPanel1, BorderLayout.CENTER);
		jPanel1.add(jLabel1,  BorderLayout.NORTH);
		jPanel1.add(scrollpane, BorderLayout.CENTER);
	}
	
	public void jMenuserverExit_actionPerformed(ActionEvent e){   //退出服务
		sendClients("QUIT:SERVERSHUTDOWN");                  
		closeAll();                                               
		System.exit(0);
	}
	
	public synchronized void sendClients(String mg){   		//实现sendClients方法专用来向每个连接的客户端发送信息 
		for(int i=0;i<clients.size();i++){
			Client c=(Client)clients.elementAt(i);          //提起对象
			c.send(mg);
		}
	}
	
	public void notifyOthers(Client c, String info){
		String people=info+":"+c.name+":";
		sendClients(people);                                 //用sendClients方法向客户端发送信息
	}
	
	public void closeAll(){                           			  //实现关闭所有连接信息
		while(clients.size()>0){                                  //遍历clients数组删除所有连接客户信息
			Client c=(Client)clients.firstElement();              //第一个对象
			try{
				c.socket.close();
			}
			catch(IOException e){
				System.out.println("关闭Socket出错:"+e);
			}
			finally{
				clients.removeElement(c);                          //从clients数组中删除此客户的相关socket等信息
				clientName.removeElement(c.name);  
			}
		}
	}
	
	public synchronized void disconnect(Client c){                 //实现断开单个客户的方法
		try{ 
			jList1.addItem(c.ip+"断开连接");                         //在服务器端程序的list框中显示断开信息
			int connum = --ServerMain.active_connects;              //将连接数减1
			statusBar.setText("目前有"+connum+"客户相连");
			c.socket.close();
		}
		catch(IOException e){
			System.out.println("错误:"+e);
		}
		finally{
			clients.removeElement(c);                      //从clients数组中删除此客户的相关socket等信息
			clientName.removeElement(c.name);
			c.thstop();
		}
	}
}

⌨️ 快捷键说明

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