How to realize Multi-thread chat by Java Network programming
This article Xiaobian introduces in detail for you "Java network programming how to achieve multithreaded chat", the content is detailed, the steps are clear, the details are handled properly, I hope this "Java network programming how to achieve multithreaded chat" article can help you solve your doubts, following the editor's ideas slowly in-depth, together to learn new knowledge.
Solution:
Receive and distribute to two different threads
1. SendThread message thread
2. RecieveThread accept message thread
3. Once Server accepts the connection, it starts both sending and receiving threads.
4. Once Client has established a connection, it starts both sending and receiving threads.
Multithreaded chat
1 SendThread
Package socket;import java.io.DataOutputStream;import java.io.IOException;import java.io.OutputStream;import java.net.Socket;import java.util.Scanner;public class SendThread extends Thread {private Socket s; public SendThread (Socket s) {this.s = s;} @ Override public void run () {try {OutputStream os = s.getOutputStream (); DataOutputStream dos = new DataOutputStream (os) While (true) {Scanner sc = new Scanner (System.in); String str = sc.next (); dos.writeUTF (str);}} catch (IOException e) {e.printStackTrace ();}
2 RecieveThread
Package socket;import java.io.DataInputStream;import java.io.IOException;import java.io.InputStream;import java.net.Socket;public class RecieveThread extends Thread {private Socket s; public RecieveThread (Socket s) {this.s = s;} @ Override public void run () {InputStream is = null; try {is = s.getInputStream (); DataInputStream dis = new DataInputStream (is) While (true) {String msg = dis.readUTF (); System.out.println (msg);} catch (IOException e) {e.printStackTrace ();}
3 Server
Package socket;import java.io.IOException;import java.net.ServerSocket;import java.net.Socket;public class Server {public static void main (String [] args) {try {ServerSocket ss = new ServerSocket (8888); System.out.println ("listener port number: 8888"); Socket s = ss.accept (); new SendThread (s) .start () New RecieveThread (s) .start ();} catch (IOException e) {e.printStackTrace ();}
4 Client
Package socket;import java.io.IOException;import java.net.Socket;import java.net.UnknownHostException;public class Client {public static void main (String [] args) {try {Socket s = new Socket ("127.0.0.1", 8888); new SendThread (s). Start (); new RecieveThread (s). Start ();} catch (UnknownHostException e) {e.printStackTrace () } catch (IOException e) {e.printStackTrace ();} simple dialog box
Server
Package socket;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.DataInputStream;import java.io.DataOutputStream;import java.net.ServerSocket;import java.net.Socket;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JTextArea;import javax.swing.JTextField;public class GUIServer {public static void main (String [] args) throws Exception {JFrame f = new JFrame (); f.setTitle ("server") F.setSize (400,300); f.setLocation (100,200); f.setLayout (null); JButton b = new JButton ("send"); b.setBounds (10,10,80,30); f.add (b); final JTextField tf = new JTextField (); tf.setBounds (10,110,80,30); f.add (tf) Final JTextArea ta = new JTextArea (); ta.setBounds (110dint 10,200,300); f.add (ta); f.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); f.setVisible (true); ServerSocket ss = new ServerSocket (8888); System.out.println ("listenning on port:8888"); final Socket s = ss.accept () New Thread () {public void run () {while (true) {try {DataInputStream dis = new DataInputStream (s.getInputStream ()); String text = dis.readUTF () Ta.append (text+ "\ r\ n");} catch (Exception e) {e.printStackTrace ();} .start () B.addActionListener (new ActionListener () {@ Override public void actionPerformed (ActionEvent e) {String text = tf.getText (); ta.append (text+ "\ r\ n"); try {DataOutputStream dos = new DataOutputStream (s.getOutputStream ()) Dos.writeUTF (text);} catch (Exception ex) {ex.printStackTrace ();};}}
Client
Package socket;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.DataInputStream;import java.io.DataOutputStream;import java.net.Socket;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JTextArea;import javax.swing.JTextField;public class GUIClient {public static void main (String [] args) throws Exception {JFrame f = new JFrame (); f.setTitle ("client"); f.setSize (400300) F.setLocation (600,200); f.setLayout (null); JButton b = new JButton ("send"); b.setBounds (10,10,80,30); f.add (b); final JTextField tf = new JTextField (); tf.setBounds (10,110,80,30); f.add (tf); final JTextArea ta = new JTextArea () Ta.setBounds (110J 10,200,300); f.add (ta); f.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); f.setVisible (true); final Socket s = new Socket ("127.0.0.1", 8888) New Thread () {public void run () {while (true) {try {DataInputStream dis = new DataInputStream (s.getInputStream ()); String text = dis.readUTF () Ta.append (text+ "\ r\ n");} catch (Exception e) {e.printStackTrace ();} .start () B.addActionListener (new ActionListener () {@ Override public void actionPerformed (ActionEvent e) {String text = tf.getText (); ta.append (text+ "\ r\ n"); try {DataOutputStream dos = new DataOutputStream (s.getOutputStream ()) Dos.writeUTF (text);} catch (Exception ex) {ex.printStackTrace ();}) }} here, the article "how to realize multithreaded chat in Java network programming" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about related articles, welcome to follow the industry information channel.