Java implements one-to-one real-time dialogue

In many mall projects, there are situations where the client communicates with the merchant. Here is a small implementation

Obtain the ip address of the client login user, and link through the user's ip address

public static String getIpAddress(HttpServletRequest request) {
    String ip = request.getHeader("x-forwarded-for");
    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
        ip = request.getHeader("Proxy-Client-IP");
    }
    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
        ip = request.getHeader("WL-Proxy-Client-IP");
    }
    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
        ip = request.getRemoteAddr();
    }
    if (ip.contains(",")) {
        return ip.split(",")[0];
    } else {
        return ip;
    }
}

Implement server-side dialogs:

Among them, you can change the configuration of the dialog box size and style by yourself.

package com.ex.controller;

import javax.servlet.http.HttpServletRequest;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
public class QqMain extends JFrame implements ActionListener{
    public static void main(String[] args){

        InetAddress ia = null;
        try {
            ia = ia.getLocalHost();
            String localip = ia.getHostAddress();
            System.out.println("本机的ip是 :" + localip);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        new QqMain();
    }


    // 说明:一个类需要页面的显示,则那个类要继承JFrame。
    // 属性
    // 文本域
    private JTextArea jta;
    // 滚动条
    private JScrollPane jsp;
    // 面板里面是文本框和按钮
    private JPanel jp;
    private JTextField jtf;
    private JButton jb ;

    BufferedWriter bw  = null;

    // 构造器
    public QqMain(){

        // 初始化上面的属性
        jta = new JTextArea();

        // 将文本域添加到滚动条中
        jsp = new JScrollPane(jta);
        jp = new JPanel();
        jtf =new JTextField(15);
        jb = new JButton("发送");

        // 把按钮和文本框添加到面板中
        jp.add(jtf);
        jp.add(jb);

        // 把滚动条和面板添加到JFrame中去
        this.add(jsp,BorderLayout.CENTER); //这个设置在中间
        this.add(jp,BorderLayout.SOUTH); //南

        this.setTitle("美丽大同店");
        this.setSize(500,500);
        this.setLocation(200, 200);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);

        /***********TCP协议*************/
        jb.addActionListener(this);  // 这是按钮点击使用
        // 回车键的监听事件 在接口KeyListener中
        //jtf.addKeyListener(this);


        jtf.addKeyListener(new KeyAdapter() {
            public void keyTyped(KeyEvent e) {
                if((char)e.getKeyChar()==KeyEvent.VK_ENTER) {
                    useVoid();
                }
            }
        });

        try{
            // 1.创建一个服务端的套接字
            ServerSocket serverSocket = new ServerSocket(8888);

            //2.等待客户端的连接
            Socket socket = serverSocket.accept();

            // 3.获取socket通道的输入流(输入流的读取方式为一行一行的读取方式 ----> readLine())
            BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));

            // 4.获取通道的输入流(也是一行一行的写出  BufferedWriter ->newLine())
            // 当用户点击“发送”按钮的时候才会,写出数据
            bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
            String line = null;
            while((line = br.readLine()) !=null){
                // 将读取的数据拼接到文本域中显示
                jta.append(line + "\n");
            }


            // 5.关闭socket通道
            serverSocket.close();

        }catch(IOException e){
            e.printStackTrace();
        }

        /************************/


    }
    // 点击按钮所实现的方法
    public void actionPerformed(ActionEvent e){
        useVoid();
    }

    public void useVoid(){
        // 1.获取文本框中的内容
        String text = jtf.getText();
        text = "服务端对客户端说:" + text;
        // 自己显示
        jta.append(text + "\n");
        // 2.发送
        try{
            // 4.发送
            bw.write(text);
            bw.newLine(); // 换行
            bw.flush();  // 刷新
            // 5.清空文本框
            jtf.setText("");
        }catch (IOException e1){
            e1.printStackTrace();
        }
    }


	/*public void KeyPressed(KeyEvent e){
		//回车键
		System.out.println("按钮数字");
	}

	public void KeyTyped(KeyEvent e){
	}

	public void KeyReleased(KeyEvent e){
	}*/
    //行为
}

Realize the dialog box of the client, obtain the ip address and port number of the server and then link.

package com.ex.controller;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
public class QqFu extends JFrame implements ActionListener{
    public static void main(String[] args){
        new QqFu();
    }
    // 说明:一个类需要页面的显示,则那个类要继承JFrame。
    // 属性
    // 文本域
    private JTextArea jta;
    // 滚动条
    private JScrollPane jsp;
    // 面板里面是文本框和按钮
    private JPanel jp;
    private JTextField jtf;
    private JButton jb ;

    BufferedWriter bw = null;

    // 构造器
    public QqFu(){

        // 初始化上面的属性
        jta = new JTextArea();

        // 将文本域添加到滚动条中
        jsp = new JScrollPane(jta);
        jp = new JPanel();
        jtf =new JTextField(15);
        jb = new JButton("发送");

        // 把按钮和文本框添加到面板中
        jp.add(jtf);
        jp.add(jb);

        // 把滚动条和面板添加到JFrame中去
        this.add(jsp,BorderLayout.CENTER); //这个设置在中间
        this.add(jp,BorderLayout.SOUTH); //南

        this.setTitle("张三");  //获取用户的昵称
        this.setSize(500,500);
        this.setLocation(200, 200);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
        jb.addActionListener(this);

        // 回车点击事件

        jtf.addKeyListener(new KeyAdapter() {
            public void keyTyped(KeyEvent e) {
                if((char)e.getKeyChar()==KeyEvent.VK_ENTER) {
                    useVoid01();
                }
            }
        });



        try{
            /*******客户端 TCP协议*********/
            // 1.创建一个客户端的套接字(尝试连接)
            Socket socket = new Socket("127.0.0.1",8888);
            // 2.获取socket通道的输入流
            BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            // 3
            bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
            String line = null;
            while((line = br.readLine()) !=null){
                jta.append(line + "\n");
            }
            // 3. 获取输出流

            // 4.关闭流
            socket.close();

            /******************************/
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    public void actionPerformed(ActionEvent e){
        useVoid01();
    }

    public void useVoid01(){
        // 1.获取文本框中需要发送的内容
        String text = jtf.getText();
        // 2. 拼接内容
        text = "客户端对服务端说:" + text;
        // 3.自己显示
        jta.append(text + "\n");
        try{
            // 4.发送
            bw.write(text);
            bw.newLine(); // 换行
            bw.flush();  // 刷新
            // 5.清空
            jtf.setText("");

        }catch(IOException e1){
            e1.printStackTrace();
        }
    }
    //行为
}

You must first start the server and then start the client. If there are two computers, you only need to get the ip of one of the computers to start the server, and the other to connect. real-time conversation

Show results:

 

Guess you like

Origin blog.csdn.net/Java_Mr_Jin/article/details/126240798