zoukankan      html  css  js  c++  java
  • 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     }
  • 相关阅读:
    springboot+vue实现前后端分离之前端vue部分(spring boot 2.5.4/vue.js 3.2.4)
    如何给一个vue项目重命名(vue.js 3.2.4)
    用git命令上传一个项目到gitee(git 2.30.2)
    kde plasma 5.21:为应用程序添加桌面快捷方式(kubuntu 21.04)
    @vue/cli 4.5.13:创建一个vue.js3.x项目(vue.js 3.2.4)
    linux:ubuntu21.04:npm安装@vue/cli时报错(@vue/cli 4.5.13/npm 7.21.0/node 14.17.1)
    python 装饰器模式
    staticmethod classmethod property
    presto 获取hive 表最大分区
    ALIGN(v, a)
  • 原文地址:https://www.cnblogs.com/shanranlei/p/5135733.html
Copyright © 2011-2022 走看看