欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-06-15 13:53:37
本文实例为大家分享了java实现多线程在线聊天的具体代码,供大家参考,具体内容如下上一篇博客通过udp实现了聊天,但只能单方面发送消息,这次实现了多线程在线聊天,也就是可以双方互发消息。发送消息:pa...

本文实例为大家分享了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,this.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) {
            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",8888)).start();
        new thread(new talkreceive(9999,"学生")).start();
    }
}

运行结果:

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

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

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

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

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

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

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

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

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

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

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