Blame view

C5/testproject/src/main/java/com/sunvote/udptest/UDPUtils.java 1.22 KB
fac86401   孙向锦   初始化C5 Vote
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
39
40
41
42
43
44
45
46
  package com.sunvote.udptest;
  
  import java.net.DatagramPacket;
  import java.net.DatagramSocket;
  
  /**
   * Created by Elvis on 2017/8/8.
   * Email:Eluis@psunsky.com
   * Description: UDP发送工具类
   */
  
  public class UDPUtils {
  
      private final static String TAG = UDPUtils.class.getSimpleName();
  
      public static boolean send(DatagramSocket skt, byte[] d, int dataLen) {
          if (skt != null && d != null) {
              try {
                  return send(skt, new DatagramPacket(d, dataLen));
              } catch (Exception e) {
                  return false;
              }
          } else {
              return false;
          }
      }
  
      public synchronized static boolean send(DatagramSocket skt, DatagramPacket p) {
          boolean sendSucess = true;
          if (skt != null && p != null) {
                          if (skt.isConnected()) {
                              try {
                                  skt.send(p);
                              } catch (Exception e) {
                                  try{
                                      skt.close();
                                  }catch (Exception ex){}
                      sendSucess = false;
                  }
              }
          } else {
          }
  
          return sendSucess;
      }
  }