In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly shows you "how to use Java multithreading to achieve multi-person chat room", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to use Java multithreading to achieve multi-person chat room" this article.
Client code:
Package netProgram;import java.io.IOException;import java.net.Socket;import java.net.SocketAddress;public class Client implements ScreenInputInterface {private SocketHandler socketHandler; public void start () {new Thread (new ScreenInputRunIns (this)) .start (); try {socketHandler = new SocketHandler (new Socket ("127.0.1", 666), null); new Thread (socketHandler) .start () } catch (IOException e) {e.printStackTrace ();} @ Override public void send (String msg) {socketHandler.sendMsg (msg);} / * @ Override public void send (String msg, SocketAddress address) {socketHandler.sendMsg (msg);} * /}
Server code:
Package netProgram;import java.io.IOException;import java.net.ServerSocket;import java.net.SocketAddress;import java.util.ArrayList;import java.util.List;public class Server implements ScreenInputInterface {private List clients = new ArrayList (); private ServerSocket serverSocket; public void start () {/ / new Thread (new ScreenInputRunIns (this)) .start (); try {serverSocket = new ServerSocket While (true) {SocketHandler socketHandler = new SocketHandler (serverSocket.accept (), this); clients.add (socketHandler); System.out.println (socketHandler.getSocket (). GetRemoteSocketAddress () + "enter chat system"); new Thread (socketHandler). Start ();} catch (IOException e) {e.printStackTrace () } @ Override public void send (String msg) {for (SocketHandler s:clients) {s.sendMsg (msg);}} / * @ Override public void send (String msg, SocketAddress address) {for (SocketHandler s:clients) {if (s.getSocket (). GetRemoteSocketAddress (). Equals (address)) {continue } s.sendMsg (msg);} * / public static void main (String [] args) {new Server () .start ();}}
Screen input information interface:
Package netProgram;import java.net.SocketAddress;public interface ScreenInputInterface {void send (String msg); / / void send (String msg, SocketAddress address);}
The thread object used for the creation of input information for each port:
Package netProgram;import java.util.Scanner;public class ScreenInputRunIns implements Runnable {private ScreenInputInterface screenInputInterface; private Scanner input = new Scanner (System.in); public ScreenInputRunIns (ScreenInputInterface screenInputInterface) {this.screenInputInterface = screenInputInterface;} @ Override public void run () {/ / screen live input while (true) {String msg = input.nextLine (); / / screenInputInterface.send (msg,null) ScreenInputInterface.send (msg);}
Socket processor (used to transmit data: actually sends information to the output stream and monitors the input of each port in real time and is responsible for display):
Package netProgram;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintWriter;import java.net.Socket;public class SocketHandler implements Runnable {private String clientName; private Socket socket; private PrintWriter writer; private Server server; public SocketHandler (Socket socket,Server server) throws IOException {this.socket = socket; this.server = server; clientName = ""; writer = new PrintWriter (this.socket.getOutputStream ()) } public Socket getSocket () {return socket;} public void sendMsg (String msg) {if (serverware null) writer.println (msg); else writer.println (clientName+ ":" + msg); writer.flush () } @ Override public void run () {while (true) {/ / listen for input try {BufferedReader reader = new BufferedReader (new InputStreamReader (socket.getInputStream (); String receiveMsg = null; System.out.println () If ((receiveMsg = reader.readLine ())! = receiveMsg.equals (")) {if (serverware null) {/ / server.send (receiveMsg,socket.getRemoteSocketAddress ()); server.send (receiveMsg);} else {System.out.println (receiveMsg) } catch (IOException e) {e.printStackTrace ();}
Client code for each port that is responsible for booting:
Package netProgram;public class ClientLinked {public static void main (String [] args) {new Client (). Start ();}} package netProgram;public class ClientLinked1 {public static void main (String [] args) {new Client (). Start ();}} package netProgram;public class ClientLinked2 {public static void main (String [] args) {new Client (). Start ();}}
The server startup code is in the main method of the server class.
The above is all the contents of the article "how to use Java multithreading to implement a multi-person chat room". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.