App下載

Java實(shí)例代碼展示基于UDP多線程實(shí)現(xiàn)在線咨詢的功能

自我否定先驅(qū)者 2021-08-09 15:58:44 瀏覽數(shù) (2764)
反饋

隨著現(xiàn)在網(wǎng)絡(luò)信息越來(lái)越發(fā)達(dá),越來(lái)越多的行業(yè)都支持在線咨詢,可以更好地幫助客戶解決問(wèn)題等。下面,將使用Java多線程,基于UDP協(xié)議來(lái)實(shí)現(xiàn)一個(gè)在線咨詢的功能。

1.發(fā)送的線程

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;

//發(fā)送線程
public class TalkSend implements Runnable {
    DatagramSocket socket = null;
    BufferedReader reader = null;
    private int formPort; //自身端口
    private String toIP; //發(fā)送到的IP
    private int toPort; //發(fā)送到的端口

    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)); //獲取控制臺(tái)輸入
        } catch (SocketException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void run() {
        while(true) {
            try {
                String data = reader.readLine(); //獲取到的數(shù)據(jù)
                byte[] datas = data.getBytes(); //轉(zhuǎn)換為需要的字節(jié)數(shù)組
                //將數(shù)據(jù)打包成包
                DatagramPacket packet = new DatagramPacket(datas,0, datas.length,new InetSocketAddress(this.toIP,this.toPort));
                socket.send(packet); //發(fā)送包
                //退出指令
                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; //與發(fā)送端的socket為同一個(gè),實(shí)現(xiàn)通信
    private int port;
    private String msgFrom; //標(biāo)志信息來(lái)源

    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);//接收數(shù)據(jù)

                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.模擬學(xué)生端

//模擬學(xué)生
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, "學(xué)生")).start();
    }
}

5.截圖

學(xué)生端:

2021420111242659

老師端:

2021420111255597

6.總結(jié)

首先創(chuàng)建一個(gè)發(fā)送線程,里面定義要發(fā)送到的IP地址和端口,再創(chuàng)建一個(gè)DatagramSocket來(lái)進(jìn)行通信,創(chuàng)建一個(gè)BufferReader(new InputStreamReader(System.in))來(lái)獲取控制臺(tái)的輸入的數(shù)據(jù),將獲取到的數(shù)據(jù)打包進(jìn)行發(fā)送。之后創(chuàng)建一個(gè)接收線程,里面定義自身的端口即可,再創(chuàng)建一個(gè)和發(fā)送端一樣的DatagramSocket來(lái)進(jìn)行通信,接收數(shù)據(jù)進(jìn)行打印輸出即可。最后模擬學(xué)生端和老師端,創(chuàng)建這兩個(gè)線程來(lái)進(jìn)行通信。學(xué)生發(fā)送線程到的端口是老師的接收端口,老師發(fā)送的端口是學(xué)生的接收端口,這樣才能互相通信。

以上就是本文關(guān)于Java代碼基于UDP多線程實(shí)現(xiàn)在線咨詢的全部?jī)?nèi)容,希望能幫助大家對(duì)于Java多線程知識(shí)的學(xué)習(xí)和理解,也希望大家多多支持W3Cschool


0 人點(diǎn)贊