您的位置:首页 > 教育 > 锐评 > Java实现一个简单的本地群聊。可以多开Client。

Java实现一个简单的本地群聊。可以多开Client。

2024/10/6 5:55:27 来源:https://blog.csdn.net/m0_70630103/article/details/142032086  浏览:    关键词:Java实现一个简单的本地群聊。可以多开Client。

网络编程也有趣的,Java中有对系统网络IO操作的封装包:Socket。现在我们在本地电脑(网络)用它来模拟一个简单的群聊功能,以便能更好地对网络编程进行深刻的理解。

"Client"去连接"Host",可同时多有多个"Client"去连接"Host";

在IDEA中,要多开一个程序(重复运行一个程序),可能需要关闭配置的"single instance only",或者打开"Allow parallel run"。

f3566c62fd8040ce8393b53efbfcb3a9.png

408ff2ddeb814f789b6cdd3b07c79bbe.png

即可实现应用多开(并行运行)。

这是一个服务主机端的模拟代码:

/*** @ Annotation:模拟服务器,实现群聊(群发)*/
public class Host {public static void main(String[] args) {try {System.out.println("Host start");List<Socket> onlineSocket = new ArrayList<>();//assign port:8898ServerSocket server = new ServerSocket(8898);while (true) {Socket socket = server.accept();//monitor port:8898System.out.println("a client:" + socket.getRemoteSocketAddress() + " online");new Thread(() -> {System.out.println("current thread id of host for new client is:" + Thread.currentThread().getId());try {InputStream is = socket.getInputStream();DataInputStream dis = new DataInputStream(is);while (true) {try {String content = dis.readUTF(dis);System.out.println(content);//static contentfor (Socket oc : onlineSocket) {OutputStream os = oc.getOutputStream();DataOutputStream dos = new DataOutputStream(os);dos.writeUTF(content);dos.flush();}} catch (Exception e) {System.out.println("a client:" + socket.getRemoteSocketAddress() + "offline");onlineSocket.remove(socket);dis.close();socket.close();break;}}} catch (Exception e) {e.printStackTrace();}}).start();onlineSocket.add(socket);}} catch (Exception e) {e.printStackTrace();}}
}

这是一个客户端的模拟代码: 

/*** @ Author Jam·Li* @ Date 2024/9/8 19:13* @ version 1.0.0* @ Annotation:模拟客户端,可多开*/
public class Client {public static void main(String[] args) {try {//Socket client = new Socket(InetAddress.getLocalHost(), 8898);Socket client = new Socket("127.0.0.1", 8898);new Thread(()->{try {InputStream is = client.getInputStream();DataInputStream dis = new DataInputStream(is);while (true){try {String msg = dis.readUTF(dis);System.out.println(msg);} catch (Exception e) {dis.close();client.close();System.out.println("I offline: "+client.getRemoteSocketAddress()+" now.");break;}}} catch (Exception e) {e.printStackTrace();}}).start();OutputStream os = client.getOutputStream();DataOutputStream dos = new DataOutputStream(os);Scanner scanner = new Scanner(System.in);while (true){System.out.println("Input Content Please");String msg = scanner.nextLine();dos.writeUTF(msg);dos.flush();System.out.println("Sent.");if (msg.equals("Bye")){client.close();dos.close();System.out.println("See you~");break;}}} catch (Exception e) {e.printStackTrace();}}
}

 先打开服务主机端:

9524d445e4954b708dd0ae747c9e2cd5.png

再打开客户端:

 9119efaab13d4f25b023fb60e8d5f915.png

多开客户端: 

662395c30dcb45a6939faea3cfb9e482.png

 5db471e848d64463822f6739b0f4ee63.png

81ca533894174bddb003eba5f0be61db.png

b1e2105edd3040a5aed250a406a2b235.png

6e7d82d0962941eb9e898b8cad0dcd74.png

27bfb0f2d8f948549fe8f70456e07a11.png

1568975b974648d2aaab38b742677228.png296c9c7f5ea14200a651bc0e1e87e82c.png 

如果此文对有帮助,请一键三连,点赞收藏加关注!作者后续会更新更多高质量文章哦!谢谢! 

 

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com