本文實例講述了Java基于socket服務實現UDP協議的方法。分享給大家供大家參考。具體如下:
示例1:
接收類:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
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(); } } |
發送類:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
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:
接收類:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
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(); } } |
發送類:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
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程序設計有所幫助。