1. 程式人生 > >java中DatagramSocket連續傳送多個數據報包時產生丟包現象解決方案

java中DatagramSocket連續傳送多個數據報包時產生丟包現象解決方案

 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  }