-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.java
More file actions
141 lines (125 loc) · 3.75 KB
/
Client.java
File metadata and controls
141 lines (125 loc) · 3.75 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import org.json.simple.JSONObject;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.Scanner;
public class Client {
private static Socket socket;
public static boolean connection_state = false;
public static void main(String[] args){
while (!connection_state) {
connect();
try {
Thread.sleep(3000);
}catch (Exception e){
e.printStackTrace();
}
}
}
private static void connect(){
try {
socket = new Socket("127.0.0.1", 9999);
connection_state = true;
ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
new Thread(new Client_listen(socket,ois)).start();
new Thread(new Client_send(socket,oos)).start();
new Thread(new Client_heart(socket,oos)).start();
}catch (Exception e){
e.printStackTrace();
connection_state = false;
}
}
public static void reconnect(){
while (!connection_state){
System.out.println("正在尝试重新链接.....");
connect();
try {
Thread.sleep(3000);
}catch (Exception e){
e.printStackTrace();
}
}
}
}
class Client_listen implements Runnable{
private Socket socket;
private ObjectInputStream ois;
Client_listen(Socket socket,ObjectInputStream ois){
this.socket = socket;
this.ois = ois;
}
@Override
public void run() {
try {
while (true){
System.out.println(ois.readObject());
}
}catch (Exception e){
e.printStackTrace();
}
}
}
class Client_send implements Runnable{
private Socket socket;
private ObjectOutputStream oos;
Client_send(Socket socket, ObjectOutputStream oos){
this.socket = socket;
this.oos = oos;
}
@Override
public void run() {
try {
Scanner scanner = new Scanner(System.in);
while (true){
System.out.print("请输入你要发送的信息:");
String string = scanner.nextLine();
JSONObject object = new JSONObject();
object.put("type","chat");
object.put("msg",string);
oos.writeObject(object);
oos.flush();
}
}catch (Exception e){
e.printStackTrace();
try {
socket.close();
Client.connection_state = false;
Client.reconnect();
}catch (Exception ee){
ee.printStackTrace();
}
}
}
}
class Client_heart implements Runnable{
private Socket socket;
private ObjectOutputStream oos;
Client_heart(Socket socket, ObjectOutputStream oos){
this.socket = socket;
this.oos = oos;
}
@Override
public void run() {
try {
System.out.println("心跳包线程已启动...");
while (true){
Thread.sleep(5000);
JSONObject object = new JSONObject();
object.put("type","heart");
object.put("msg","心跳包");
oos.writeObject(object);
oos.flush();
}
}catch (Exception e){
e.printStackTrace();
try {
socket.close();
Client.connection_state = false;
Client.reconnect();
}catch (Exception ee){
ee.printStackTrace();
}
}
}
}