UDP及其组播,接收发送封装
1.Receiver
1 public class Receiver 2 { 3 public delegate void HeartBeat(byte[] data); 4 public event HeartBeat Heart; 5 6 #region 内部变量 7 private int size = 65535; 8 private UdpClient newsock; 9 private Thread t; 10 private int m_port; 11 private IPEndPoint sender; 12 #endregion 13 14 /// <summary> 15 /// UDP开始接收指定端口数据 16 /// </summary> 17 /// <param name="port"></param> 18 public void Start(string port) 19 { 20 Int32.TryParse(port, out m_port); 21 22 newsock = new UdpClient(m_port); 23 newsock.EnableBroadcast = true; 24 newsock.MulticastLoopback = true; 25 26 sender = new IPEndPoint(IPAddress.Any, m_port); 27 t = new Thread(start); 28 t.IsBackground = true; 29 t.Start(); 30 } 31 32 33 public void Dispose() 34 { 35 if (newsock != null) 36 { 37 newsock.Close(); 38 } 39 40 if (t != null && t.ThreadState == ThreadState.Running) 41 { t.Abort(); } 42 43 } 44 45 void start() 46 { 47 try 48 { 49 byte[] data; 50 51 while (true) 52 { 53 if (newsock == null) 54 { 55 return; 56 } 57 if (newsock.Available <= 0) 58 { 59 continue; 60 } 61 if (newsock.Client == null) 62 { 63 return; 64 } 65 66 data = newsock.Receive(ref sender); 67 68 if (Heart != null) 69 { 70 Heart(data); 71 } 72 } 73 74 } 75 catch (Exception e) 76 { 77 78 79 } 80 81 82 } 83 84 85 public void Join(string ip,string port) 86 { 87 Int32.TryParse(port, out m_port); 88 89 newsock = new UdpClient(m_port); 90 newsock.EnableBroadcast = true; 91 newsock.MulticastLoopback = true; 92 93 sender = new IPEndPoint(IPAddress.Any, m_port); 94 95 if (newsock != null) 96 { 97 newsock.JoinMulticastGroup(IPAddress.Parse(ip)); 98 } 99 100 t = new Thread(start); 101 t.IsBackground = true; 102 t.Start(); 103 } 104 105 public void Leave(string ip) 106 { 107 if (newsock != null) 108 newsock.DropMulticastGroup(IPAddress.Parse(ip)); 109 } 110 111 112 }
2.UdpSender
1 public class UdpSender 2 { 3 private UdpClient sendsock; 4 private int m_sendport; 5 private IPEndPoint m_SendPoint; 6 private static object _LockObj = new object(); 7 8 9 10 11 /// <summary> 12 /// 单点UDP初始化 13 /// </summary> 14 /// <param name="ip"></param> 15 /// <param name="port"></param> 16 public void SetSend(string ip, string port) 17 { 18 Int32.TryParse(port, out m_sendport); 19 m_SendPoint = new IPEndPoint(IPAddress.Parse(ip), m_sendport); 20 21 sendsock = new UdpClient 22 { 23 EnableBroadcast = true, 24 MulticastLoopback = true 25 }; 26 } 27 28 public void SendMsg(byte[] data) 29 { 30 lock (_LockObj) 31 { 32 if (sendsock != null) 33 { 34 sendsock.Send(data, data.Length, m_SendPoint); 35 } 36 } 37 38 } 39 40 public void Dispose() 41 { 42 if (sendsock != null) 43 { 44 sendsock.Close(); 45 } 46 } 47 48 /// <summary> 49 /// UDP组播初始化 50 /// </summary> 51 /// <param name="ip"></param> 52 /// <param name="port"></param> 53 public void Join(string ip, string port) 54 { 55 Int32.TryParse(port, out m_sendport); 56 sendsock = new UdpClient 57 { 58 EnableBroadcast = true, 59 MulticastLoopback = true 60 }; 61 m_SendPoint = new IPEndPoint(IPAddress.Parse(ip), m_sendport); 62 sendsock.JoinMulticastGroup(IPAddress.Parse(ip)); 63 } 64 65 public void Leave(string ip) 66 { 67 if (sendsock != null) 68 sendsock.JoinMulticastGroup(IPAddress.Parse(ip)); 69 } 70 71 }