Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use javasocket to implement a chat Mini Program

Shulou Source: shulou.com Published: 2022-06-01 00:29:11 10月12日 Update

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

Server

Package demo04;import java.awt.BorderLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;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.JOptionPane;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JTextArea;import javax.swing.JTextField;import javax.swing.ScrollPaneConstants Public class ownServer {public static void main (String [] args) {/ / TODO Auto-generated method stub new Server (). Launch ();} class Server {JFrame frame; JTextArea taArea; JTextField tfField; JButton sbButton; DataInputStream read; DataOutputStream write; ServerSocket scServerSocket; Socket socket; public void launch () {CreateUI (); CreateNetWork (); new ServerWrite (). Start (); new ServerRead (). Start ();} public void CreateUI () {frame = new JFrame ("MM Server"); JPanel Panle = new JPanel () TaArea = new JTextArea (155.25); taArea.setLineWrap (true); taArea.setWrapStyleWord (true); taArea.setEditable (true); JScrollPane qScroller=newJScrollPane (taArea); qScroller.setVerticalScrollBarPolicy (ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); qScroller.setHorizontalScrollBarPolicy (ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS); tfField = new JTextField (20); sbButton = new JButton ("send"); Panle.add (qScroller); Panle.add (tfField); Panle.add (sbButton); frame.getContentPane (). Add (BorderLayout.CENTER,Panle) Frame.setSize (400400); frame.setVisible (true);} public void close () {try {write.close (); read.close (); socket.close (); scServerSocket.close ();} catch (Exception e) {System.exit (- 1);}} public void CreateNetWork () {try {scServerSocket = new ServerSocket (7780); socket= scServerSocket.accept (); read = new DataInputStream (socket.getInputStream ()) Write = new DataOutputStream (socket.getOutputStream ()); System.out.print ("Network connection");} catch (Exception e) {/ / TODO: handle exception e.printStackTrace ();}} class ServerRead extends Thread {public void run () {while (true) {try {String message = read.readUTF (); taArea.append ("other party:" + message+ "\ n") } catch (Exception E1) {/ / TODO: handle exception JOptionPane.showMessageDialog (taArea, "prompt: server has been disconnected"); / / e.printStackTrace (); / / System.exit (- 1); return;}} class ServerWrite extends Thread {public void run () {tfField.addActionListener (new ServerListen ()); sbButton.addActionListener (new ServerListen ()) }} class ServerListen implements ActionListener {@ Override public void actionPerformed (ActionEvent e) {try {String str = tfField.getText (); tfField.setText (""); taArea.append ("I:" + str + "\ n"); write.writeUTF (str);} catch (Exception e2) {/ / TODO: handle exception e2.printStackTrace ();}

Client

Package demo04;import java.awt.BorderLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import java.io.DataInputStream;import java.io.DataOutputStream;import java.net.Socket;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JTextArea;import javax.swing.JTextField;import javax.swing.ScrollPaneConstants Public class ownClient {public static void main (String [] args) {/ / TODO Auto-generated method stub new Client (). Launch ();} class Client {JTextArea taArea; JTextField tfField; JButton sbButton; DataInputStream read = null; DataOutputStream write = null; Socket socket; public void launch () {CreateUI (); CreatNetWork (); new ClientRead (). Start (); new ClientWrite (). Start ();;} public void CreateUI () {JFrame frame = new JFrame ("MM Client"); JPanel Panle = new JPanel () TaArea = new JTextArea (155.25); taArea.setLineWrap (true); taArea.setWrapStyleWord (true); taArea.setEditable (true); JScrollPane qScroller = new JScrollPane (taArea); qScroller.setVerticalScrollBarPolicy (ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); qScroller.setHorizontalScrollBarPolicy (ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS); tfField = new JTextField (20); sbButton = new JButton ("send"); Panle.add (qScroller); Panle.add (tfField); Panle.add (sbButton); frame.getContentPane (). Add (BorderLayout.CENTER,Panle) Frame.setSize (400400); frame.setVisible (true);} public void CreatNetWork () {/ / TODO Auto-generated method stub try {socket = new Socket ("127.0.0.1", 7780); write = new DataOutputStream (socket.getOutputStream ()); read = new DataInputStream (socket.getInputStream ()); System.out.print ("Network connection established");} catch (Exception e) {/ / TODO: handle exception e.printStackTrace () }} class ClientRead extends Thread {public void run () {while (true) {try {String message= read.readUTF (); taArea.append ("other party:" + message+ "\ n");} catch (Exception E1) {/ / TODO: handle exception JOptionPane.showMessageDialog (taArea, "prompt: client has been disconnected"); / / e.printStackTrace (); / / System.exit (- 1); return } class ClientWrite extends Thread {public void run () {tfField.addActionListener (new ClientListen ()); sbButton.addActionListener (new ClientListen ());}} class ClientListen implements ActionListener {@ Override public void actionPerformed (ActionEvent e) {try {String str = tfField.getText (); tfField.setText ("); taArea.append (" I: "+ str +"\ n "); write.writeUTF (str) } catch (Exception e2) {/ / TODO: handle exception e2.printStackTrace ();} that's all for "how to use javasocket to implement a chat Mini Program". Thank you for 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.

Tags: Programs knowledge clients clients each other networks industries tips services different practical content practical practical articles methods more servers cases knowledge points Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple MariaDB Shulou Technology OPPO Reno Docker