This article shares a Socket simple chat tool for you, I hope you like it.
The code runs as shown in the figure and looks pretty good
Server side
package qiu;import java.awt.BorderLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.print.Printable;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintWriter;import java.net.ServerSocket;import java.net.Socket;import java.util.Calendar;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JTextArea;import javax.swing.JTextField;/** * Server for simple chat software* */public class MyServer extends JFrame implements ActionListener{ JTextArea jTextArea =null;//Single-line area used to display plain text JTextField jTextField=null;//It can be allowed to edit single-line text JButton sendButton=null; JPanel jPanel=null; JScrollPane jScrollPane =null; //Send information to the client object PrintWriter printWriter =null;/** * Main function on the server* */public static void main(String[] args) { // TODO Auto-generated method stub new MyServer();}/** * The constructor of the server, used to initialize* */public MyServer(){ // Here is the initialization of the GUI jTextArea = new JTextArea(); jTextField= new JTextField(20); sendButton= new JButton("Send"); sendButton.addActionListener(this); sendButton.setActionCommand("send"); jScrollPane= new JScrollPane(jTextArea); jPanel = new JPanel(); jPanel.add(jTextField);//Add the edit box jPanel.add(sendButton);//Add button//Add the layout of the two panels this.add(jScrollPane,BorderLayout.CENTER); this.add(jPanel,BorderLayout.SOUTH); this.setSize(400,300); this.setTitle("Chat Server"); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//Set the exit button this.setVisible(true); this.setResizable(true); //The following is the construction of the socket server try { //The server listens ServerSocket ss = new ServerSocket(9988); //Waiting for the client to connect Socket socket = ss.accept(); //Get the stream of data sent by the client BufferedReader br = new BufferedReader (new InputStreamReader(socket.getInputStream())); printWriter = new PrintWriter(socket.getOutputStream(),true); //Read the information sent from the client while(true){ String info = br.readLine(); jTextArea.append("Client"+getTime()+"/r/n"+info+"/r/n"); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }/** * Used to get the current time* @return Current time*/public String getTime(){ //Calendar c = Calendar.getInstance(); int hour = c.get(Calendar.HOUR_OF_DAY);//Get the hour int minute = c.get(Calendar.MINUTE); int second = c.get(Calendar.SECOND); return hour+":"+minute+":"+second; }/** * Called when the button is clicked*/@Overridepublic void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub //Call if(e.getActionCommand().equals("send")){ //Send the content written by the server in jTextField to the client String info= jTextField.getText(); jTextArea.append("server"+getTime()+"/r/n"+info+"/r/n"); printWriter.println(info); //Clearly send box content jTextField.setText(""); } }}Client
package qiu;import java.awt.BorderLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintWriter;import java.net.Socket;import java.net.UnknownHostException;import java.util.Calendar;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JTextArea;import javax.swing.JTextField;/** * Client for simple chat software* */public class MyClient extends JFrame implements ActionListener{ JTextArea jTextArea=null; JTextField jTextField=null; JPanel jPanel=null; JScrollPane jScrollPane=null; JButton sendButton=null; PrintWriter printWriter=null;/** * The main function of the client* */public static void main(String[] args) { // TODO Auto-generated method stub new MyClient();}/** * The client constructor is used to initialize* */public MyClient(){ // GUI initialization jTextArea= new JTextArea(); jTextField=new JTextField(20); sendButton= new JButton("Send"); sendButton.addActionListener(this); sendButton.setActionCommand("send"); jScrollPane=new JScrollPane(jTextArea); jPanel=new JPanel(); jPanel.add(jTextField); jPanel.add(sendButton); this.add(jScrollPane,BorderLayout.CENTER); this.add(jPanel,BorderLayout.SOUTH); this.setSize(400, 300); this.setTitle("Chat Client"); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); this.setResizable(true); //socket communication code try { Socket s= new Socket("127.0.0.1",9988); BufferedReader br = new BufferedReader (new InputStreamReader(s.getInputStream())); printWriter= new PrintWriter(s.getOutputStream(),true); while(true){ //Continuously read the information sent by the server String string=br.readLine(); jTextArea.append("Server"+getTime()+"/r/n"+string+"/r/n"); } } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }}/** * Used to get the current time* @return Current time*/public String getTime(){ // Each individual time domain can be modified Calendar c = Calendar.getInstance(); int hour = c.get(Calendar.HOUR_OF_DAY);//Get the hour int minute = c.get(Calendar.MINUTE); int second = c.get(Calendar.SECOND); return hour+":"+minute+":"+second; }/** * Called when the button is clicked*/@Overridepublic void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if(sendButton.getActionCommand().equals("send")){ String info= jTextField.getText(); //Send the information sent by the client to the server jTextArea.append("Client"+getTime()+"/r/n"+info+"/r/n"); printWriter.println(info); jTextField.setText(""); } }} 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.