Server.java
package Chat;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.List;
public class Server {
private List<ServerThread> clients = null;
public static void main(String[] args) {
System.out.println("服务器已启动...");
new Server().startUp();
}
private void startUp() {
ServerSocket ss = null;
try {
ss = new ServerSocket(7788); // step1 创建ServerSocket对象
clients = new ArrayList<ServerThread>(); // step2 创建一个用于维护客户端线程的集合
while (true) { // step3 循环侦听每一个客户端的连接
Socket s = ss.accept();
Thread t = new Thread(new ServerThread(s)); // 没成功连接一个客户端,就单独开一个线程去维护这个客户端的读写操作
t.start();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (ss != null)
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
class ServerThread implements Runnable {
private Socket socket;
private BufferedReader in = null;
private PrintWriter out = null;
private String name = null;
public ServerThread(Socket s) {
this.socket = s;
try {
in = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(), true);
name = "[" + socket.getInetAddress().getHostAddress() + ":"
+ socket.getPort() + "]";
clients.add(this);
} catch (IOException e) {
e.printStackTrace();
}
}
public void run() {
getMsg();
}
// 群发消息(向所有客户端发消息)
public void sendMsg(String msg) {
for (ServerThread st : clients) {
st.out.println(msg);
}
}
// 读取客户端的消息
public void getMsg() {
String msg = null;
try {
while ((msg = in.readLine()) != null) {
if (msg.equalsIgnoreCase("quit")) {
this.out.println("disconnect");
stop();
break;
}
}
} catch (SocketException e) {
System.out.println(name + "非法退出!");
stop();
} catch (IOException e) {
e.printStackTrace();
}
}
// 当前线程资源释放
public void stop() {
sendMsg(name + "下线了!");
clients.remove(this);
}
}
}
Client.java
package Chat;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
public class Client {
private Socket s = null; // 用于连接服务器的socket
private BufferedReader in = null; // 用于接收服务器消息的阅读器
private PrintWriter out = null; // 用于向服务器发送消息的编写器
private BufferedReader sbr = null; // 用于从键盘读入数据的阅读器
private boolean flag = true; // 线程结束标志
public static void main(String[] args) {
new Client().startUp();
}
public void startUp() {
try {
s = new Socket("127.0.0.1", 7788);
in = new BufferedReader(new InputStreamReader(s.getInputStream()));
out = new PrintWriter(s.getOutputStream(), true);
sbr = new BufferedReader(new InputStreamReader(System.in));
new Thread(new ClientThread()).start();
sendMsg();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (s != null)
s.close();
if (in != null)
in.close();
if (out != null)
out.close();
if (sbr != null)
sbr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
// 向服务端不断的通过键盘输入并发送消息
private void sendMsg() {
String str = null;
while (flag) {
try {
str = sbr.readLine();
if (str == null)
break;
out.println(str);
} catch (IOException e) {
e.printStackTrace();
}
}
}
// 接收服务端发送过来的消息
private void getMsg() {
String str;
try {
str = this.in.readLine();
if (str == null)
return;
if (str.equalsIgnoreCase("discounnect"))
flag = false;
System.out.println(str);
} catch (IOException e) {
e.printStackTrace();
}
}
// 从服务端获取数据的线程(循环接收)
class ClientThread implements Runnable {
public void run() {
while (flag) {
getMsg();
}
}
}
}