This article describes the implementation of Socket Chat Room based on TCP protocol in Java programming. Share it for your reference, as follows:
Here, Socket sockets are used for programming, which accomplishes dual communication between the server and the client based on TCP reliable service.
Server server side:
package com.han;import java.awt.Container;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import java.io.*;import java.net.*;import javax.swing.JDialog;import javax.swing.JScrollPane;import javax.swing.JTextArea;import javax.swing.JTextField;/** * This program implements the server programming part of a TCP program. * Programming using Socket sockets is done to achieve dual communication with the client based on TCP reliable service. * See the client class Client in this package for programming * @author HAN * */@SuppressWarnings("serial")public class Server extends JDialog{ private BufferedReader reader; private PrintWriter writer; private ServerSocket server; private Socket socket; private JTextArea ta=new JTextArea(); private JScrollPane sp=new JScrollPane(ta); private JTextField tf=new JTextField(); Container cc; public Server(String title) { setTitle(title); addWindowListener(new WindowAdapter() { public void windowClosing (WindowEvent we) { dispose(); //Revoke all related resources related to dialog System.exit(0); //Exit the program normally} }); cc=getContentPane(); setLayout(null); ta.setLineWrap(true); ta.setEditable(false); sp.setBounds(0,0,300,342); tf.setBounds(0,342,300,25); cc.add(sp); cc.add(tf); tf.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ try { writer=new PrintWriter(socket.getOutputStream(),true); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } writer.println(tf.getText()); ta.append("User1:"+tf.getText()+'/n'); tf.setText(""); } }); } void getserver(){ try{ server=new ServerSocket(8998); ta.append("server socket has been created successfully/n"); while(true){ ta.append("waiting for client connection/n"); socket=server.accept(); ta.append("client connected/n"); reader=new BufferedReader(new InputStreamReader(socket.getInputStream())); getClientMessage(); } } catch(Exception e){ e.printStackTrace(); } } private void getClientMessage(){ try { while(true){ String news=reader.readLine(); if(news!=null){ ta.append("User2:"+news+"/n"); }else{ ta.append("User2 (client) Disconnected/n"); break; } } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try{ if(reader!=null){ reader.close(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } if(socket!=null){ try { socket.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public static void main(String[] args) { Server user1=new Server("User1"); user1.setBounds(0,0,300,400); user1.setResizable(false); user1.setVisible(true); user1.getserver(); }}Client Client:
package com.han;import java.awt.*;import java.awt.event.*;import java.io.*;import java.net.*;import javax.swing.*;/** * This program implements the client programming part of a TCP program. * Programming using Socket sockets is done to achieve dual communication with the server based on TCP reliable service. * See the Server class in this package for programming the server * It can be run on different platforms and different machines, but the IP address written in the code must be modified to the IP address of the machine running the server program Server. * @author HAN * */@SuppressWarnings("serial")public class Client extends JDialog{ private BufferedReader reader; private PrintWriter writer; private Socket socket; private JTextArea ta=new JTextArea(); private JScrollPane sp=new JScrollPane(ta); private JTextField tf=new JTextField(); Container cc; public Client(String title) { setTitle(title); addWindowListener(new WindowAdapter() { public void windowClosing (WindowEvent we) { dispose(); //Revoke all related resources related to dialog System.exit(0); //Exit the program normally} }); cc=getContentPane(); setLayout(null); ta.setLineWrap(true); ta.setEditable(false); sp.setBounds(0,0,300,342); tf.setBounds(0,342,300,25); cc.add(sp); cc.add(tf); tf.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ writer.println(tf.getText()); ta.append("User2:"+tf.getText()+'/n'); tf.setText(""); } }); } private void connect(){ ta.append("try to connect/n"); try { socket=new Socket("192.168.1.3",8998); } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { writer=new PrintWriter(socket.getOutputStream(),true); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } ta.append("Complete the connection/n"); } private void getClientMessage(){ try { reader=new BufferedReader(new InputStreamReader(socket.getInputStream())); while(true){ String news=reader.readLine(); if(news!=null){ ta.append("User1:"+news+"/n"); }else{ ta.append("User1 (server) has been disconnected, and when the server is reconnected, restart User2 (client) for communication/n"); break; } } } catch (IOException e) { // TODO Auto-generated catch block ta.append("User1 (server) for communication/n"); e.printStackTrace(); } try{ if(reader!=null){ reader.close(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } if(socket!=null){ try { socket.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public static void main(String[] args) { Client user2=new Client("User2"); user2.setBounds(0,0,300,400); user2.setVisible(true); user2.setResizable(false); user2.connect(); user2.getClientMessage(); }}For more information about Java related content, please check out the topics of this site: "Summary of Java Socket Programming Skills", "Summary of Java File and Directory Operation Skills", "Tutorial on Java Data Structure and Algorithm", "Summary of Java Operation DOM Node Skills" and "Summary of Java Cache Operation Skills"
I hope this article will be helpful to everyone's Java programming.