Java的socket服務UDP協議

來源:互聯網
上載者:User

標籤:方便   data   解析   data-   連接埠號碼   監聽   net   system.in   packet   

練習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("接收端啟動…………");/*2、建立UDP的socket的服務,必須明白一個連接埠號碼3、建立資料包。用於儲存接收到的資料,方便用資料包對象的方法解析這些資料 4、使用DatagramSocket的receive方法將接收到的資料存放區到資料包中5、通過資料包的方法解析資料包中的資料5、關閉socket服務*///udpsocket服務。使用DatagramSocket對象DatagramSocket ds=new DatagramSocket(10002);//使用DatagramPacket將資料封裝到該對象中byte[] buf=new byte[1024];DatagramPacket dp=new DatagramPacket(buf, buf.length);//通過udp的socket服務將資料包發送出去。通過send方法ds.receive(dp);//通過資料包的方法解析資料包中的資料,比方。地址、連接埠、資料內容等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);//關閉資源ds.close();}}

發送類

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("發送端啟動…………");/* *  1、建立udp傳輸的發送端2、建立UDP的socket的服務3、將要發送的資料封裝到資料包中4、通過udp的socket服務將資料包發送出去5、關閉socket服務*///udpsocket服務。使用DatagramSocket對象DatagramSocket ds=new DatagramSocket(8888);//監聽連接埠//將要發送的資料封裝到資料包中String str="udp傳輸示範。go";//使用DatagramPacket將資料封裝到該對象中byte[] buf=str.getBytes();DatagramPacket dp=new DatagramPacket(buf, buf.length,InetAddress.getByName("192.168.1.100"),10002);//通過udp的socket服務將資料包發送出去,通過send方法ds.send(dp);//關閉資源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("接收端啟動…………");/*2、建立UDP的socket的服務,必須明白一個連接埠號碼3、建立資料包,用於儲存接收到的資料,方便用資料包對象的方法解析這些資料 4、使用DatagramSocket的receive方法將接收到的資料存放區到資料包中5、通過資料包的方法解析資料包中的資料5、關閉socket服務*///udpsocket服務,使用DatagramSocket對象DatagramSocket ds=new DatagramSocket(10003);while(true){//使用DatagramPacket將資料封裝到該對象中byte[] buf=new byte[1024];DatagramPacket dp=new DatagramPacket(buf, buf.length);//通過udp的socket服務將資料包發送出去,通過send方法ds.receive(dp);//堵塞式的。//通過資料包的方法解析資料包中的資料,比方。地址、連接埠、資料內容等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);}//關閉資源//ds.close();}}

發送類:

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("發送端啟動…………");/* *  1、建立udp傳輸的發送端2、建立UDP的socket的服務3、將要發送的資料封裝到資料包中4、通過udp的socket服務將資料包發送出去5、關閉socket服務*///udpsocket服務,使用DatagramSocket對象DatagramSocket ds=new DatagramSocket(9999);//監聽連接埠//將要發送的資料封裝到資料包中//String str="udp傳輸示範,go";BufferedReader bufr=new BufferedReader(new InputStreamReader(System.in));//鍵盤輸入String line=null;//使用DatagramPacket將資料封裝到該對象中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服務將資料包發送出去,通過send方法ds.send(dp);if("886".equals(line)){break;}}//關閉資源ds.close();}}
執行:

接收


發送


Java的socket服務UDP協議

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.