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();
    }
}

5.截圖

學生端:

老師端:

6.總結

首先創建一個發送線程,裡面定義要發送到的IP地址和端口,再創建一個DatagramSocket來進行通信,創建一個BufferReader(new InputStreamReader(System.in))來獲取控制臺的輸入的數據,將獲取到的數據打包進行發送。之後創建一個接收線程,裡面定義自身的端口即可,再創建一個和發送端一樣的DatagramSocket來進行通信,接收數據進行打印輸出即可。最後模擬學生端和老師端,創建這兩個線程來進行通信。學生發送線程到的端口是老師的接收端口,老師發送的端口是學生的接收端口,這樣才能互相通信。

以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。

推薦閱讀:

    None Found