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

📄 serverpool.java~1~

📁 试题库管理系统 该系统所包含的子系统有:用户管理子系统、课程管理子系统、习题管理子系统和试卷库管理子系统。而用户管理子系统下分的模块有:添加用户、删除用户和修改用户信息;课程管理子系统下分的模块有:创
💻 JAVA~1~
📖 第 1 页 / 共 2 页
字号:
package server.pool;
import java.io.*;
import java.net.*;
import java.sql.*;
import java.util.concurrent.*;
import java.util.Vector;
import java.util.Date;
/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2008</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */
public class ServerPool  implements Runnable{
    private int port=8000;
    private Vector userContainer=new Vector();
    private Thread acceptClient=null;
    private boolean closed=false;
    private ServerSocket serverSocket;
    private ExecutorService executorService;
    private final int POOL_SIZE=4;
    /**
     * 默认构造函数
     */
    public ServerPool() {
    }
    /**
     * 启动服务器函数
     * @throws IOException
     */
    public void startServer()throws IOException{
        closed=false;
        serverSocket = new ServerSocket(port);
        executorService=Executors.newFixedThreadPool(POOL_SIZE);
        acceptClient=new Thread(this);
        acceptClient.setDaemon(true);
        acceptClient.start();
        System.out.println("服务器已启动...");
    }
    /**
     * 关闭服务器函数
     * @throws IOException
     */
    public void closeServer()throws IOException{
         closed=true;
         acceptClient=null;
         userContainer.clear();
        executorService.shutdown();
        serverSocket.close();
        serverSocket=null;
        executorService=null;
        System.out.println("服务器已关闭...");
    }
    /**
     * 实现Runnable接口,用来接收客户端的连接
     */
    public void run(){
        System.out.println("守护进程已运行...");
        while(!closed){
        try {
          Socket   socket = serverSocket.accept();
            executorService.execute(new WorkThread(socket));
             System.out.println("服务器已接收到客户端...");
        } catch (IOException ex) {
        }
        }
        System.out.println("守护进程已关闭...");
    }
    /**
     * 内部类,用来创建工作线程
     * @author not attributable
     * @version 1.0
     */
  class WorkThread implements Runnable{
    Socket socket=null;
    BufferedReader bufin;
    PrintWriter prout;
    DataBaseConnection DBPool;
    Connection conn;
    String id;
    /**
     * 构造函数
     * @param socket Socket
     */
    public WorkThread(Socket socket) {
        this.socket=socket;
    try {
        bufin = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        prout = new PrintWriter(socket.getOutputStream());
    } catch (IOException ex) {
        System.out.println("读取流失败");
    }
      DBPool=DataBaseConnection.getInstance();
      conn=DBPool.getConnection();
    }
    /**
     * 实现Runnable接口,服务器的主要工作线程
     */
    public void run(){
        String cmd="";
     while(!closed){       //服务器未关闭
            try {
              cmd=bufin.readLine();    //接收客户端命令
            } catch (IOException ex) {
              cmd="";
              System.err.println(ex.getMessage());
              break;
            }
            if(cmd.equals("close")){      //关闭连接命令
             System.out.println(cmd);     //测试用
              break;
            }
            else if(cmd.equals("read")){    //读取习题命令
               System.out.println(cmd);    //测试用
                 send();
            }
            else if(cmd.equals("write")){    //修改、添加命令
                System.out.println(cmd);     //测试用
                    receive();
                }
            else if(cmd.equals("login")){    //登陆命令
                   System.out.println(cmd);    //测试用
                   login();
                }
            else if(cmd.equals("load")){    //下载试卷命令
                 System.out.println(cmd);    //测试用
                    load();
                }
                else if(cmd.equals("transfer")||cmd.equals("savetest")){     //上传试卷命令
                    System.out.println(cmd);     //测试用
                    transfer();
                }
                else if(cmd.equals("readCourse")){
                    System.out.println(cmd);     //测试用
                    sendCourse();
                }
                try {
                     Thread.sleep(5000);
                     System.out.println("sleep");   //测试用
                } catch (InterruptedException ex2) {
                  System.err.println(ex2.getMessage());
                }
      }
        try{
            userContainer.remove(id);    //从userContainer中移除该用户ID
            bufin.close();      //关闭输入流
            prout.close();      //关闭输出流
            DBPool.freeConnection(conn);   //释放数据库连接
            socket.close();     //关闭socket连接
      }catch(IOException ex){
          System.err.println(ex.getMessage());
        }
    }
    /**
     * 响应客户端的读取习题命令
     */
    public void send(){
        String sql=null;
        ResultSet rt1;
        try {
            sql = bufin.readLine();       //接收客户端的SQL语句
            Statement st = conn.createStatement();
            rt1 = st.executeQuery(sql);    //查询数据库
            while (rt1.next()) {
                prout.println(rt1.getString("id"));
                prout.println(rt1.getString("course_name"));
                prout.println(rt1.getString("type"));
                prout.println(rt1.getString("text"));
                prout.println("over");      //提示客户端习题内容发送完毕
                prout.println(rt1.getString("answer"));
                prout.println("over");       //提示客户端习题答案发送完毕
                prout.println(rt1.getString("chapter"));
                prout.println(rt1.getString("section"));
                prout.println(rt1.getString("difficulty"));
                prout.flush();
            }
            rt1.close();    //关闭结果集
            st.close();     //关闭 Statement
        } catch (IOException ex) {
            System.err.println(ex.toString());
        } catch (SQLException ex1) {
            System.err.println(ex1.toString());
        }finally{
            prout.println("End");        //提示客户端习题发送完毕
            prout.flush();
        }
    }
    public void sendCourse(){
         String  sql="select * from course;";
          try {
              Statement st = conn.createStatement();
              ResultSet rt1 = st.executeQuery(sql);
             while(rt1.next()){
                 prout.println(rt1.getString(1));
                 prout.println(rt1.getString(2));
                  prout.println(rt1.getString(3));
                   prout.println(rt1.getString(4));
                    prout.println(rt1.getString(5));
                     prout.println(rt1.getString(6));
                 prout.flush();
             }
            prout.println("End");        //提示客户端习题发送完毕
            prout.flush();
             rt1.close();
             st.close();
        } catch (SQLException ex1) {
            System.err.println(ex1.toString());
        }

⌨️ 快捷键说明

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