This article shares the Java group chat function for your reference. The specific content is as follows
Java support for TCP protocol:
--> The java.net package defines two classes ServerSocket and Socket, which are used to implement bidirectional connections, respectively.
--> Client class defines the client
package com.dragon.java.tcpchat;import java.io.IOException;import java.net.Socket;import java.net.UnknownHostException;/** * Client* * @author Auser * */public class Client { public static void main(String args[]) throws UnknownHostException, IOException { Socket client = new Socket("192.168.1.188", 10000); // The thread that sends information through the piece new ClientSend(client).start(); // Create a thread that receives information new ClientReceive(client).start(); // Because you want to implement the chat function instead of sending information only once, neither the output stream nor the client can be closed. // client.shutdownOutput(); // client.close(); }}--> ClientSend class defines the thread on which the client sends information to the server
package com.dragon.java.tcpchat;import java.io.IOException;import java.io.PrintStream;import java.net.Socket;import java.util.Scanner;/** * Thread where the client sends information to the server* * @author Auser * */public class ClientSend extends Thread { private Scanner scanner; private Socket socket; public ClientSend(Socket socket) { this.socket = socket; } @Override public void run() { scanner = new Scanner(System.in); try { PrintStream ps = new PrintStream(socket.getOutputStream()); String line = ""; // Blocking sending information while ((line = scanner.nextLine()) != null) { ps.println(line); } } catch (IOException e) { e.printStackTrace(); } }}--> ClientReceive class defines the thread on which the client receives server information
package com.dragon.java.tcpchat;import java.io.BufferedReader;import java.io.InputStreamReader;import java.net.Socket;/** * Thread on the client receiving information* * @author Auser * */public class ClientReceive extends Thread { private Socket socket; public ClientReceive(Socket socket) { this.socket = socket; } @Override public void run() { try { BufferedReader br = new BufferedReader(new InputStreamReader( socket.getInputStream())); // Receive information by line String line = ""; while ((line = br.readLine()) != null) { System.out.println(line); } } catch (Exception e) { e.printStackTrace(); } }}--> Server class definition server
package com.dragon.java.tcpchat;import java.io.IOException;import java.net.ServerSocket;import java.net.Socket;import java.util.ArrayList;import java.util.List;/** * Server* * @author Auser * */public class Server { public static void main(String[] args) throws IOException, InterruptedException { List<Socket> list = new ArrayList<>(); // Create a server-side socket ServerSocket server = new ServerSocket(10000); while (true) { // Blocking method of receiving the client Socket socket = server.accept(); // Designing multiple threads that may add or delete the set, synchronized (list) { list.add(socket); } // Start a new thread to handle the communication of this client new HandleSocket(socket, list).start(); } // Because you don't know when the client sends information, the server must be turned on and cannot be turned off. }}--> HandleSocket class operates on clients connected to the server (upper and offline notifications, blocking and blackmailing, sending information to each client, etc.)
package com.dragon.java.tcpchat;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintStream;import java.net.InetAddress;import java.net.Socket;import java.util.List;/** * HandleSocket extends Thread { private Socket socket; private List<Socket> list; /** * Constructor* * @param socket * Currently connected client* @param list * Collection of stored connected clients*/ public HandleSocket(Socket socket, List<Socket> list) { this.socket = socket; this.list = list; } /** * Thread run method*/ @Override public void run() { InetAddress address = socket.getInetAddress(); // Get the address of the client connected to the server String ip = address.getHostAddress(); System.out.println(ip + "Let's go online!"); if (ip.equals("192.168.1.117")) { synchronized (list) { sendToAll(ip + "Blacklisted due to violation!"); list.remove(socket); } return; } try { BufferedReader br = new BufferedReader(new InputStreamReader( socket.getInputStream(), "gbk")); String line = ""; while ((line = br.readLine()) != null) { String msg = ip + ":" + line; System.out.println(msg); // Output to the server console// Send what this client says to all other clients sendToAll(msg); } } catch (IOException e) { // e.printStackTrace(); System.out.println(ip + "Offlined! "); synchronized (list) { list.remove(socket); } } } /** * Send information to all clients and remove the current socket * * @param msg * The information sent by */ private void sendToAll(String msg) { synchronized (list) { for (Socket s : list) { if (s != socket) { try { PrintStream ps = new PrintStream(s.getOutputStream()); ps.println(); } catch (IOException e) { e.printStackTrace(); } } } } } } } } } } --> Note: Because to enable the client to connect to the server, that is, the client must first find the server, so the server must be enabled first and then the server...
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.