正文

JAVA C/S 考试模拟系统源代码(服务器端)2006-08-24 22:15:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/edwardguo/17869.html

分享到:

ServerThread.java

package ServerPart;
import java.io.*;
import java.net.*;
import java.util.*;

public class ServerThread {

 private Socket connectToClient;
 private DataOutputStream outToClient;
 private DataInputStream inFromClient;
 private ReadTestFile readTestFile;
 private String selectedAnswer = "";
 
 public ServerThread(Socket socket){
  connectToClient = socket;
  try{
   readTestFile = new ReadTestFile();
   inFromClient = new DataInputStream(connectToClient.getInputStream());
   outToClient = new DataOutputStream(connectToClient.getOutputStream());
  }
  catch(IOException e){
   
  }
  //start();
 }
 
 private void socketClosing(){
  try{
   inFromClient.close();
   outToClient.close();
   connectToClient.close();
  }
  catch(Exception e){
   System.out.println("关闭socket异常!");
  }
 }
 
 public void run(){
  String inStr = "";
  String outStr = "";
  while(true){
   try{
    //sleep(10);//线程休眠10毫秒;
    inStr = inFromClient.readUTF();
    if(inStr.startsWith("开始考试")){
     int time = readTestFile.getTestTime();
     outToClient.writeUTF("考试时间@"+time);
     System.out.println(inStr);
    }
    if(inStr.startsWith("下一题")){
     outStr = readTestFile.getTestQuestion();
     outToClient.writeUTF("下一题@"+outStr);
    }
    else if(inStr.startsWith("提交答案")){
     inStr = inStr.substring(inStr.indexOf("@")+1);
     selectedAnswer += inStr;
    }
    else if(inStr.startsWith("成绩")){
     int score = getTestScore();
     if(score>=60)
      outStr = "成绩:"+score+"\n祝贺你通过考试!";
     else
      outStr = "成绩:"+score+"\n你没有通过考试!";
     outToClient.writeUTF(outStr);
     saveTestScore("testscore.txt","成绩\t"+score);
     System.out.println(outStr);
    } 
   }
   catch(IOException e){
    socketClosing();
    System.out.println("与客户的连接中断");
    break;
   }
  }
 }
  
 private int getTestScore(){
  String correctAnswer = readTestFile.getCorrectAnswer();
  int n = 0,testScore = 0;
  int length1 = correctAnswer.length();
  int length2 = selectedAnswer.length();
  int min = Math.min(length1,length2);
  for(int i=0;i<min;i++){
   if(selectedAnswer.charAt(i)==correctAnswer.charAt(i))
    n++;
  }
  testScore=(int)(100.0*n/length1);
  return testScore;
 }
 
 private void saveTestScore(String file,String score){
  try{
   BufferedWriter scoreFile = new BufferedWriter(new FileWriter(file,true));
   String name = connectToClient.getInetAddress().toString();
   scoreFile.write(name+"\t");
   scoreFile.write(score);
   scoreFile.newLine();
   scoreFile.flush();
   scoreFile.close();
  }
  catch(IOException e){
   System.out.println("写成绩到文件发生异常!");
  }
  
 }
 public static void main(String[] args) {
  // TODO Auto-generated method stub

 }

}

 

ReadTestFile.java

package ServerPart;
import java.io.*;
public class ReadTestFile {

 private BufferedReader bufReader;
 public int testTime;
 private String correctAnswer;
 
 public ReadTestFile()throws IOException{
  bufReader = new BufferedReader(new FileReader("test.txt"));
  String s = bufReader.readLine();
  int i1 = s.indexOf('@');
  int i2 = s.indexOf("分钟");
  s = s.substring(i1+1,i2);
  testTime = Integer.parseInt(s)*60*1000;
  s = bufReader.readLine().trim();
  correctAnswer = s.substring(s.indexOf('@'+1));
 }
 public int getTestTime(){
  return testTime;
 }
 public String getCorrectAnswer(){
  return correctAnswer;
 } 
 public String getTestQuestion(){
  String testQuestion = "";
  try{
   StringBuffer temp = new StringBuffer();
   String s = "";
   if(bufReader!=null){
    while((s=bufReader.readLine())!=null){
     if(s.startsWith("*"))
      break;
     temp.append("\n"+s);
     if(s.startsWith("试题结束"));
      bufReader.close();
    }
   }
   testQuestion = temp.toString();
  }
  catch(Exception e){
   testQuestion = "试题结束";
  }
  return testQuestion;
 }
 public static void main(String args[]){
  
 }
}

 


MultiServer.java

package ServerPart;
import java.io.*;
import java.net.*;

public class MultiServer {
 
 public static void main(String[] args)throws IOException {
  System.out.println("建立并等待连接……");
  ServerSocket serverSocket = new ServerSocket(5500);
  Socket connectToClient = null;
  while(true){
   connectToClient = serverSocket.accept();
   new ServerThread(connectToClient);
  }

 }

}

 

test.txt

考试用时@1分钟
标准答案@DB
1.下面关于类的说法不正确的是( )。
A)类是Java语言中的一种复合数据类型。
B)类中包含数据变量和方法。
C)类是对所有具有一定共性的对象的抽象。
D)Java语言的类支持多继承。
************
试题结束

阅读(7960) | 评论(4)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

loading...
您需要登录后才能评论,请 登录 或者 注册