UDP---用戶數(shù)據(jù)報(bào)協(xié)議,是一個(gè)簡單的面向數(shù)據(jù)報(bào)的運(yùn)輸層協(xié)議。(無連接、封包、大小限制、速度快)。
一、UDP協(xié)議的特點(diǎn):
二、通俗解釋:
面向無連接的,你在與不在我都在發(fā)。就像郵局寄包裹,比如寄餅干。,郵局會(huì)給你一個(gè)盒子和膠帶,你將餅干封上(封包),將數(shù)據(jù)達(dá)成數(shù)據(jù)包,打完之后,貼張地址(天堂街)、門牌號(hào)(地獄門18號(hào))(地址、端口),但地址可能不存在,那么則丟棄。
三、UDP舉例:
步話機(jī)、FEIQ、網(wǎng)絡(luò)會(huì)議、錄屏傳播、聊天。
四、socket(套接字):
Socket就是為網(wǎng)絡(luò)服務(wù)提供的一種機(jī)制。通信的兩端都有Socket。(插座,初期可簡單理解為應(yīng)用程序,而非網(wǎng)卡接口)網(wǎng)絡(luò)通信其實(shí)就是Socket通信。數(shù)據(jù)在兩個(gè)Socket間通過IO傳輸。
五、發(fā)送端:
需求:通過Udp傳輸?shù)姆绞?,將一段文字?jǐn)?shù)據(jù)發(fā)送出去。定義了一個(gè)udp的接收端。思路:1、建立udpSocket服務(wù)(找郵局)2、提供數(shù)據(jù),將數(shù)據(jù)封裝到數(shù)據(jù)包中(封包、寫地址)3、通過socket服務(wù)發(fā)送功能,將數(shù)據(jù)包發(fā)送出去。(包裹上路)4、關(guān)閉資源。

1 class UdpSend 2 { 3 public static void main(String[] args) throws Exception 4 { 5 //1、創(chuàng)建udpSocket服務(wù)。通過DatagramSocket對(duì)象 6 DatagramSocket ds=new DatagramSocket(8888); 7 8 //2、確定數(shù)據(jù),并封裝數(shù)據(jù)包 9 byte[] buf="udp I coming".getBytes();10 11 /*12 InetAddress addr=InetAddress.getLocalHost();13 InetAddress addr = InetAddress.getByName(null);14 InetAddress.getByName("localhost");15 InetAddress.getByName("127.0.0.1");16 */17 DatagramPacket dp=new DatagramPacket(buf,buf.length,InetAddress.getByName("192.168.1.102"),10000);18 19 //3、通過socket服務(wù),將已有的數(shù)據(jù)包發(fā)送出去,通過send方法20 ds.send(dp);21 22 //4、關(guān)閉資源23 ds.close();24 }25 }發(fā)送端程序代碼(class UdpSend )六、接收端:
需求:定義一個(gè)應(yīng)用程序,用于接收udp協(xié)議傳輸?shù)臄?shù)據(jù)并處理定義udp的接收端。思路:1、定義udpSocket服務(wù)。通常會(huì)監(jiān)聽一個(gè)端口。其實(shí)就是給這個(gè)接收網(wǎng)絡(luò)應(yīng)用程序定義一個(gè)數(shù)字標(biāo)識(shí),方便于明確哪些數(shù)據(jù)過來,該應(yīng)用程序可以處理。2、定義一個(gè)數(shù)據(jù)包。因?yàn)橐鎯?chǔ)接收到的字節(jié)數(shù)據(jù)。數(shù)據(jù)包對(duì)象中有更多功能可以提取字節(jié)數(shù)據(jù)中的不同數(shù)據(jù)信息。3、通過socket服務(wù)端額receive方法將收到的數(shù)據(jù)存入已定義的數(shù)據(jù)包中4、通過手機(jī)開那片對(duì)象的特有功能,將這些不同數(shù)據(jù)取出打印在控制臺(tái)上。5、關(guān)閉資源

1 class UdPReceive 2 { 3 public static void main(String[] agrs) throws Exception 4 { 5 //1、創(chuàng)建udpsocket,建立端點(diǎn) 6 DatagramSocket ds=new DatagramSocket(10000); 7 8 //2、定義數(shù)據(jù)包,用于存儲(chǔ)數(shù)據(jù) 9 byte[] buf=new byte[1024];10 DatagramPacket dp=new DatagramPacket(buf,buf.length);11 12 //3、通過receive方法將收到的數(shù)據(jù)存到數(shù)據(jù)包 中13 ds.receive(dp);//阻塞式方法,線程機(jī)制,wait了14 15 //通過數(shù)據(jù)包的方法獲取其中的數(shù)據(jù)16 String ip=dp.getAddress().getHostAddress();//獲取發(fā)送端IP地址17 String data=new String(dp.getData(),0,dp.getLength());//獲取數(shù)據(jù)18 int port=dp.getPort();//獲取發(fā)送端端口19 20 System.out.println(ip+"::"+data+"::"+port);//信息打印控制臺(tái)21 22 //5、關(guān)閉資源23 ds.close();24 }25 }接收端程序代碼(class UdpReceive)
運(yùn)行截圖:

七、在接收者與發(fā)送者之間的改進(jìn)
承接UdpDemo.java,實(shí)現(xiàn)只能一個(gè)人發(fā),一個(gè)人收。1、實(shí)現(xiàn)鍵盤錄入:2、實(shí)現(xiàn)連續(xù)輸入(while)

1 import java.net.*; 2 import java.io.*; 3 4 class UdpSend2 5 { 6 public static void main(String[] args) throws Exception 7 { 8 DatagramSocket ds=new DatagramSocket(); 9 10 //實(shí)現(xiàn)鍵盤錄入11 BufferedReader bufr=12 new BufferedReader(new InputStreamReader(System.in));13 String line=null;14 15 while ((line=bufr.readLine())!=null)16 {17 if ("886".equals(line))18 break;19 byte[] buf=line.getBytes();20 21 //IP地址,暫且用本機(jī)IP地址,也可用廣播地址192.168.1.25522 DatagramPacket dp=new DatagramPacket(buf,buf.length,InetAddress.getByName("192.168.1.102"),10001);23 24 ds.send(dp);25 }26 ds.close();27 }28 }29 30 31 class UdpReceive232 {33 public static void main(String[] agrs) throws Exception34 {35 DatagramSocket ds=new DatagramSocket(10001);36 37 byte[] buf=new byte[1024];38 DatagramPacket dp=new DatagramPacket(buf,buf.length);39 40 while (true)41 {42 ds.receive(dp);//阻塞式方法,線程機(jī)制,wait了43 44 String ip=dp.getAddress().getHostAddress();//獲取發(fā)送端IP地址45 String data=new String(dp.getData(),0,dp.getLength());//獲取數(shù)據(jù)46 int port=dp.getPort();//獲取發(fā)送端端口47 48 System.out.println(ip+"::"+data+"::"+port);//信息打印控制臺(tái)49 }50 51 //ds.close();服務(wù)器一般不關(guān),也有種方法,while判斷條件關(guān)52 }53 }發(fā)送端+接收端Code運(yùn)行截圖:


新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注