多人登录验证

多人登录验证

请求响应模式下多人验证模式

服务器:

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
/**
* 服务器(双向)多人登录:
* 1.指定端口,使用ServerSocket创建服务器
* 2.阻塞式等待链接 accept 返回一个Socket
* 3.操作:输入输出流
* 4.释放资源
*/
public class TCP_MultiWayServer {
public static void main(String[] args) throws Exception {
System.out.println("-------Server-------");
// 1.指定端口,使用ServerSocket创建服务器
ServerSocket SS = new ServerSocket(6666);
// 2.阻塞式等待链接 accept
while (true) {
Socket Client = SS.accept();
new Thread(new Server(Client)).start();
System.out.println("客户端连接已建立");
}
}
}
class Server implements Runnable {
private Socket Client;
// 3.操作:输入输出流
private DataInputStream dis;
private DataOutputStream out;
private String name = "";
private String pwd = "";
private String st;

Server(Socket client) {
Client = client;
try {
dis = new DataInputStream(Client.getInputStream());
out = new DataOutputStream(Client.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
try {
Client.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
@Override
public void run() {
try {
st = dis.readUTF();
String[] data = st.split("&");
for (String b : data) {
String[] c = b.split("=");
if (c[0].equals("name")) {
name = c[1];
} else {
pwd = c[1];
}
}
SendandReceive();
// 4.释放资源
dis.close();
out.close();
Client.close();
} catch (IOException e) {
e.printStackTrace();
}

}
public void SendandReceive() {
try {
if (name.equals("123") && pwd.equals("456")) {
out.writeUTF("登陆成功");
} else {
out.writeUTF("用户名或密码错误");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

客户端:

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
/**
* 客户端(双向)多人登录:
* 1.建立连接:使用Socket创建客户端:服务器地址和端口
* 2.操作:输入输出流
* 3.释放资源
*/
public class TCP_MultiWayClient {
public static void main(String[] args) throws Exception {
System.out.println("-------Client-------");
//1.建立连接:使用Socket创建客户端:服务器地址和端口
Socket S=new Socket("localhost",6666);
new Client(S).in();
}
}
class Client{
private Socket client;
public Client(Socket client) {
this.client = client;
}
private BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
private String name;
private String pwd;
public void in() {
try {
System.out.println("请输入姓名:");
name=bf.readLine();
System.out.println("请输入密码:");
pwd=bf.readLine();
SendandReceive();
} catch (IOException e) {
e.printStackTrace();
}
}
public void SendandReceive() {
// 2.操作:输入输出流
DataOutputStream out;
try {
out = new DataOutputStream(client.getOutputStream());
out.writeUTF("name="+name+"&"+"pwd="+pwd);
out.flush();
DataInputStream dis=new DataInputStream(client.getInputStream());
String sss=dis.readUTF();
System.out.println(sss);
// 3.释放资源
dis.close();
out.close();
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
原创技术分享,您的支持将鼓励我继续创作
0%