📄 server.java
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.SwingUtilities;
import java.io.*;
import java.net.*;
import java.util.*;
public class Server
{
public static void main(String[] args)
{
boolean started = false;
ServerSocket server = null; //创建服务器套接字
Socket connection = null;
ObjectInputStream input = null;
ObjectOutputStream toClient;
Object obj = null;
ObjectOutputStream output; //PrintStream 为其他输出流添加了功能,使它们能够方便地打印各种数据值表示形式。
int number = 0;
String line;
String Ans[]=new String[100];
int score = 0;
try
{
server = new ServerSocket(8888);
}catch (BindException e) //BindException试图将套接字绑定到本地地址和端口时发生错误的情况下,抛出此异常。这些错误通常发生在端口正在使用中或无法分配所请求的本地地址时。
{
System.out.println("端口已被使用......");
}catch (IOException e)
{
e.printStackTrace();
}
System.out.println("==================================");
System.out.println("服务器启动成功............");
try
{
started = true;
while(started)
{
connection = server.accept();
System.out.println("==================================");
System.out.println("一客户端发送信息请求");
output = new ObjectOutputStream(connection.getOutputStream());
//toClient = new ObjectOutputStream(connection.getOutputStream());
input = new ObjectInputStream(connection.getInputStream());
try
{
obj = input.readObject();
if(obj.getClass().getName().equals("Customer"))
{
Customer clientMessage =(Customer)obj;
System.out.println("客户端发来的信息是:");
System.out.println("用户名:"+clientMessage.getCustName());
System.out.println("密码:"+clientMessage.getCustPassword()+"\n");
System.out.println("==================================");
if(clientMessage.getCustName().equals("37")&&clientMessage.getCustPassword().equals("37"))
{
output.writeObject( "请求通过,进入答题系统" );
output.flush();
System.out.println("客户已进入答题系统开始答题.......");
try
{
//获取考题的题目数目
FileReader reader1 = new FileReader("./" + "Qus.txt");
Scanner ques_num = new Scanner(reader1);
while(ques_num.hasNextLine())
{
ques_num.nextLine();
number++;
}
output.writeObject(""+number);
output.flush();
FileReader reader2 = new FileReader("./" + "Qus.txt");
Scanner ques = new Scanner(reader2);
while(ques.hasNextLine())
{
line = ques.nextLine();
output.writeObject(line);
output.flush();
}
FileReader reader3 = new FileReader("./" + "Opt.txt");
Scanner option = new Scanner(reader3);
while(option.hasNextLine())
{
line = option.nextLine();
output.writeObject(line);
output.flush();
}
System.out.println( " 学生最终答案为:" );
for(int j=0;j<number;j++)
{
Ans[j] = (String)input.readObject();
if(Ans[j].equals("1")) Ans[j]="A";
else if(Ans[j].equals("2")) Ans[j]="B";
else if(Ans[j].equals("3")) Ans[j]="C";
else if(Ans[j].equals("4")) Ans[j]="D";
else if(Ans[j].equals("0")) Ans[j]="null";
System.out.print(" "+Ans[j]);
}
System.out.println( "\n 标准答案为:" );
FileReader reader4 = new FileReader("./" + "Asw.txt");
Scanner answer = new Scanner(reader4);
int j = 0;
while(answer.hasNextLine())
{
line = answer.nextLine();
if(Ans[j].equals(line)) score ++;
j++;
System.out.print(" " + line);
}
System.out.println();
System.out.println("==================================");
System.out.print("\n 客户答对" + score + "题" + "\n");
System.out.println("==================================");
}catch(ClassNotFoundException e)
{
System.out.println(e);
}
}
else
{
output.writeObject("您的输入有误,请检查后重新输入!");
}
if(input.readObject().equals("客户答题结束,请返回客户作答情况!"))
{
//System.out.println("11111");
output.writeObject( "\n 您答对" + score + "题" + "\n" );
}
}
}catch ( ClassNotFoundException classNotFoundException )
{
System.out.println( "Unknown object type received" );
}
output.close();
input.close();
connection.close();
break;
}//END OF WHILE
}catch (EOFException e)
{
System.out.println("客户端关闭了");
}catch (BindException e)
{
System.out.println("端口使用中....");
}catch (IOException e)
{
e.printStackTrace();
}
}//END OF MAIN
}//END
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -