且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

java中DatagramSocket连续发送多个数据报包时产生丢包现象解决方案

更新时间:2022-03-28 04:17:11

 1 try {
 2                          //向指定的ip和端口发送数据~!
 3                          //先说明一下数据是谁发送过来的!
 4                          byte[] ip = InetAddress.getLocalHost().getHostAddress().getBytes();
 5                          dp = new DatagramPacket(ip, ip.length, sendIAD, QQReceive.getPort());
 6                          ds.send(dp);
 7                          
 8                          //这里主要是因为多可数据报包发送时会产生丢包的情况...所以暂停一段时间!
 9                          try {
10                             Thread.sleep(100);
11                          } catch (InterruptedException e1) {
12                          }
13                          
14                          dp = new DatagramPacket("PARAGRAPH".getBytes(), "PARAGRAPH".getBytes().length, sendIAD, QQReceive.getPort());
15                          ds.send(dp);
16                          
17                          try {
18                                 Thread.sleep(100);
19                          } catch (InterruptedException e1) {
20                              
21                          }
22                          
23                          dp = new DatagramPacket(bt, bt.length, sendIAD, QQReceive.getPort());
24                          ds.send(dp);
25  } catch (IOException e1) {
26       e1.printStackTrace();
27  }