java 中模擬UDP傳輸?shù)陌l(fā)送端和接收端實例詳解
一、創(chuàng)建UDP傳輸?shù)陌l(fā)送端
1、建立UDP的Socket服務;
2、將要發(fā)送的數(shù)據(jù)封裝到數(shù)據(jù)包中;
3、通過UDP的Socket服務將數(shù)據(jù)包發(fā)送出去;
4、關閉Socket服務。
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
|
import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; public class UDPSend { public static void main(String[] args) throws IOException { System.out.println( "發(fā)送端啟動......" ); // 1、創(chuàng)建UDP的Socket,使用DatagramSocket對象 DatagramSocket ds = new DatagramSocket(); // 2、將要發(fā)送的數(shù)據(jù)封裝到數(shù)據(jù)包中 String str = "UDP傳輸演示:I'm coming!" ; byte [] buf = str.getBytes(); //使用DatagramPacket將數(shù)據(jù)封裝到該對象的包中 DatagramPacket dp = new DatagramPacket(buf, buf.length, InetAddress.getByName( "192.168.191.1" ), 10000 ); // 3、通過UDP的Socket服務將數(shù)據(jù)包發(fā)送出去,使用send方法 ds.send(dp); // 4、關閉Socket服務 ds.close(); } } |
二、創(chuàng)建UDP傳輸?shù)慕邮斩?/strong>
1、建立UDP的Socket服務,因為要接收數(shù)據(jù),所以必須明確一個端口號;
2、創(chuàng)建數(shù)據(jù)包,用于存儲接收到的數(shù)據(jù),方便用數(shù)據(jù)包對象的方法解析這些數(shù)據(jù);
3、使用UDP的Socket服務的receive方法接收數(shù)據(jù)并存儲到數(shù)據(jù)包中;
4、通過數(shù)據(jù)包的方法解析這些數(shù)據(jù);
5、關閉Socket服務。
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
|
import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; public class UDPReceive { public static void main(String[] args) throws IOException { System.out.println( "接收端啟動......" ); // 1、建立UDP的Socket服務 DatagramSocket ds = new DatagramSocket( 10000 ); // 2、創(chuàng)建數(shù)據(jù)包 byte [] buf = new byte [ 1024 ]; DatagramPacket dp = new DatagramPacket(buf, buf.length); // 3、使用接收方法將數(shù)據(jù)存儲到數(shù)據(jù)包中 ds.receive(dp); // 該方法為阻塞式的方法 // 4、通過數(shù)據(jù)包對象的方法解析這些數(shù)據(jù),例如:地址、端口、數(shù)據(jù)內容等 String ip = dp.getAddress().getHostAddress(); int port = dp.getPort(); String text = new String(dp.getData(), 0 , dp.getLength()); System.out.println(ip + ":" + port + ":" + text); // 5、關閉Socket服務 ds.close(); } } |
三、優(yōu)化UDP傳輸?shù)陌l(fā)送端和接收端
由于在前兩部分中,我們一次只能發(fā)送(或接收)一條消息,然后就關閉服務啦!因此如果我們想要發(fā)送多條消息,則需要不斷的在發(fā)送端修改發(fā)送的內容,并且還需要重新啟動服務器,比較麻煩。為了克服以上的缺點,我們可以對其進行優(yōu)化,即:
1、在發(fā)送端,創(chuàng)建BufferedReader,從鍵盤錄入內容;
2、在接收端,添加while(ture)循環(huán),不斷的循環(huán)接收內容。
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
|
/** *優(yōu)化UDP傳輸?shù)陌l(fā)送端 */ 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 UDPSend { public static void main(String[] args) throws IOException { System.out.println( "發(fā)送端啟動......" ); // 創(chuàng)建UDP的Socket,使用DatagramSocket對象 DatagramSocket ds = new DatagramSocket(); BufferedReader bufr = new BufferedReader( new InputStreamReader(System.in)); String line = null ; while ((line = bufr.readLine()) != null ) { // 使用DatagramPacket將數(shù)據(jù)封裝到該對象的包中 byte [] buf = line.getBytes(); DatagramPacket dp = new DatagramPacket(buf, buf.length, InetAddress.getByName( "192.168.191.1" ), 10000 ); // 通過UDP的Socket服務將數(shù)據(jù)包發(fā)送出去,使用send方法 ds.send(dp); // 如果輸入信息為over,則結束循環(huán) if ( "over" .equals(line)) break ; } // 關閉Socket服務 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
|
/** *優(yōu)化UDP傳輸?shù)慕邮斩?/code> */ import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; public class UDPReceive { public static void main(String[] args) throws IOException { System.out.println( "接收端啟動......" ); // 建立UDP的Socket服務 DatagramSocket ds = new DatagramSocket( 10000 ); while ( true ) { // 創(chuàng)建數(shù)據(jù)包 byte [] buf = new byte [ 1024 ]; DatagramPacket dp = new DatagramPacket(buf, buf.length); // 使用接收方法將數(shù)據(jù)存儲到數(shù)據(jù)包中 ds.receive(dp); // 該方法為阻塞式的方法 // 通過數(shù)據(jù)包對象的方法解析這些數(shù)據(jù),例如:地址、端口、數(shù)據(jù)內容等 String ip = dp.getAddress().getHostAddress(); int port = dp.getPort(); String text = new String(dp.getData(), 0 , dp.getLength()); System.out.println(ip + ":" + port + ":" + text); } } } |
四、創(chuàng)建聊天室
根據(jù)UDP(User Datagram Protocol, 用戶數(shù)據(jù)報協(xié)議)的相關性質,我們可以進一步創(chuàng)建一個簡單的基于UDP傳輸協(xié)議下的聊天室,實現(xiàn)互動聊天的功能。
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
|
/** *創(chuàng)建UDP傳輸下的聊天室發(fā)送端 */ package chat; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; public class Send implements Runnable { private DatagramSocket ds; public Send(DatagramSocket ds) { this .ds = ds; } public void run() { try { BufferedReader bufr = new BufferedReader( new InputStreamReader(System.in)); String line = null ; while ((line = bufr.readLine()) != null ) { byte [] buf = line.getBytes(); DatagramPacket dp = new DatagramPacket(buf, buf.length, InetAddress.getByName( "192.168.191.255" ), 10001 ); ds.send(dp); if ( "886" .equals(line)) break ; } ds.close(); } catch (Exception e) { System.out.println( "對不起,發(fā)生錯誤啦!" ); } } } |
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
|
/** *創(chuàng)建UDP傳輸下的聊天室接收端 */ package chat; import java.net.DatagramPacket; import java.net.DatagramSocket; public class Rece implements Runnable { private DatagramSocket ds; public Rece(DatagramSocket ds) { this .ds = ds; } public void run() { try { while ( true ) { byte [] buf = new byte [ 1024 ]; DatagramPacket dp = new DatagramPacket(buf, buf.length); ds.receive(dp); String ip = dp.getAddress().getHostAddress(); String text = new String(dp.getData(), 0 , dp.getLength()); System.out.println(ip + ":::" + text); if (text.equals( "886" )){ System.out.println(ip+ "......退出聊天室!" ); } } } catch (Exception e) { System.out.println( "對不起,發(fā)生錯誤啦!" ); } } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
/** *創(chuàng)建UDP傳輸下的聊天室 */ package chat; import java.io.IOException; import java.net.DatagramSocket; public class ChatRoom { public static void main(String[] args) throws IOException { DatagramSocket send = new DatagramSocket(); DatagramSocket rece = new DatagramSocket( 10001 ); new Thread( new Send(send)).start(); new Thread( new Rece(rece)).start(); } } |
原文鏈接:http://blog.csdn.net/qq_35246620/article/details/53557668