-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.java
More file actions
147 lines (125 loc) · 4.34 KB
/
Copy pathClient.java
File metadata and controls
147 lines (125 loc) · 4.34 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
package NetworkProgramming.SeniorTest;
import javax.swing.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.rmi.UnexpectedException;
class ClientThread implements Runnable {
//客户端负责的输入流
BufferedReader br = null;
public ClientThread(BufferedReader br) {
this.br = br;
}
@Override
public void run() {
//读取服务端输入流
try {
String line = null;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
try {
if (br != null) {
br.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
public class Client {
private static final int SERVER_PORT = 30000;
private Socket socket;
private PrintStream ps;
private BufferedReader brServer;
private BufferedReader brKey;
public void init() {
try {
//初始化键盘输入
brKey = new BufferedReader(new InputStreamReader(System.in));
socket = new Socket();
//连接到服务器端
socket.connect(new InetSocketAddress("127.0.0.1", SERVER_PORT), 10000);
//获取Socket输出和输出流
ps = new PrintStream(socket.getOutputStream());
brServer = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String tip = "";
while (true) {
String userName = JOptionPane.showInputDialog(tip + "输入用户名");
//增加协议字符发送信息
ps.println(CrazyitProtocol.USER_ROUND + userName + CrazyitProtocol.USER_ROUND);
//读取服务器的响应
String result = brServer.readLine();
if (result.equals(CrazyitProtocol.NAME_REP)) {//姓名重复,重新输入
tip = "用户名重复,请重新";
continue;
}
if (result.equals(CrazyitProtocol.LOGIN_SUCCESS)) {
//登陆成功,结束循环
break;
}
}
} catch (UnknownHostException uhe) {
System.out.println("找不到远程服务器,请确定服务器已经启动");
closeRs();
System.exit(1);
} catch (IOException ioe) {
System.out.println("网络异常,请重新登陆");
closeRs();
System.exit(1);
}
//启动
new Thread(new ClientThread(brServer)).start();
}
//读取键盘输入,发送服务端
private void readAndSend() {
String line = null;
try {
while ((line = brKey.readLine()) != null) {
//如果发送的信息里面有冒号,且以//开头,则认为发送私聊信息
if (line.indexOf(":") > 0 && line.startsWith("//")) {
line = line.substring(2);//去除//和:
ps.println(CrazyitProtocol.PRIVATE_ROUND +
line.split(":")[0] + CrazyitProtocol.SPLIYT_SIGN + line.split(":")[1] + CrazyitProtocol.PRIVATE_ROUND);
} else {//公共聊天
ps.println(CrazyitProtocol.MSG_ROUND + line + CrazyitProtocol.MSG_ROUND);
}
}
} catch (IOException ioe) {
System.out.println("网络通信异常!请重新登陆!");
closeRs();
System.exit(1);
}
}
private void closeRs() {
try {
if (brKey != null) {
brKey.close();
}
if (brServer != null) {
brServer.close();
}
if (ps != null) {
ps.close();
}
if (socket != null) {
socket.close();
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
public static void main(String[] args) {
Client client = new Client();
client.init();
client.readAndSend();
}
}