1. 效果图
在前一篇文章中,利用多线程实现了客户端和服务端的一对一非阻塞通信功能,这篇文章在原来的基础上进一步改进,实现了多人简易群聊,由于没做gui,都是在控制台进行的,下面就是项目运行截图。
2.群聊功能分析
服务器不停的监听客户端的链接,一旦有一个客户端发起连接,就新建一个socket,开启一个线程,并添加保存到List中,服务段线程只负责不停的接受不同客户端发来的消息,同时转发每一条消息给所有客户端,当然,除了发送这条消息的客户端自身,为啥?没必要呗,自己发的,服务器再发回来给我有什么意思?
客户端当然要实现接受和发送数据,根据自己的想法,画了以下的实现思路图,另外附上项目结构。(根据个人理解,若有错误,欢迎指正)
2.1思路图与项目结构
至于客户端为什么也需要接受、发送两个线程,上一篇文章已经分析过了,可以参阅
3. 实现代码
3.1 服务器
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
public class Server {
public static List<Socket> socketList =new ArrayList<>();
private void start(){
try {
ServerSocket serverSocket = new ServerSocket(10086);
System.out.println("server is running at "+serverSocket.getLocalSocketAddress());
//监听客户端连接
while (true){
Socket socket = serverSocket.accept();
System.out.println("["+socket.getRemoteSocketAddress()+"]has connected to server");
socketList.add(socket);
//启动服务端聊天线程
new Thread(new ServerThread(socket)).start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new Server().start();//启动服务器
}
}
32 服务端socket线程
import java.io.*;
import java.net.Socket;
public class ServerThread implements Runnable {
private Socket socket = null;
BufferedReader br = null;
BufferedWriter bw = null;
public ServerThread(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
try {
br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
while (true){
String str = br.readLine();
if(str!=null){
System.out.println("[" + socket.getRemoteSocketAddress() + "]:" + str);
//循环遍历给每一个socket发送收到的消息,除了自身(即发送这条消息给服务端的socket)
for (Socket s : Server.socketList) {
if(!socket.equals(s)){
bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
bw.write("[" + socket.getPort()+ "]:" + str);
bw.write("\n");
bw.flush();
}
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
3.3接收线程
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
/**
* 接收线程
*/
public class RecevieThread implements Runnable {
Socket socket = null;
String him = null;
public RecevieThread (Socket socket,String him) {
this.socket = socket;
this.him = him;
}
@Override
public void run() {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
while (true) {
String str = br.readLine();
System.out.println(str);
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
3.4发送线程
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.util.Scanner;
/**
* 发送线程
*/
public class SendThread implements Runnable {
Socket socket = null;
String me = null;
public SendThread(Socket socket,String me) {
this.socket = socket;
this.me =me;
}
@Override
public void run() {
try {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
while (true){
System.out.println("["+me+"想说]:");
Scanner scanner = new Scanner(System.in);
bw.write(scanner.nextLine());
bw.write("\n");
bw.flush();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
3.5聊天客户端
import java.io.IOException;
import java.net.Socket;
public class Client {
public static void main(String[] args) {
Socket socket = null;
try {
socket = new Socket("127.0.0.1",10086);
System.out.println("客户端1启动成功");
} catch (IOException e) {
e.printStackTrace();
}
new Thread(new SendThread(socket,"用户1")).start();
new Thread(new RecevieThread(socket,"server")).start();
}
}
import java.io.IOException;
import java.net.Socket;
public class Client2 {
public static void main(String[] args) {
Socket socket = null;
try {
socket = new Socket("127.0.0.1",10086);
System.out.println("客户端2启动成功");
} catch (IOException e) {
e.printStackTrace();
}
new Thread(new SendThread(socket,"用户2")).start();
new Thread(new RecevieThread(socket,"server")).start();
}
}
mport java.io.IOException;
import java.net.Socket;
public class Client3 {
public static void main(String[] args) {
Socket socket = null;
try {
socket = new Socket("127.0.0.1",10086);
System.out.println("客户端3启动成功");
} catch (IOException e) {
e.printStackTrace();
}
new Thread(new SendThread(socket,"用户3")).start();
new Thread(new RecevieThread(socket,"server")).start();
}
}
后续将继续更新完善,继续学习啦