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

📄 examserver.java

📁 java实现的c/s模式的考试系统.比较简单
💻 JAVA
字号:
package exam_server;

import java.io.*;
import java.net.*;

import exam.model.Paper;
import exam.model.Question;
import exam.model.Student;

public class ExamServer {
	private ServerSocket ss;
	private String fileSrc;
	
	public ExamServer(){
		try {
			ss = new ServerSocket(8888);
			fileSrc = System.getProperty("user.dir")+"\\exam_server\\files\\";
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void go(){
		while(true){
			try {
				Socket s = ss.accept();
				new ExamServerThread(s).start();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	public static void main(String[] args) {
		new ExamServer().go();
	}
	
	class ExamServerThread extends Thread{
		private Socket s;
		
		public ExamServerThread(Socket s){
			this.s = s;
		}
		
		@Override
		public void run() {
			System.out.println(s.getInetAddress()+"链接到服务器");
			BufferedReader br = null;
			BufferedReader brf = null;
			
			try {
				br = new BufferedReader(new InputStreamReader(s.getInputStream()));
				String str = null;
				while(true){
					str = br.readLine();
					if(str != null){
						if(str.indexOf("%GET_STUDENT%")==0){
							String id = str.split(":")[1];
							String passwd = str.split(":")[2];
							System.out.println("server: " + s.getInetAddress() + " get student " + id);
							brf = new BufferedReader(new InputStreamReader(new FileInputStream(fileSrc + "student.cfg")));
							String stuInfo = null;
							boolean unvalid = true;
							while((stuInfo = brf.readLine()) != null){
								if(stuInfo.split(":")[0].equals(id)){
									if(stuInfo.split(":")[2].equals(passwd)){
										unvalid = false;
										sendObject(new Student(Integer.parseInt(id),stuInfo.split(":")[1]));
									}
									break;
								}
							}
							if(unvalid){
								sendObject(null);
							}
						}else if(str.indexOf("%GET_SUBJECT%")==0){
							System.out.println("server: " + s.getInetAddress() + " download subject list");
							String sbjs = "";
							brf = new BufferedReader(new InputStreamReader(new FileInputStream(fileSrc + "subject.cfg")));
							int i = 0;
							String strr = "";
							while((strr = brf.readLine()) != null){
								sbjs = sbjs + strr + ":";
								i++;
							}
							sendObject(sbjs);
						}else if(str.indexOf("%GET_PAPER%") == 0){
							String sbj = str.split(":")[1];
							String paperName = sbj.split("=")[0];
							brf = new BufferedReader(new InputStreamReader(new FileInputStream(fileSrc + sbj.split("=")[1])));
							int i = 0;
							int qnum = 0;
							char trueAnswer = '#';
							String sp = "";
							String title = "";
							String[] option = new String[4];
							Question[] questions = new Question[1000]; 
							for(int j=0;i<4;i++){
								option[j]=null;
							}
							for(int j=0;i<1000;i++){
								questions[j]=null;
							}
							while((sp = brf.readLine())!=null){
								if(i%5 == 0){
									title = sp;
									i = 0;
								}else{
									if(sp.indexOf("<T>")==0){
										trueAnswer = (char)(i+'A'-1);
										option[i - 1] = sp.split(">")[1];
									}else{
										option[i - 1] = sp;
									}
								}
								i++;
								if(i == 5){
									questions[qnum] = new Question(title,option,trueAnswer);
									qnum ++ ;
								}
							}
							Question[] temp = new Question[qnum];
							System.arraycopy(questions, 0, temp, 0, qnum);
							questions = temp;
							Paper paper = new Paper(paperName,questions);
							sendObject(paper);
							System.out.println("server: " + s.getInetAddress() +" download paper " + paper.getName());
						}else if(str.indexOf("%ADD_SCORE%") == 0){
							System.out.println("server: " + s.getInetAddress() + " add score" + str);
							String outputpath = fileSrc + str.split(":")[3] + ".sco";
							System.out.println( "score path "+outputpath);
							RandomAccessFile f = new RandomAccessFile(outputpath,"rw");
							f.seek(f.length());
							String strb = str.split(":")[1] + ":" + str.split(":")[2];
							f.writeBytes(strb + "\n");
							f.close();
							br.close();
							s.close();
							break;
						}
					}
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		private void sendObject(Object obj){
			try {
				ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream());
				oos.writeObject(obj);
				oos.flush();
//				oos.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}

⌨️ 快捷键说明

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