Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize Multi-person chat Room with Java

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to achieve a multi-person chat room in Java". The content of the explanation in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to achieve a multi-person chat room in Java".

Source code

Tool class:

This class is used to close various streams.

Public class CloseUtil {public static void CloseAll (Closeable... Closeable) {for (Closeable c:closeable) {if (c! = null) {try {c.close ();} catch (IOException e) {e.printStackTrace ();}

Server:

The server creates a serverSocket object to listen to whether there is a tcp connection through the accept () method, while a collection of socket objects stores the connected objects into the List collection, and the server forwards the message.

/ / Server public class Server {/ / stores every connected client public static List list=new ArrayList (); public static void main (String [] args) throws Exception {/ / create ServerSocket object ServerSocket serverSocket = new ServerSocket (9999); while (true) {/ / connected client Socket client = serverSocket.accept (); System.out.println (client.getInetAddress () + "enter chat room"); MyChannel myChannel = new MyChannel (client) List.add (myChannel); new Thread (myChannel). Start ();}}

Message forwarding class:

A specific message forwarding implementation class that sends information to clients other than sending messages.

/ / method for information forwarding public class MyChannel implements Runnable {private DataInputStream dis; private DataOutputStream dos; private boolean flag=true; public MyChannel (Socket socket) {try {dis=new DataInputStream (socket.getInputStream ()); dos=new DataOutputStream (socket.getOutputStream ());} catch (IOException e) {flag=false; CloseUtil.CloseAll (dis,dos);}} / / method for receiving data private String receive () {String str= "; try {str= dis.readUTF ();} catch (IOException e) {flag=false CloseUtil.CloseAll (dis,dos); Server.list.remove (this);} return str;} / / method of sending data private void send (String str) {try {if (str! = null & & str.length ()! = 0) {dos.writeUTF (str); dos.flush ();}} catch (Exception exception) {flag=false; CloseUtil.CloseAll (dos,dis); Server.list.remove (this) }} / / method to forward messages private void sendToOther () {String str=this.receive (); List list = Server.list; for (MyChannel other:list) {if (other==list) {continue;// does not send messages to itself} / / send messages to other clients other.send (str);} @ Override public void run () {while (flag) {sendToOther ();}

Send message class:

Used to get data from the keyboard and send it out

Public class Send implements Runnable {/ / get data from the keyboard private BufferedReader br; private DataOutputStream dos; private boolean flag=true; public Send () {br=new BufferedReader (new InputStreamReader (System.in));} public Send (Socket socket) {this (); try {dos=new DataOutputStream (socket.getOutputStream ());} catch (Exception e) {flag=false; CloseUtil.CloseAll (dos,socket); e.printStackTrace ();}} private String getMessage () {String str= "; try {str=br.readLine () } catch (IOException e) {flag=false; CloseUtil.CloseAll (br);} return str;} private void send (String str) {try {dos.writeUTF (str); dos.flush ();} catch (IOException e) {flag=false; CloseUtil.CloseAll (dos); e.printStackTrace ();}} @ Override public void run () {while (flag) {this.send (getMessage ();}

Information receiving class:

Public class Receive implements Runnable {/ / accept data flow private DataInputStream dis; private boolean flag=true; public Receive (Socket socket) {try {dis = new DataInputStream (socket.getInputStream ());} catch (Exception e) {flag=false; CloseUtil.CloseAll (dis,socket);}} private String getMessage () {String str= "; try {str=dis.readUTF ();} catch (IOException e) {flag=false CloseUtil.CloseAll (dis); e.printStackTrace ();} return str;} @ Override public void run () {while (flag) {System.out.println (this.getMessage ());}

Client:

Public class client {public static void main (String [] args) throws Exception {Socket socket = new Socket (InetAddress.getLocalHost (), 9999); Send send = new Send (socket); Receive receive = new Receive (socket); new Thread (send). Start (); new Thread (receive). Start ();}}

Start the server first and then start the client

Thank you for your reading, the above is the content of "how to achieve a multi-person chat room in Java". After the study of this article, I believe you have a deeper understanding of how to achieve a multi-person chat room in Java, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report