examserver.java

来自「这是个可以进行多项选择的系统」· Java 代码 · 共 159 行

JAVA
159
字号
package exam.web;


import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.HashSet;
import java.util.Set;



public class ExamServer {
	private ServerSocket ss;
	private StringBuffer students;
	private Set<Socket> clients;
	private boolean isStop;
	public ExamServer(){
		students=new StringBuffer("");
		clients=new HashSet<Socket>();
		isStop=false;
		new ControllStop(clients,this);
		try {
			ss=new ServerSocket(8888);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public void go(){
		while(true){
			try {
				Socket s=ss.accept();
				clients.add(s);
				new MyThread(s).start();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
	}
	
	class MyThread extends Thread{
		private Socket s;
		public MyThread(Socket s){
			this.s=s;
		}
		
		public void run(){
			while(s!=null){
				PrintWriter pw=null;
				BufferedReader br=null;
				
				String str;
				try {
					pw=new PrintWriter(new OutputStreamWriter(s.getOutputStream(),"UTF-8"));
					br=new BufferedReader(new InputStreamReader(s.getInputStream(),"UTF-8"));
					str = br.readLine();
					if(isStop){
						pw.println("isStop");
						pw.flush();
						continue;
					}
					if(str==null) continue;
					String[] strs=str.split(":");
					if(strs[0].equals("student")){
						BufferedReader br1=new BufferedReader(new InputStreamReader(new FileInputStream("student.txt"),"UTF-8"));
						String str1;
						boolean isExist=false;
						while((str1=br1.readLine())!=null){
							if(str1.equals(""))continue;
							String[] strs1=str1.split(":");
							System.out.println(students.toString());
							 if(strs[1].trim().equals(strs1[1])&&strs[2].equals(strs1[2])){
								 if(students.toString().contains(strs1[1])){
									 pw.println("cf");
									 pw.flush();
									 isExist=true;
									 break;
								 }
								students.append(strs1[1]);
								pw.println(str1);
								pw.flush();
								isExist=true;
								break;
							}
						}
						 if(!isExist){
							pw.println("");
							pw.flush();
						}
						
					}
					if(strs[0].equals("course")){
						BufferedReader br2=new BufferedReader(new InputStreamReader(new FileInputStream("course.txt"),"UTF-8"));
						String str2;
						while((str2=br2.readLine())!=null){
							pw.println(str2);
							pw.flush();
						}
						pw.println("");
						pw.flush();
					}
					if(strs[0].equals("paper")){
						BufferedReader br3=new BufferedReader(new InputStreamReader(new FileInputStream(strs[1]),"UTF-8"));
						String str3;
						while((str3=br3.readLine())!=null){
							if(str3.equals(""))continue;
							pw.println(str3);
							pw.flush();
						}
						pw.println("");
						pw.flush();
					}
					if(strs[0].equals("score")){
						Calendar now=new GregorianCalendar ();
						String str4=now.get(Calendar.YEAR)+(now.get(Calendar.MONTH+1))+now.get(Calendar.DAY_OF_MONTH)+"";
						FileOutputStream fos=new FileOutputStream(str4+".score",true);
						System.out.println(strs[1]+" "+strs[2]+" "+strs[3]+" "+strs[4]+" "+strs[5]);
						byte[] b=(strs[1]+" "+strs[2]+" "+strs[3]+" "+strs[4]+" "+strs[5]+"\n").getBytes("UTF-8");
						fos.write(b);
						fos.flush();
						fos.close();
					}
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				} catch(SocketException e){
					s=null;
				}catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	public static void main(String[] args){
		 ExamServer es=new ExamServer();
		es.go();
		
	}
//	public String getStudents(){
//		return students.toString();
//	}
	public boolean isStop() {
		return isStop;
	}
	public void setStop(boolean isStop) {
		this.isStop = isStop;
	}
}

⌨️ 快捷键说明

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