运维开发网

Java网络编程UDP实现多线程在线聊天

运维开发网 https://www.qedev.com 2021-07-05 09:29 出处:网络 作者: 跑起来要带风!
本文实例为大家分享了java实现多线程在线聊天的具体代码,供大家参考,具体内容如下

本文实例为大家分享了java实现多线程在线聊天的具体代码,供大家参考,具体内容如下

上一篇博客通过UDP实现了聊天,但只能单方面发送消息,这次实现了多线程在线聊天,也就是可以双方互发消息。

发送消息:

package com.kuang.chat;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import 编程客栈java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketException;

public class TalkSend implements Runnable {
    DatagramSocket socket = null;
    BufferedReader reader = null;

    private int fromPort;
    private String toIP;
    private int toPort;

    public TalkSend(int fromPort, String toIP, int toPort) {
        this.fromPort = fromPort;
        this.toIP = toIP;
        this.toPort = toPort;

        try {
            socket = new DatagramSocket(fromPort);
            reader = new BufferedReader(new InputStreamReader(System.in));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void run() {
        while (true) {
            try {
                String data = reader.readLine();
                byte[] datas = data.getBytes();
                DatagramPacket packet = new DatagramPacket(datas,0,datas.length,new InetSocketAddress(this.toIP,thhttp://www.cppcns.comis.toPort));

                socket.send(packet);
                if (data.equals("bye")) {
                    break;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        socket.close();
    }
}

接收消息:

package com.kuang.chat;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

public class TalkReceive implements Runnable {
    DatagramSocket socket = null;
    private int port;
    private String msgFrom;

    public TalkReceive(int port,String msgFrom) 编程客栈{
        this.port = port;
        this.msgFrom = msgFrom;
        try {
            socket = new DatagramSocket(port);
        } catch (SocketException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void run() {

        while (true) {
       http://www.cppcns.com     try {
                //准备接收数据
                byte[] container = new byte[1024];
                DatagramPacket packet = new DatagramPacket(container,0,container.length);
                socket.receive(packet);//阻塞式接受包裹

                //断开连接 bye
                byte[] data = packet.getData();
                String receiveData = new String(data,0,data.length);

                System.out.println(msgFrom + ":" +receiveData);
                if (receiveData.equals("bye")) {
                    break;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        socket.close();
    }
}

用户1:学生

package com.kuang.chat;

public class TalkStudent {
    public static void main(String[] args) {
        //开启两个线程
        new Thread(new TalkSend(7777,"localhost",9999)).start();
        new Thread(new TalkReceive(8888,"老师")).start();
    }
}

用户2:老师

package com.kuang.chat;

public class TalkTeacher {
    public static void main(String[] args) {
        new Thread(new TalkSend(5555,"localhost",8编程客栈888)).start();
        new Thread(new TalkReceive(9999,"学生")).start();
    }
}

运行结果:

1、启动学生端,开始运行

Java网络编程UDP实现多线程在线聊天

2、启动教师端,开始运行

Java网络编程UDP实现多线程在线聊天

3、学生发送消息,教师收到

Java网络编程UDP实现多线程在线聊天

Java网络编程UDP实现多线程在线聊天

4、教师发送消息,学生收到

Java网络编程UDP实现多线程在线聊天

Java网络编程UDP实现多线程在线聊天

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

0

精彩评论

暂无评论...
验证码 换一张
取 消