Java基于socket服務(wù)實(shí)現(xiàn)UDP協(xié)議的方法
本文實(shí)例講述了Java基于socket服務(wù)實(shí)現(xiàn)UDP協(xié)議的方法。分享給大家供大家參考。具體如下:
示例1:
接收類:
package com.socket.demo; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; public class UDPReceiveDemo { public static void main(String[] args) throws IOException{ System.out.println("接收端啟動(dòng)…………"); /* 2、建立UDP的socket的服務(wù),必須明確一個(gè)端口號(hào) 3、創(chuàng)建數(shù)據(jù)包,用于儲(chǔ)存接收到的數(shù)據(jù),方便用數(shù)據(jù)包對(duì)象的方法解析這些數(shù)據(jù) 4、使用DatagramSocket的receive方法將接收到的數(shù)據(jù)存儲(chǔ)到數(shù)據(jù)包中 5、通過數(shù)據(jù)包的方法解析數(shù)據(jù)包中的數(shù)據(jù) 5、關(guān)閉socket服務(wù) */ //udpsocket服務(wù),使用DatagramSocket對(duì)象 DatagramSocket ds=new DatagramSocket(10002); //使用DatagramPacket將數(shù)據(jù)封裝到該對(duì)象中 byte[] buf=new byte[1024]; DatagramPacket dp=new DatagramPacket(buf, buf.length); //通過udp的socket服務(wù)將數(shù)據(jù)包發(fā)送出去,通過send方法 ds.receive(dp); //通過數(shù)據(jù)包的方法解析數(shù)據(jù)包中的數(shù)據(jù),比如,地址、端口、數(shù)據(jù)內(nèi)容等 String ip=dp.getAddress().getHostAddress(); //String name=dp.getAddress().getHostName(); int port=dp.getPort(); String text=new String(dp.getData(),0,dp.getLength()); //System.out.println("-----"+ip+"-----"+name+"-----"+port+"-----"+text); System.out.println("-----"+ip+"----------"+port+"-----"+text); //關(guān)閉資源 ds.close(); } }
發(fā)送類:
package com.socket.demo; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.net.SocketException; import java.net.UnknownHostException; public class UDPSendDemo { public static void main(String[] args) throws IOException{ System.out.println("發(fā)送端啟動(dòng)…………"); /* * 1、創(chuàng)建udp傳輸?shù)陌l(fā)送端 2、建立UDP的socket的服務(wù) 3、將要發(fā)送的數(shù)據(jù)封裝到數(shù)據(jù)包中 4、通過udp的socket服務(wù)將數(shù)據(jù)包發(fā)送出去 5、關(guān)閉socket服務(wù) */ //udpsocket服務(wù),使用DatagramSocket對(duì)象 DatagramSocket ds=new DatagramSocket(8888);//監(jiān)聽端口 //將要發(fā)送的數(shù)據(jù)封裝到數(shù)據(jù)包中 String str="udp傳輸演示,go"; //使用DatagramPacket將數(shù)據(jù)封裝到該對(duì)象中 byte[] buf=str.getBytes(); DatagramPacket dp= new DatagramPacket(buf, buf.length,InetAddress.getByName("192.168.1.100"),10002); //通過udp的socket服務(wù)將數(shù)據(jù)包發(fā)送出去,通過send方法 ds.send(dp); //關(guān)閉資源 ds.close(); } }
示例2:
接收類:
package com.socket.demo; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; public class UDPReceiveDemo2 { public static void main(String[] args) throws IOException{ System.out.println("接收端啟動(dòng)…………"); /* 2、建立UDP的socket的服務(wù),必須明確一個(gè)端口號(hào) 3、創(chuàng)建數(shù)據(jù)包,用于儲(chǔ)存接收到的數(shù)據(jù),方便用數(shù)據(jù)包對(duì)象的方法解析這些數(shù)據(jù) 4、使用DatagramSocket的receive方法將接收到的數(shù)據(jù)存儲(chǔ)到數(shù)據(jù)包中 5、通過數(shù)據(jù)包的方法解析數(shù)據(jù)包中的數(shù)據(jù) 5、關(guān)閉socket服務(wù) */ //udpsocket服務(wù),使用DatagramSocket對(duì)象 DatagramSocket ds=new DatagramSocket(10003); while(true){ //使用DatagramPacket將數(shù)據(jù)封裝到該對(duì)象中 byte[] buf=new byte[1024]; DatagramPacket dp=new DatagramPacket(buf, buf.length); //通過udp的socket服務(wù)將數(shù)據(jù)包發(fā)送出去,通過send方法 ds.receive(dp);//阻塞式的。 //通過數(shù)據(jù)包的方法解析數(shù)據(jù)包中的數(shù)據(jù),比如,地址、端口、數(shù)據(jù)內(nèi)容等 String ip=dp.getAddress().getHostAddress(); //String name=dp.getAddress().getHostName(); int port=dp.getPort(); String text=new String(dp.getData(),0,dp.getLength()); //System.out.println("-----"+ip+"-----"+name+"-----"+port+"-----"+text); System.out.println("-----"+ip+"----------"+port+"-----"+text); } //關(guān)閉資源 //ds.close(); } }
發(fā)送類:
package com.socket.demo; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; public class UDPSendDemo2 { public static void main(String[] args) throws IOException{ System.out.println("發(fā)送端啟動(dòng)…………"); /* * 1、創(chuàng)建udp傳輸?shù)陌l(fā)送端 2、建立UDP的socket的服務(wù) 3、將要發(fā)送的數(shù)據(jù)封裝到數(shù)據(jù)包中 4、通過udp的socket服務(wù)將數(shù)據(jù)包發(fā)送出去 5、關(guān)閉socket服務(wù) */ //udpsocket服務(wù),使用DatagramSocket對(duì)象 DatagramSocket ds=new DatagramSocket(9999);//監(jiān)聽端口 //將要發(fā)送的數(shù)據(jù)封裝到數(shù)據(jù)包中 //String str="udp傳輸演示,go"; BufferedReader bufr=new BufferedReader(new InputStreamReader(System.in));//鍵盤輸入 String line=null; //使用DatagramPacket將數(shù)據(jù)封裝到該對(duì)象中 while((line=bufr.readLine())!=null){ byte[] buf=line.getBytes();// DatagramPacket dp= new DatagramPacket(buf, buf.length,InetAddress.getByName("192.168.1.100"),10003); //通過udp的socket服務(wù)將數(shù)據(jù)包發(fā)送出去,通過send方法 ds.send(dp); if("886".equals(line)){ break; } } //關(guān)閉資源 ds.close(); } }
運(yùn)行效果圖如下:
接收:
發(fā)送:
希望本文所述對(duì)大家的java程序設(shè)計(jì)有所幫助。
相關(guān)文章
Java 實(shí)戰(zhàn)項(xiàng)目錘煉之小區(qū)物業(yè)管理系統(tǒng)的實(shí)現(xiàn)流程
讀萬卷書不如行萬里路,只學(xué)書上的理論是遠(yuǎn)遠(yuǎn)不夠的,只有在實(shí)戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+SSM+jsp+mysql+maven實(shí)現(xiàn)一個(gè)小區(qū)物業(yè)管理系統(tǒng),大家可以在過程中查缺補(bǔ)漏,提升水平2021-11-11Java使用Queryable-pageable實(shí)現(xiàn)分頁效果
這篇文章主要為大家介紹了Java如何使用Queryable-pageable從而實(shí)現(xiàn)分頁效果,文中的示例代碼簡潔易懂,感興趣的小伙伴可以動(dòng)手嘗試一下2022-06-06java如何實(shí)現(xiàn)判斷文件的真實(shí)類型
本篇文章主要介紹了java如何實(shí)現(xiàn)判斷文件的真實(shí)類型,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-08-08通過實(shí)例了解如何在JavaWeb實(shí)現(xiàn)文件下載
這篇文章主要介紹了通過實(shí)例了解如何在JavaWeb實(shí)現(xiàn)文件下載,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09從0構(gòu)建Oauth2Server服務(wù)之Refreshing-access-tokens
這篇文章主要為大家介紹了從0構(gòu)建Oauth2Server服務(wù)之Refreshing-access-tokens刷新令牌示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05將對(duì)象轉(zhuǎn)化為字符串的java實(shí)例
這篇文章主要介紹了將對(duì)象轉(zhuǎn)化為字符串的java實(shí)例,有需要的朋友可以參考一下2013-12-12JAVA實(shí)現(xiàn)長連接(含心跳檢測(cè)Demo)
這篇文章主要介紹了JAVA實(shí)現(xiàn)長連接(含心跳檢測(cè)Demo),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10