千家信息网

Java中基于UDP多线程怎么实现在线咨询的功能

发表于:2025-01-31 作者:千家信息网编辑
千家信息网最后更新 2025年01月31日,本篇内容主要讲解"Java中基于UDP多线程怎么实现在线咨询的功能",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"Java中基于UDP多线程怎么实现在线咨询
千家信息网最后更新 2025年01月31日Java中基于UDP多线程怎么实现在线咨询的功能

本篇内容主要讲解"Java中基于UDP多线程怎么实现在线咨询的功能",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"Java中基于UDP多线程怎么实现在线咨询的功能"吧!

1.发送的线程

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 formPort; //自身端口    private String toIP; //发送到的IP    private int toPort; //发送到的端口    public TalkSend(int formPort, String toIP, int toPort) {        this.formPort = formPort;        this.toIP = toIP;        this.toPort = toPort;        try {            socket = new DatagramSocket(formPort);            reader = new BufferedReader(new InputStreamReader(System.in)); //获取控制台输入        } catch (SocketException 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(packet.equals("bye")){                    break;                }            } catch (IOException e) {                e.printStackTrace();            }        }        socket.close();    }}

2.接收的线程

import java.io.IOException;import java.net.DatagramPacket;import java.net.DatagramSocket;import java.net.SocketException;//接收线程public class TalkReceive implements Runnable {    DatagramSocket socket = null; //与发送端的socket为同一个,实现通信    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);//接收数据                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();    }}

3.模拟学生端

//模拟学生public class TalkStudent {    public static void main(String[] args) {        new Thread(new TalkSend(7777, "localhost", 9999)).start();        new Thread(new TalkReceive(8888, "老师")).start();    }}

4.模拟老师端

//模拟老师public class TalkTeacher {    public static void main(String[] args) {        new Thread(new TalkSend(5555, "localhost", 8888)).start();        new Thread(new TalkReceive(9999, "学生")).start();    }}

到此,相信大家对"Java中基于UDP多线程怎么实现在线咨询的功能"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

0