-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathServer.java
More file actions
117 lines (111 loc) · 2.69 KB
/
Copy pathServer.java
File metadata and controls
117 lines (111 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package main;
import java.io.*;
import java.net.*;
import java.sql.*;
import java.util.*;
public class Server {
static int num=1;
public static void main(String args[]) {
ServerSocket server = null;
Socket client = null;
while(true){
try {
server = new ServerSocket(4444); // 创建一个ServerSocket在端口4444监听客户请求
} catch (Exception e) {
System.out.println("Error:" + e);// 屏幕打印出错信息
System.exit(-1);
}
try {
client = server.accept(); // 使用accept()阻塞等待客户请求,有客户请求
// 到来则产生一个Socket对象
} catch (Exception e) {
System.out.println("接受请求失败!");
System.exit(-1);
}
System.out.println("Client"+Server.num+"登录!");
ServerThread st=new ServerThread(client);
Thread t=new Thread(st);
t.start();
try
{
server.close();
}
catch(IOException e)
{
System.out.println("关闭失败!");
}
num++;
}
}
}// 服务器端程序结束
class ServerThread implements Runnable//账号登录线程
{
private Socket client;
public ServerThread(Socket client)
{
this.client=client;
}
@Override
public void run() {
// TODO Auto-generated method stub
try{
BufferedReader is = new BufferedReader(new InputStreamReader(
client.getInputStream()));
// 由Socket对象得到输入流,并构造相应的BufferedReader对象
PrintWriter os = new PrintWriter(client.getOutputStream());
// 由Socket对象得到输出流,并构造PrintWriter对象
String id;//接收到的id
String pwd;//接收到的pwd
String duty;//接收到的duty
String ip=new String("localhost:1433");//数据库ip地址
Connection con=null;
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con=DriverManager.getConnection("jdbc:sqlserver://"+ip+"; DatabaseName=HospitalDB","sa","sa");
if(con!=null)
System.out.println("连接成功!");
else
System.out.println("连接失败!");
Statement st=con.createStatement();
while(!client.isClosed())
{
id=is.readLine();
pwd=is.readLine();
duty=is.readLine();
String sql="Select password,duty from Account where id='"+id+"'";
ResultSet rs=st.executeQuery(sql);
if(rs.next())
{
if(rs.getString("password").equals(pwd))
{
if(rs.getString("duty").equals(duty)){
os.println("succeed");
os.flush();
os.println(ip);
os.flush();
}
else{
os.println("您无此客户端登录权限!");
os.flush();
}
}
else
{
os.println("密码错误!");
os.flush();
}
}
else
{
os.println("账号错误!");
os.flush();
}
}
os.close(); // 关闭Socket输出流
is.close(); // 关闭Socket输入流
client.close(); // 关闭Socket
System.out.println("聊天结束!");
} catch (Exception e) {
System.out.println("Error:" + e);
}
}
}