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 the function of group chat with java

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

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of "how to achieve group chat function in java". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "how to achieve group chat function in java" can help you solve the problem.

1. Server

Package networkCoding; import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.IOException;import java.net.ServerSocket;import java.net.Socket;import java.util.concurrent.CopyOnWriteArrayList / * 1, specify port, use serverSocket to create server * 2, block to wait for connection to accept * 3, operate: input / output stream operation * 4, release resources * * 5, join container to achieve group chat * * / public class WeiHuShanChatRoomServer {private static CopyOnWriteArrayList all= new CopyOnWriteArrayList (); public static void main (String [] args) throws IOException {System.out.println ("- server") / 1, specify the port, and use serverSocket to create the server ServerSocket server= new ServerSocket (9999); / / 2, blocking waiting connection accept while (true) {Socket client=server.accept (); Chat chat= new Chat (client); / / handing it to container management all.add (chat); new Thread (chat) .start () }} static class Chat implements Runnable {private DataOutputStream dos; private DataInputStream dis; private Socket client; private boolean isRuning; private String name; public Chat (Socket client) {this.client=client; this.isRuning=true; try {this.dis = new DataInputStream (client.getInputStream ()) This.dos=new DataOutputStream (client.getOutputStream ()); this.name=receive (); this.send (this.name+, Welcome to Weihu Mountain); this.sendOthers (this.name+ "came to Weihu Mountain", true) } catch (IOException e) {/ / error releasing resource System.out.println ("= 1percent ="); this.release ();}} private String receive () {String data= ""; try {data= dis.readUTF () } catch (IOException e) {System.out.println ("= 2percent ="); / / this.release ();} return data;} / / Group chat private void send (String data) {try {dos.writeUTF (data) Dos.flush ();} catch (IOException e) {System.out.println ("= 3x ="); this.release ();}} private void sendOthers (String data,boolean isSys) {boolean isPrivate = data.startsWith ("@") If (isPrivate) {int index= data.indexOf (":"); String targetName=data.substring (1 focus index); String msg=data.substring (index+1) For (Chat chat: all) {if (chat.name.equals (targetName)) {System.out.println ("*" + chat.name+targetName); chat.send (this.name+ "whispers to you:" + msg) } else {for (Chat chat: all) {if (chat==this) {continue } else {if (isSys) {chat.send (data);} else {chat.send (this.name+ "says to everyone:" + data) } private void release () {this.isRuning=false; Utils.close (dis,dos,client); all.remove (this) SendOthers (this.name+ "left Weihu Mountain", true);} @ Override public void run () {while (isRuning) {String data = receive (); if (! data.equals (")) {sendOthers (data,false) } else {send ("cannot send empty message");}

2. Client

Package networkCoding; import java.io.BufferedOutputStream;import java.io.BufferedReader;import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.IOException;import java.io.InputStreamReader;import java.net.Socket;import java.net.UnknownHostException; / * 1, establish a connection and use socket to create the address port number of the client + server * 2, operation: input / output stream operation * 3, release resources * / public class WeiHuShanChatRoomClient {public static void main (String [] args) throws UnknownHostException, IOException {BufferedReader bf= new BufferedReader (new InputStreamReader (System.in)); System.out.println ("Please enter name"); String bfString = bf.readLine () / / 1, establish a connection and use socket to create the address and port number of the client + server Socket client = new Socket ("localhost", 9999); / / 2, operation: input / output stream operation new Thread (new Send (client,bfString)). Start (); new Thread (new Receive (client)). Start ();}}

(1) send wrapper class

Package networkCoding; import java.io.BufferedReader;import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.IOException;import java.io.InputStreamReader;import java.net.Socket; public class Send implements Runnable {private BufferedReader bf; private DataOutputStream dos; private Socket client; private boolean isRuning; private String name; public Send (Socket client,String name) {this.client=client; this.isRuning=true; this.name=name This.bf= new BufferedReader (new InputStreamReader (System.in)); try {this.dos=new DataOutputStream (client.getOutputStream ()); this.send (name);} catch (IOException e) {/ / error releasing the resource System.out.println ("= 4policies ="); this.release (); this.isRuning=false }} private void release () {this.isRuning=false; Utils.close (dos,client);} private void send (String data) {try {dos.writeUTF (data); dos.flush ();} catch (IOException e) {System.out.println ("= 5x ="); this.release () }} private String getString () {String dataString = "; try {dataString = this.bf.readLine ();} catch (IOException e) {System.out.println (" = 6 = "); this.release ();} return dataString } @ Override public void run () {/ / TODO Auto-generated method stub while (isRuning) {String data = getString (); if (! data.equals (")) {send (data);} else {/ / send (" cannot send empty message ") }

(2) receive wrapper class

Package networkCoding; import java.io.BufferedReader;import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.IOException;import java.io.InputStreamReader;import java.net.Socket; public class Receive implements Runnable {private DataInputStream dis; private Socket client; private boolean isRuning; public Receive (Socket client) {this.client=client; this.isRuning=true; try {this.dis = new DataInputStream (client.getInputStream ()) } catch (IOException e) {/ / error releasing resource System.out.println ("= 6 hours ="); this.release (); this.isRuning=false;}} private String receive () {String data= ""; try {data= dis.readUTF () } catch (IOException e) {System.out.println ("= 7 years ="); this.release ();} return data;} private void release () {this.isRuning=false; Utils.close (dis,client);} @ Override public void run () {while (isRuning) {String data = receive () If (! data.equals ("")) {System.out.println (data);} else {}

3. Tool class

Package networkCoding; import java.io.Closeable;import java.io.IOException; public class Utils {public static void main (String [] args) {} public static void close (Closeable...target) {for (Closeable obj: target) {try {if (nullroomroomobj) {obj.close () }} catch (IOException e) {/ / TODO Auto-generated catch block e.printStackTrace ();} this is the end of the introduction about "how to achieve group chat in java". Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

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