您好,登錄后才能下訂單哦!
這篇文章主要介紹Java如何實(shí)現(xiàn)UDP多線程在線咨詢,文中介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們一定要看完!
Java是一門面向?qū)ο缶幊陶Z言,可以編寫桌面應(yīng)用程序、Web應(yīng)用程序、分布式系統(tǒng)和嵌入式系統(tǒng)應(yīng)用程序。
具體內(nèi)容如下
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)); //獲取控制臺輸入 } 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(); } }
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為同一個,實(shí)現(xiàn)通信 private int port; private String msgFrom; //標(biāo)志信息來源 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(); } }
//模擬學(xué)生 public class TalkStudent { public static void main(String[] args) { new Thread(new TalkSend(7777, "localhost", 9999)).start(); new Thread(new TalkReceive(8888, "老師")).start(); } }
//模擬老師 public class TalkTeacher { public static void main(String[] args) { new Thread(new TalkSend(5555, "localhost", 8888)).start(); new Thread(new TalkReceive(9999, "學(xué)生")).start(); } }
學(xué)生端:
老師端:
首先創(chuàng)建一個發(fā)送線程,里面定義要發(fā)送到的IP地址和端口,再創(chuàng)建一個DatagramSocket來進(jìn)行通信,創(chuàng)建一個BufferReader(new InputStreamReader(System.in))來獲取控制臺的輸入的數(shù)據(jù),將獲取到的數(shù)據(jù)打包進(jìn)行發(fā)送。之后創(chuàng)建一個接收線程,里面定義自身的端口即可,再創(chuàng)建一個和發(fā)送端一樣的DatagramSocket來進(jìn)行通信,接收數(shù)據(jù)進(jìn)行打印輸出即可。最后模擬學(xué)生端和老師端,創(chuàng)建這兩個線程來進(jìn)行通信。學(xué)生發(fā)送線程到的端口是老師的接收端口,老師發(fā)送的端口是學(xué)生的接收端口,這樣才能互相通信。
以上是“Java如何實(shí)現(xiàn)UDP多線程在線咨詢”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。