📄 servermanager.java
字号:
/*
* 创建日期 2006-4-18
* 作者:褚廷军
* 项目名称:学生考试系统;
* 项目要求:毕业设计;
* 模块名称:服务器端总控制器
* 模块功能:建立一组线程接收用户请求,并转发到相应功能模块;
*
* 4.27号增加新功能:统计登陆人数,和定义最大连接数
* 5.12号增加新功能,如果考生已经考过,则不能进入考场
*
* 未完成功能:1 如果用户考完后,让出空间让以后来的用户登陆;
*
* 更改所生成文件模板为
* 窗口 > 首选项 > Java > 代码生成 > 代码和注释
*/
package tserver;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerManager {
// 服务器端,为建立的每个连接创建一个服务器线程;
private static int counter = 0; //记录参加考试的人数;
private static int Maxcount =0; //此值为接收的管理员对此次参加考试人数的限定;
// private static int Timecount=0;
static String userstatus="";
static String linkstatus="";
static String serstatus="";
static String ucount;
public static String linkShow(String str) {
System.out.println(str);
return str;
}
public static String userShow(String str) {
System.out.println(str);
return str;
}
public static String serShow(String str) {
System.out.println(str);
return str;
}
public static String countShow(String count) {
return count;
}
public ServerManager() {
Thread td1 = new Thread() {
public void run() {
serstatus= "login thread start!\n";
serShow(serstatus);
ServerSocket ss1 = null;
Socket you = null;
int port1 = 8001;
try {
ss1 = new ServerSocket(port1);
serstatus = "listener the port:" + port1;
serShow(serstatus);
} catch (IOException e) {
e.printStackTrace();
}
while (true) {
try {
you = ss1.accept();
} catch (IOException e) {
e.printStackTrace();
}
if (you != null) {
counter++;
ucount = "现在参加考试的人数为:" + counter;
countShow(ucount);
if (counter < Maxcount) {
userstatus= "user's IP" + you.getInetAddress()
+ " connected " + port1 + "进行登陆";
userShow(userstatus);
// 第一个线程启动
new Server_login(you).start();
} else {
userstatus = "user's IP" + you.getInetAddress()
+ " connected " + port1 + "中断线程启动";
userShow(userstatus);
// 中断线程启动
new Maxcounter(you);
}
}
}
}
};
td1.start();
// 启动一个响应客户端读取试题的线程
Thread td2 = new Thread("two") {
public void run() {
serstatus= "thread 2 start!";
serShow(linkstatus);
ServerSocket ss2 = null;
int port2 = 8002;
try {
ss2 = new ServerSocket(port2);
linkstatus = "listener the port:" + port2;
linkShow(serstatus);
} catch (IOException e) {
e.printStackTrace();
}
while (true) {
Socket you2 = null;
try {
you2 = ss2.accept();
} catch (IOException e) {
e.printStackTrace();
}
if (you2 != null) {
userstatus = "user's IP" + you2.getInetAddress()
+ " connected " + port2 + "进行读题";
userShow(userstatus);
new Server_readTest(you2).start();
}}}
};
td2.start();
// 启动一个接收用户答应并存储的线程
Thread td3 = new Thread("three") {
ServerSocket ss3 = null;
int port3 = 8003;
public void run() {
serstatus="thread 3 start!";
serShow(serstatus);
try {
ss3 = new ServerSocket(port3);
linkstatus="listener the port:" + port3;
linkShow(linkstatus);
} catch (IOException e) {
e.printStackTrace();
}
while (true) {
Socket you3 = null;
try {
you3 = ss3.accept();
} catch (IOException e) {
e.printStackTrace();
}
if (you3 != null) {
userstatus="user's IP" + you3.getInetAddress()
+ " connected " + port3 + "提交答案";
userShow(userstatus);
// 第3个线程启动
new Server_readScore(you3).start();
}
}
}
};
td3.start();
// 启动一个用户查询的线程
Thread td4 = new Thread("four") {
ServerSocket ss4 = null;
int port4 = 8004;
public void run() {
serstatus="thread 4 start!";
serShow(serstatus);
try {
ss4 = new ServerSocket(port4);
linkstatus="listener the port:" + port4;
linkShow(linkstatus);
} catch (IOException e) {
e.printStackTrace();
}
while (true) {
Socket you4 = null;
try {
you4 = ss4.accept();
} catch (IOException e) {
e.printStackTrace();
}
if (you4 != null) {
userstatus="user's IP" + you4.getInetAddress()
+ " connected " + port4 + "进行查询";
userShow(userstatus);
// 第4个线程启动
new Server_userQuery(you4).start();
}
}
}
};
td4.start();
// 启动一个教师登陆的线程
Thread td5 = new Thread("five") {
ServerSocket ss5 = null;
int port5 = 8005;
public void run() {
serstatus="thread 5 start!";
serShow(serstatus);
try {
ss5 = new ServerSocket(port5);
linkstatus="listener the port:" + port5;
linkShow(linkstatus);
} catch (IOException e) {
e.printStackTrace();
}
while (true) {
Socket you5 = null;
try {
you5 = ss5.accept();
} catch (IOException e) {
e.printStackTrace();
}
if (you5 != null) {
userstatus="user's IP" + you5.getInetAddress()
+ " connected " + port5 + "进行查询";
userShow(userstatus);
// 第4个线程启动
new Server_techLogin(you5).start();
}
}
}
};
td5.start();
// 启动一个教师查询的线程
Thread td6 = new Thread("five") {
ServerSocket ss6 = null;
int port6 = 8006;
public void run() {
serstatus="thread 6 start!";
serShow(serstatus);
try {
ss6 = new ServerSocket(port6);
linkstatus="listener the port:" + port6;
linkShow(linkstatus);
} catch (IOException e) {
e.printStackTrace();
}
while (true) {
Socket you6 = null;
try {
you6 = ss6.accept();
} catch (IOException e) {
e.printStackTrace();
}
if (you6 != null) {
userstatus="teacher's IP" + you6.getInetAddress()
+ " connected " + port6 + "进行查询";
userShow(userstatus);
// 第4个线程启动
new Server_techQuery(you6).start();
}
}
}
};
td6.start();
}
public static int getMaxcount() {
return Maxcount;
}
public static void setMaxcount(int maxcount) {
Maxcount = maxcount;
}
public static int getCounter() {
return counter;
}
public static void setCounter(int counter) {
ServerManager.counter = counter;
}
// public static void main(String s[]) {
// new ServerManager();
//
// }
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -