elasticsearch入門系列">elasticsearch入門系列
829
2022-05-28
一.UDP通信程序
1. UDP發(fā)送數(shù)據(jù)
Java中的UDP通信
UDP協(xié)議是一種不可靠的網(wǎng)絡(luò)協(xié)議,它在通信的兩端各建立一個(gè)Socket對(duì)象,但是這兩個(gè)Socket只是發(fā)送,接收數(shù)據(jù)的對(duì)象,因此對(duì)于基于UDP協(xié)議的通信雙方而言,沒有所謂的客戶端和服務(wù)器的概念
Java提供了DatagramSocket類作為基于UDP協(xié)議的Socket
構(gòu)造方法
相關(guān)方法
發(fā)送數(shù)據(jù)的步驟
創(chuàng)建發(fā)送端的Socket對(duì)象(DatagramSocket)
創(chuàng)建數(shù)據(jù),并把數(shù)據(jù)打包
調(diào)用DatagramSocket對(duì)象的方法發(fā)送數(shù)據(jù)
關(guān)閉發(fā)送端
代碼演示
public class SendDemo { public static void main(String[] args) throws IOException { //創(chuàng)建發(fā)送端的Socket對(duì)象(DatagramSocket) // DatagramSocket() 構(gòu)造數(shù)據(jù)報(bào)套接字并將其綁定到本地主機(jī)上的任何可用端口 DatagramSocket ds = new DatagramSocket(); //創(chuàng)建數(shù)據(jù),并把數(shù)據(jù)打包 //DatagramPacket(byte[] buf, int length, InetAddress address, int port) //構(gòu)造一個(gè)數(shù)據(jù)包,發(fā)送長(zhǎng)度為 length的數(shù)據(jù)包到指定主機(jī)上的指定端口號(hào)。 byte[] bys = "hello,udp,我來了".getBytes(); DatagramPacket dp = new DatagramPacket(bys,bys.length,InetAddress.getByName("127.0.0.1"),10086); //調(diào)用DatagramSocket對(duì)象的方法發(fā)送數(shù)據(jù) //void send(DatagramPacket p) 從此套接字發(fā)送數(shù)據(jù)報(bào)包 ds.send(dp); //關(guān)閉發(fā)送端 //void close() 關(guān)閉此數(shù)據(jù)報(bào)套接字 ds.close(); } }
2.UDP接收數(shù)據(jù)
接收數(shù)據(jù)的步驟
創(chuàng)建接收端的Socket對(duì)象(DatagramSocket)
創(chuàng)建一個(gè)數(shù)據(jù)包,用于接收數(shù)據(jù)
調(diào)用DatagramSocket對(duì)象的方法接收數(shù)據(jù)
解析數(shù)據(jù)包,并把數(shù)據(jù)在控制臺(tái)顯示
關(guān)閉接收端
構(gòu)造方法
相關(guān)方法
示例代碼
public class ReceiveDemo { public static void main(String[] args) throws IOException { //創(chuàng)建接收端的Socket對(duì)象(DatagramSocket) DatagramSocket ds = new DatagramSocket(12345); //創(chuàng)建一個(gè)數(shù)據(jù)包,用于接收數(shù)據(jù) byte[] bys = new byte[1024]; DatagramPacket dp = new DatagramPacket(bys, bys.length); //調(diào)用DatagramSocket對(duì)象的方法接收數(shù)據(jù) ds.receive(dp); //解析數(shù)據(jù)包,并把數(shù)據(jù)在控制臺(tái)顯示 System.out.println("數(shù)據(jù)是:" + new String(dp.getData(), 0, dp.getLength())); } } }
3.UDP通信程序練習(xí)
案例需求
UDP發(fā)送數(shù)據(jù):數(shù)據(jù)來自于鍵盤錄入,直到輸入的數(shù)據(jù)是886,發(fā)送數(shù)據(jù)結(jié)束
UDP接收數(shù)據(jù):因?yàn)榻邮斩瞬恢腊l(fā)送端什么時(shí)候停止發(fā)送,故采用死循環(huán)接收
代碼實(shí)現(xiàn)
/* UDP發(fā)送數(shù)據(jù): 數(shù)據(jù)來自于鍵盤錄入,直到輸入的數(shù)據(jù)是886,發(fā)送數(shù)據(jù)結(jié)束 */ public class SendDemo { public static void main(String[] args) throws IOException { //創(chuàng)建發(fā)送端的Socket對(duì)象(DatagramSocket) DatagramSocket ds = new DatagramSocket(); //鍵盤錄入數(shù)據(jù) Scanner sc = new Scanner(System.in); while (true) { String s = sc.nextLine(); //輸入的數(shù)據(jù)是886,發(fā)送數(shù)據(jù)結(jié)束 if ("886".equals(s)) { break; } //創(chuàng)建數(shù)據(jù),并把數(shù)據(jù)打包 byte[] bys = s.getBytes(); DatagramPacket dp = new DatagramPacket(bys, bys.length, InetAddress.getByName("192.168.1.66"), 12345); //調(diào)用DatagramSocket對(duì)象的方法發(fā)送數(shù)據(jù) ds.send(dp); } //關(guān)閉發(fā)送端 ds.close(); } } /* UDP接收數(shù)據(jù): 因?yàn)榻邮斩瞬恢腊l(fā)送端什么時(shí)候停止發(fā)送,故采用死循環(huán)接收 */ public class ReceiveDemo { public static void main(String[] args) throws IOException { //創(chuàng)建接收端的Socket對(duì)象(DatagramSocket) DatagramSocket ds = new DatagramSocket(12345); while (true) { //創(chuàng)建一個(gè)數(shù)據(jù)包,用于接收數(shù)據(jù) byte[] bys = new byte[1024]; DatagramPacket dp = new DatagramPacket(bys, bys.length); //調(diào)用DatagramSocket對(duì)象的方法接收數(shù)據(jù) ds.receive(dp); //解析數(shù)據(jù)包,并把數(shù)據(jù)在控制臺(tái)顯示 System.out.println("數(shù)據(jù)是:" + new String(dp.getData(), 0, dp.getLength())); } //關(guān)閉接收端 // ds.close(); } }
4.UDP三種通訊方式
單播
單播用于兩個(gè)主機(jī)之間的端對(duì)端通信
組播
組播用于對(duì)一組特定的主機(jī)進(jìn)行通信
廣播
廣播用于一個(gè)主機(jī)對(duì)整個(gè)局域網(wǎng)上所有主機(jī)上的數(shù)據(jù)通信
5.UDP組播實(shí)現(xiàn)
實(shí)現(xiàn)步驟
發(fā)送端
創(chuàng)建發(fā)送端的Socket對(duì)象(DatagramSocket)
創(chuàng)建數(shù)據(jù),并把數(shù)據(jù)打包(DatagramPacket)
調(diào)用DatagramSocket對(duì)象的方法發(fā)送數(shù)據(jù)(在單播中,這里是發(fā)給指定IP的電腦但是在組播當(dāng)中,這里是發(fā)給組播地址)
釋放資源
接收端
創(chuàng)建接收端Socket對(duì)象(MulticastSocket)
創(chuàng)建一個(gè)箱子,用于接收數(shù)據(jù)
把當(dāng)前計(jì)算機(jī)綁定一個(gè)組播地址
將數(shù)據(jù)接收到箱子中
解析數(shù)據(jù)包,并打印數(shù)據(jù)
釋放資源
代碼實(shí)現(xiàn)
// 發(fā)送端 public class ClinetDemo { public static void main(String[] args) throws IOException { // 1. 創(chuàng)建發(fā)送端的Socket對(duì)象(DatagramSocket) DatagramSocket ds = new DatagramSocket(); String s = "hello 組播"; byte[] bytes = s.getBytes(); InetAddress address = InetAddress.getByName("224.0.1.0"); int port = 10000; // 2. 創(chuàng)建數(shù)據(jù),并把數(shù)據(jù)打包(DatagramPacket) DatagramPacket dp = new DatagramPacket(bytes,bytes.length,address,port); // 3. 調(diào)用DatagramSocket對(duì)象的方法發(fā)送數(shù)據(jù)(在單播中,這里是發(fā)給指定IP的電腦但是在組播當(dāng)中,這里是發(fā)給組播地址) ds.send(dp); // 4. 釋放資源 ds.close(); } } // 接收端 public class ServerDemo { public static void main(String[] args) throws IOException { // 1. 創(chuàng)建接收端Socket對(duì)象(MulticastSocket) MulticastSocket ms = new MulticastSocket(10000); // 2. 創(chuàng)建一個(gè)箱子,用于接收數(shù)據(jù) DatagramPacket dp = new DatagramPacket(new byte[1024],1024); // 3. 把當(dāng)前計(jì)算機(jī)綁定一個(gè)組播地址,表示添加到這一組中. ms.joinGroup(InetAddress.getByName("224.0.1.0")); // 4. 將數(shù)據(jù)接收到箱子中 ms.receive(dp); // 5. 解析數(shù)據(jù)包,并打印數(shù)據(jù) byte[] data = dp.getData(); int length = dp.getLength(); System.out.println(new String(data,0,length)); // 6. 釋放資源 ms.close(); } }
6.UDP廣播實(shí)現(xiàn)
實(shí)現(xiàn)步驟
發(fā)送端
創(chuàng)建發(fā)送端Socket對(duì)象(DatagramSocket)
創(chuàng)建存儲(chǔ)數(shù)據(jù)的箱子,將廣播地址封裝進(jìn)去
發(fā)送數(shù)據(jù)
釋放資源
接收端
創(chuàng)建接收端的Socket對(duì)象(DatagramSocket)
創(chuàng)建一個(gè)數(shù)據(jù)包,用于接收數(shù)據(jù)
調(diào)用DatagramSocket對(duì)象的方法接收數(shù)據(jù)
解析數(shù)據(jù)包,并把數(shù)據(jù)在控制臺(tái)顯示
關(guān)閉接收端
代碼實(shí)現(xiàn)
// 發(fā)送端 public class ClientDemo { public static void main(String[] args) throws IOException { // 1. 創(chuàng)建發(fā)送端Socket對(duì)象(DatagramSocket) DatagramSocket ds = new DatagramSocket(); // 2. 創(chuàng)建存儲(chǔ)數(shù)據(jù)的箱子,將廣播地址封裝進(jìn)去 String s = "廣播 hello"; byte[] bytes = s.getBytes(); InetAddress address = InetAddress.getByName("255.255.255.255"); int port = 10000; DatagramPacket dp = new DatagramPacket(bytes,bytes.length,address,port); // 3. 發(fā)送數(shù)據(jù) ds.send(dp); // 4. 釋放資源 ds.close(); } } // 接收端 public class ServerDemo { public static void main(String[] args) throws IOException { // 1. 創(chuàng)建接收端的Socket對(duì)象(DatagramSocket) DatagramSocket ds = new DatagramSocket(10000); // 2. 創(chuàng)建一個(gè)數(shù)據(jù)包,用于接收數(shù)據(jù) DatagramPacket dp = new DatagramPacket(new byte[1024],1024); // 3. 調(diào)用DatagramSocket對(duì)象的方法接收數(shù)據(jù) ds.receive(dp); // 4. 解析數(shù)據(jù)包,并把數(shù)據(jù)在控制臺(tái)顯示 byte[] data = dp.getData(); int length = dp.getLength(); System.out.println(new String(data,0,length)); // 5. 關(guān)閉接收端 ds.close(); } }
Socket編程 UDP
版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實(shí)的內(nèi)容,請(qǐng)聯(lián)系我們jiasou666@gmail.com 處理,核實(shí)后本網(wǎng)站將在24小時(shí)內(nèi)刪除侵權(quán)內(nèi)容。