📄 web158_com_server.java
字号:
JOptionPane.showMessageDialog(null, "加载时出现错误:\n\n"+okorerror);
System.exit(0);
}
}//end of web158_frame
//处理动作//////////////////////////////////////////////////////////////////////////
public void actionPerformed(ActionEvent e)
{
Object event=e.getSource();
//分钟秒钟
String minutes=""+m;
String seconds=""+n;
if(event==closetimer)
{
//c--;
//this.setSize(this.getWidth(),this.getHeight()-10);
//if(c<0)
//{
System.exit(0);
//}
}
if(event==timer)
{
if(event==timer)
{
n++;
web158_com_left.setText(" 服务器已运行: "+minutes+" 分钟 "+seconds+" 秒");
if(n>60)
{
n=0;
m++;
seconds=""+n;
}
}//end of timer
}
//处理开启服务器
if(event==web158_com_start)
{
if(web158_com_start.getActionCommand().equals("stop"))
{
try
{
closetimer.start();
}
catch(Exception esf)
{
System.out.println(esf.getMessage());
}
}//关闭服务
if(web158_com_start.getActionCommand().equals("start"))
{
if(!thread.isAlive())
{
thread=new Thread(this);
}
try
{
thread.start();
timer.start();
web158_com_start.setText(" 关闭服务 ");
web158_com_start.setActionCommand("stop");
//System.out.print("服务器已经启动");
}catch(Exception es)
{
System.out.print("线程已经启动");
}
}
}//end of web158_start
}//end of actionPerformed
/********************************************************
* *
* 窗口居中显示 *
* *
*********************************************************/
void web158_com_goCenter()
{
Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize(); //得到用户屏幕大小
Dimension frameSize=this.getSize(); //得到当前窗口大小 以上以定义:800*600
if(frameSize.width>screenSize.width)
{
frameSize.width=screenSize.width; //窗口长度大于屏幕时,令窗口长度等于屏幕长度
}
if(frameSize.height>screenSize.height) //与上同理,处理窗口高度
{
frameSize.height=screenSize.height; //窗口高度
}
this.setLocation((screenSize.width-frameSize.width)/2,(screenSize.height-frameSize.height)/2);//居中显示
}//end of web158_com_goCenter()
//线程设置
public void run()
{
try
{
ss = new ServerSocket(port); //port
System.out.print("服务器已经启动");
}
catch(IOException e)
{
out.println("出现错误:"+e.getMessage());
}
while(true) //这不算是死循环吗?
{
try
{
socket = ss.accept(); //接受连接
String RemoteIp = socket.getInetAddress().getHostAddress(); //ip
String RemotePort = ":"+socket.getLocalPort(); //port
System.out.println("客户端请求IP地址:"+RemoteIp+RemotePort);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
//in 为读取缓冲流,从客户端读取
String line = in.readLine(); //把in流转换成String
System.out.println("请求的命令是:"+line);
out = new PrintWriter(socket.getOutputStream(),true); //true指定是否缓冲
out.println("你的命令已经收到:"+line);
//如果是请求发卷
if(line.indexOf("#")!=-1)
{
//取考生姓名和考号
String [] strinfo=line.split("#");
//是否存在考生信息
String ifhaveStr=sHave(strinfo[1],strinfo[0]);
//System.out.print(ifhaveStr);
//显示
//System.out.println(strinfo[0]+strinfo[1]);
//System.out.println(strinfo[2]);
if(ifhaveStr.equals("noexit"))
{
//JOptionPane.showMessageDialog(null, "考生信息不存在");
out.println("noexit");
}
//已经考过试
else if(ifhaveStr.equals("true"))
{
//JOptionPane.showMessageDialog(null, "考生信息不存在");
out.println("examed");
}
//sql错误
else if(ifhaveStr.equals("error"))
{
//JOptionPane.showMessageDialog(null, "考生信息不存在");
out.println("error");
//System.out.print("error");
}
//服务器生成试卷失败
else if(ifhaveStr.equals("cerror"))
{
out.println("cerror");
}
//处理正常取试题
else if(strinfo[2].equals("web158")&&ifhaveStr.equals("send"))
{
out.println(strinfo[2]);
try
{
Thread.sleep(500);
serverSend("web158_com_config/temp.dll",sendport);
Thread.sleep(500);
//传送图片
String sendpic="";
File f=new File("web158_com_stupic/"+strinfo[1]+".gif");
if(f.exists())
{
sendpic="web158_com_stupic/"+strinfo[1]+".gif";
}
else
{
sendpic="web158_com_config/nophoto.dll";
}
serverSend(sendpic,sendport);
//更新已经取试卷,不能再取
updateStuGet(strinfo[1]);
//System.out.print(strinfo[1]);
//添加取试卷人数
if(txtinfo.getText().equals("还没有考生领取试卷"))
{
txtinfo.setText(strinfo[0]+" "+strinfo[1]);
}
else
{
txtinfo.setText(txtinfo.getText()+"\n"+strinfo[0]+" "+strinfo[1]);
}
//刷新统计信息
getNum++;
web158_com_b_1.setText("已经取卷: "+getNum+" 人 已经交卷: "+subNum+" 人");
}catch(Exception ef)
{
System.out.print(ef.getMessage());
}
}
//处理其它情况
else
{
out.println("othererror");
}
}//end of 请求发卷
//请求交///////////////////////////////////////////////////////////////////////////////////////////////////
else if(line.indexOf("¤")!=-1)
{
//取考生姓名和考号
String [] strinfo=line.split("¤");
//是否存在考生信息
System.out.println(strinfo[0]);
//如果成绩更新成功则接收文件
if(updateScore(strinfo[0],strinfo[2]).equals("true"))
{
//创建文件夹
String filebase="web158_answers/"+strinfo[1]+"("+strinfo[0]+")/";
File f=new File(filebase);
if(!f.exists())
{
f.mkdirs();
}//发出请求命令
out.println("oksend");
try
{
Thread.sleep(200);
serverReceive(filebase+"question.txt",sendport);
System.out.println("传完文本了");
Thread.sleep(100);
serverReceive(filebase+"answer.rar",sendport);
System.out.print("传完文件了");
out.println("receiveok");
updateSubmit(strinfo[0]);
//System.out.print(strinfo[1]);
//添加取试卷人数
if(txtinfo2.getText().equals("还没有人交卷"))
{
txtinfo2.setText(strinfo[1]+" "+strinfo[0]);
}
else
{
txtinfo2.setText(txtinfo2.getText()+"\n"+strinfo[1]+" "+strinfo[0]);
}
//刷新统计信息
subNum++;
web158_com_b_1.setText("已经取卷: "+getNum+" 人 已经交卷: "+subNum+" 人");
}catch(Exception ef)
{
System.out.println("接收文件出问题");
}
}
else
{
out.println("scoreerror");
}
}//end of 交卷
out.close();
in.close();
socket.close();
}
catch(Exception e){}
//处理不可达
finally
{
try
{
out.close();
in.close();
socket.close();
}
catch(Exception ef)
{out.print(ef.getMessage());}
}//end of finally
}//end of while
}//end of run
public void serverReceive(String f,int port) {
try {
ServerSocket serverSocket = new ServerSocket(port);
System.out.println("服务器启动!");
Socket socket = serverSocket.accept();
byte[] buffer = new byte[1024];
File file = new File(f);
int length = 0;
DataInputStream in = new DataInputStream(socket.getInputStream());
if(!file.exists())
file.createNewFile();
FileOutputStream fileOut = new FileOutputStream(file);
while((length=in.read(buffer))!= -1){
fileOut.write(buffer, 0, length);
}
fileOut.close();
socket.close();
serverSocket.close();
} catch (Exception e) {
// TODO: handle exception
System.out.println("错误:" + e);
}
}
//向客户端发送文件
public void serverSend(String f,int port) {
try {
ServerSocket serverSocket = new ServerSocket(port);
Socket socket = serverSocket.accept();
System.out.println("服务器启动!");
byte[] buffer = new byte[1024];
File file = new File(f);
int length = 0;
DataInputStream in = new DataInputStream(new FileInputStream(file));
DataOutputStream out = new DataOutputStream(socket
.getOutputStream());
while ((length = in.read(buffer)) != -1) {
out.write(buffer, 0, length);
}
out.flush();
out.close();
in.close();
System.out.println("文件传送完毕!!!");
socket.close();
serverSocket.close();
} catch (Exception e) {
System.out.println("出现错误:" + e.getMessage());
}
}
//是否存在考生信息
public String sHave(String id,String name)
{
ResultSet rs=null;
String sql="select * from stu_list where stuname='"+name+"' and stuid='"+id+"'";
boolean crok=false;
try
{
rs=db.getResult(sql);
if(rs.next())
{
System.out.print(rs.getString("examed"));
//如果考生已经取过试题
if(rs.getString("examed").equals("true"))
{
return "true";
}
//考生第一次取题,生成是库
else
{
//是否生成试卷成功
crok=createExam(id);
if(crok)
{
return "send";
}
else
{
return "cerror";
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -