java 中模拟UDP传输的发送端和接收端实例详解

2025-05-29 0 105

java 中模拟UDP传输的发送端接收端实例详解

一、创建UDP传输的发送端

1、建立UDP的Socket服务;

2、将要发送的数据封装到数据包中;

3、通过UDP的Socket服务将数据包发送出去;

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("发送端启动......");

// 1、创建UDP的Socket,使用DatagramSocket对象

DatagramSocket ds = new DatagramSocket();

// 2、将要发送的数据封装到数据包中

String str = "UDP传输演示:I'm coming!";

byte[] buf = str.getBytes(); //使用DatagramPacket将数据封装到该对象的包中

DatagramPacket dp = new DatagramPacket(buf, buf.length, InetAddress.getByName("192.168.191.1"), 10000);

// 3、通过UDP的Socket服务将数据包发送出去,使用send方法

ds.send(dp);

// 4、关闭Socket服务

ds.close();

}

}

二、创建UDP传输的接收端

1、建立UDP的Socket服务,因为要接收数据,所以必须明确一个端口号;

2、创建数据包,用于存储接收到的数据,方便用数据包对象的方法解析这些数据;

3、使用UDP的Socket服务的receive方法接收数据并存储到数据包中;

4、通过数据包的方法解析这些数据;

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、创建数据包

byte[] buf = new byte[1024];

DatagramPacket dp = new DatagramPacket(buf, buf.length);

// 3、使用接收方法将数据存储到数据包中

ds.receive(dp); // 该方法为阻塞式的方法

// 4、通过数据包对象的方法解析这些数据,例如:地址、端口、数据内容等

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();

}

}

三、优化UDP传输的发送端接收端

由于在前两部分中,我们一次只能发送(或接收)一条消息,然后就关闭服务啦!因此如果我们想要发送多条消息,则需要不断的在发送端修改发送的内容,并且还需要重新启动服务器,比较麻烦。为了克服以上的缺点,我们可以对其进行优化,即:

1、在发送端,创建BufferedReader,从键盘录入内容;

2、在接收端,添加while(ture)循环,不断的循环接收内容。

?

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
/**

*优化UDP传输的发送端

*/

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("发送端启动......");

// 创建UDP的Socket,使用DatagramSocket对象

DatagramSocket ds = new DatagramSocket();

BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));

String line = null;

while ((line = bufr.readLine()) != null) {

// 使用DatagramPacket将数据封装到该对象的包中

byte[] buf = line.getBytes();

DatagramPacket dp = new DatagramPacket(buf, buf.length, InetAddress.getByName("192.168.191.1"), 10000);

// 通过UDP的Socket服务将数据包发送出去,使用send方法

ds.send(dp);

// 如果输入信息为over,则结束循环

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
/**

*优化UDP传输的接收端

*/

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) {

// 创建数据包

byte[] buf = new byte[1024];

DatagramPacket dp = new DatagramPacket(buf, buf.length);

// 使用接收方法将数据存储到数据包中

ds.receive(dp); // 该方法为阻塞式的方法

// 通过数据包对象的方法解析这些数据,例如:地址、端口、数据内容等

String ip = dp.getAddress().getHostAddress();

int port = dp.getPort();

String text = new String(dp.getData(), 0, dp.getLength());

System.out.println(ip + ":" + port + ":" + text);

}

}

}

四、创建聊天室

根据UDP(User Datagram Protocol, 用户数据报协议)的相关性质,我们可以进一步创建一个简单的基于UDP传输协议下的聊天室,实现互动聊天的功能。

?

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
/**

*创建UDP传输下的聊天室发送端

*/

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("对不起,发生错误啦!");

}

}

}

?

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
/**

*创建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("对不起,发生错误啦!");

}

}

}

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16
/**

*创建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

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 java 中模拟UDP传输的发送端和接收端实例详解 https://www.kuaiidc.com/118008.html

相关文章

发表评论
暂无评论