forked from DuGuQiuBai/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.java
More file actions
168 lines (156 loc) · 4.2 KB
/
Client.java
File metadata and controls
168 lines (156 loc) · 4.2 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package com.elient;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Client extends Thread{
public Socket c_socket ;
private Client_chatFrame c_chatFrame;
private Client_enterFrame c_enterFrame;
private Client_singleFrame c_singleFrame;
public DataInputStream dis = null;
public DataOutputStream dos = null;
private boolean flag_exit = false;
private int threadID;
public Map<String, Client_singleFrame> c_singleFrames;
public List<String> username_online;
public List<Integer> clientuserid;
public String username = null;
public String chat_re;
//getter, setter方法
public Client_chatFrame getC_chatFrame() {
return c_chatFrame;
}
public Client_singleFrame getC_singlFrame() {
return c_singleFrame;
}
public void setC_singlFrame(Client_singleFrame c_singlFrame) {
this.c_singleFrame = c_singlFrame;
}
public void setC_chatFrame(Client_chatFrame c_chatFrame) {
this.c_chatFrame = c_chatFrame;
}
public Client_enterFrame getC_enterFrame() {
return c_enterFrame;
}
public void setC_enterFrame(Client_enterFrame c_enterFrame) {
this.c_enterFrame = c_enterFrame;
}
public int getThreadID() {
return threadID;
}
public void setThreadID(int threadID) {
this.threadID = threadID;
}
public Client(){
c_singleFrames = new HashMap<String, Client_singleFrame>();
username_online = new ArrayList<String>();
clientuserid = new ArrayList<Integer>();
// signlechatuse = new ArrayList<String>();
}
public static void main(String[] args) {
Client client = new Client();
Client_enterFrame c_enterFrame = new Client_enterFrame(client);
client.setC_enterFrame(c_enterFrame);
c_enterFrame.setVisible(true);
}
public String login(String username, String hostIp, String hostPort) {
this.username = username;
String login_mess = null;
try {
c_socket = new Socket(hostIp, Integer.parseInt(hostPort));
} catch (NumberFormatException e) {
login_mess = "连接的服务器端口号port为整数,取值范围为:1024<port<65535";
return login_mess;
} catch (UnknownHostException e) {
login_mess = "主机地址错误";
return login_mess;
} catch (IOException e) {
login_mess = "连接服务其失败,请稍后再试";
return login_mess;
}
return "true";
}
public void showChatFrame(String username) {
getDataInit();
c_chatFrame = new Client_chatFrame(this,username);
c_chatFrame.setVisible(true);
flag_exit = true;
this.start();
}
private void getDataInit() {
try {
dis = new DataInputStream(c_socket.getInputStream());
dos = new DataOutputStream(c_socket.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
public void run() {
while(flag_exit){
try {
chat_re = dis.readUTF();
} catch (IOException e) {
flag_exit = false;
if(!chat_re.contains("@serverexit")){
chat_re = null;
}
}
if(chat_re != null){
if(chat_re.contains("@clientThread")){
int local = chat_re.indexOf("@clientThread");
setThreadID(Integer.parseInt(chat_re.substring(0, local)));
try {
dos.writeUTF(username + "@login" + getThreadID() + "@login");
} catch (IOException e) {
e.printStackTrace();
}
}else{
if(chat_re.contains("@userlist")){
c_chatFrame.setDisUsers(chat_re);
}else{
if(chat_re.contains("@chat")){
c_chatFrame.setDisMess(chat_re);
}else{
if(chat_re.contains("@serverexit")){
c_chatFrame.closeClient();
}else{
if(chat_re.contains("@single")){
c_chatFrame.setSingleFrame(chat_re);
}
}
}
}
}
}
}
}
public void transMess(String mess) {
try {
dos.writeUTF(username + "@chat" + getThreadID() + "@chat"+ mess + "@chat");
} catch (IOException e) {
e.printStackTrace();
}
}
public void exitChat() {
try {
dos.writeUTF(username + "@exit" + getThreadID() + "@exit");
flag_exit = false;
System.exit(0);
} catch (IOException e) {
e.printStackTrace();
}
}
public void exitLogin() {
System.exit(0);
}
public void exitClient() {
flag_exit = false;
System.exit(0);
}
}