📄 server.java
字号:
import java.io.*;
import java.net.*;
import java.util.*;
public class Server {
public static void main(String args[]) {
ServerSocket server = null;
Socket sok = null;
while (true) {
try {
server = new ServerSocket(5000);
} catch (IOException e1) {
System.out.println("正在监听");
}
try {// 获得客户端IP
sok = server.accept();
InetAddress address = sok.getInetAddress();
System.out.println("客户端的IP:" + address);
} catch (IOException e) {
}
if (sok != null) {
new Server_thread(sok).start();
} else {
continue;
}
}
}
}
class FileName implements FilenameFilter // 实现过滤器对试题格式过滤取出试题
{
String str = null;
FileName(String s) {
str = "." + s;
}
public boolean accept(File dir, String name) {
return name.endsWith(str);
}
}
class Server_thread extends Thread {
int i = 0;
Socket socket = null;
File file = null;
DataOutputStream out = null;
DataInputStream in = null;
ReadTestquestion rtq = null;
boolean firsttimeclick = true;
Server_thread(Socket t) {
socket = t;
try {
in = new DataInputStream(socket.getInputStream()); // 由Socket对象得到输入流
out = new DataOutputStream(socket.getOutputStream()); // 由Socket对象得到输出流
} catch (IOException e) {
}
rtq = new ReadTestquestion();
}
public void run() {
while (true) {
String s = null;
try {
s = in.readUTF(); // 从输入流中读取操作的字节
if (s.startsWith("将试题文件添加到下拉列表中")) // 如果匹配到list_test_file就列出考试文件
{
File f1 = new File("");
File dir = new File(f1.getAbsolutePath(), ""); // 当前目录
// String 当前目录=System.getProperty("user.dir");
// File dir=new File(当前目录); //当前目录
FileName fileTxt = new FileName("txt"); // 在当前目录下过滤出所有txt文件
String fileName[] = dir.list(fileTxt); // 列出带过滤器的文件名清单
for (int i = 0; i < fileName.length; i++) {
out.writeUTF("试题文件@" + fileName[i]);
}
out.writeUTF("传送完毕@");
System.out.println("试题文件传送完毕");
} else if (s.startsWith("选择试题:")) {
String fileName = s.substring(s.indexOf(":") + 1);
System.out.println("客户端选择了试题"+fileName);
rtq.setFilename(fileName);
rtq.setTestOver(false);
firsttimeclick = true;
} else if (s.startsWith("读取下一题")) {
String contentTest = rtq.getTestContent();
if(contentTest.startsWith("endend")){
out.writeUTF("考试结束:");
}else{
out.writeUTF("文件内容:" + contentTest);
System.out.println("客户端读取下一题了");
if (firsttimeclick == true) // 继续答题显示倒计时
{
long time = rtq.getTime();
out.writeUTF("考试时间:" + time);
System.out.println("考试时间:" + time);
firsttimeclick = false;
}
if (rtq.getTestOver()) // 答题完毕
{
out.writeUTF("考试结束:");
}
}
} else if (s.startsWith("提交该题答案:")) // 提交答案
{
String answer = s.substring(s.indexOf(":") + 1);
System.out.println("客户端提交了答案"+answer);
rtq.setSelection(answer);
} else if (s.startsWith("查看成绩")) // 显示分数
{
int score = rtq.getScore();
String messages = rtq.getMessages();
out.writeUTF("你的成绩是:" + score + "\n" + messages);
System.out.println("客户端查看成绩了");
}
} catch (IOException e1) {
try {
socket.close();
} catch (Exception e2) {
}
break;
}
}
}
}
class ReadTestquestion {
String filename = "", // 存储文件名字
correctAnswer = "", // 存储正确答案
testContent = "", // 试题内容
selection = ""; // 存储选择的答案
int score = 0; // 得分
long time = 0; // 答题时间
boolean testOver = false; // 考试完毕
File f = null;
FileReader in = null;
BufferedReader br = null;
public void setFilename(String name) // 接受传到的文件名做相应处理
{
filename = name;
score = 0;
selection = "";
try {
if (in != null && br != null) {
in.close();
br.close();
}
f = new File(filename);
in = new FileReader(f);
br = new BufferedReader(in);
correctAnswer = (br.readLine()).trim();
//String time = (br.readLine()).trim();
//String time = line2.substring(line2.indexOf(":") + 1);
String temp = (br.readLine()).trim();
StringTokenizer token = new StringTokenizer(temp, ":"); // 设置标记以:分隔时间格式
int hour = Integer.parseInt(token.nextToken()); // 设置小时
int minute = Integer.parseInt(token.nextToken()); // 设置分钟
int second = Integer.parseInt(token.nextToken()); // 设置秒
time = 1000 * (second + minute * 60 + hour * 60 * 60);
} catch (Exception e) {
testContent = "没有选择试题";
}
}
public String getFilename() // 得到文件名
{
return filename;
}
public long getTime() // 获取倒计时时间
{
return time;
}
public void setTestOver(boolean b) // 完成考试
{
testOver = b;
}
public boolean getTestOver() // 完成考试
{
return testOver;
}
public String getTestContent() // 获取试题内容
{
try {
String s = null;
StringBuffer temp = new StringBuffer();
if (br != null) {
while ((s = br.readLine()) != null) {
if (s.startsWith("**")) // 匹配到**就break
break;
temp.append("\n" + s);
if (s.startsWith("endend")) // 出现endend就关闭流
{
in.close();
br.close();
testOver = true;
}
}
testContent = new String(temp);
} else {
testContent = new String("没有选择试题");
}
} catch (Exception e) {
testContent = "试题内容为空,考试结束!!";
}
return testContent;
}
public void setSelection(String s) {
selection = selection + s;
}
public int getScore() // 计算分数
{
score = 0;
int length1 = selection.length();
int length2 = correctAnswer.length();
int min = Math.min(length1, length2);
for (int i = 0; i < min; i++) {
try {
if (selection.charAt(i) == correctAnswer.charAt(i)) // 如果匹配到了相同的就执行++
score++;
} catch (StringIndexOutOfBoundsException e) {
i = 0;
}
}
return score;
}
public String getMessages() // 得到最终的答题信息
{
int length1 = selection.length();
int length2 = correctAnswer.length();
int length = Math.min(length1, length2);
String message = "正确答案:" + correctAnswer.substring(0, length) + "\n"
+ "你的回答:" + selection + "\n";
return message;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -