• UDP学习笔记(.NET)


    以下代码来源于《C#网络编程技术教程》。

     1     public class UDPComm
     2     {
     3         public static byte[] EncodingASCII(string buf)//编码
     4         {
     5             byte[] data = Encoding.ASCII.GetBytes(buf + "
    ");
     6             return data;
     7         }
     8         public static string DecodingASCII(byte[] buf)//解码
     9         {
    10             string st = Encoding.ASCII.GetString(buf);
    11             return st;
    12         }
    13     }
     1 class UDPServer
     2     {
     3         static UdpClient m_server;
     4         static ArrayList mblist;
     5 
     6         static void AddMember(IPEndPoint rep)//加入组
     7         {
     8             mblist.Add(rep);
     9             byte[] data = UDPComm.EncodingASCII("OK");
    10             m_server.Send(data, data.Length, rep);
    11         }
    12         static void DelMember(IPEndPoint rep)//离开组
    13         {
    14             mblist.Remove(rep);
    15             byte[] data = UDPComm.EncodingASCII("OK");
    16             m_server.Send(data, data.Length, rep);
    17 
    18         }
    19         static void SendToMember(string buf)//组内转发数据
    20         {
    21             foreach (IPEndPoint mb in mblist)
    22             {
    23                 byte[] data = UDPComm.EncodingASCII(buf);
    24                 m_server.Send(data, data.Length, mb);
    25             }
    26         }
    27 
    28         static void Main(string[] args)
    29         {
    30             string m_hostIP = "127.0.0.1";
    31             int m_port = 6666;
    32             IPEndPoint m_EndPoint;
    33             ArrayList memberlist = new ArrayList();
    34             bool rt = false;
    35             byte[] data;
    36             string m_ReturnData;
    37 
    38             //从命令行提取主机IP和端口
    39             if (args.Length < 2)
    40             {
    41                 Console.WriteLine("Usage: UDPServer hostIP port");
    42             }
    43             else
    44             {
    45                 m_hostIP = args[0].ToString();
    46                 m_port = int.Parse(args[1].ToString());
    47                 rt = true;
    48             }
    49 
    50             if (rt)
    51             {
    52                 mblist = new ArrayList();//组成员列表
    53 
    54                 IPAddress m_ipA = IPAddress.Parse(m_hostIP);
    55                 m_EndPoint = new IPEndPoint(m_ipA, m_port);
    56                 m_server = new UdpClient(m_EndPoint);
    57 
    58                 Console.WriteLine("Ready for Connect......");
    59                 while (true)
    60                 {
    61                     data = m_server.Receive(ref m_EndPoint);//接收数据
    62                     m_ReturnData = UDPComm.DecodingASCII(data);
    63 
    64                     if (m_ReturnData.IndexOf("ADD") > -1)//加入组
    65                     {
    66                         AddMember(m_EndPoint);
    67                         Console.WriteLine(m_EndPoint.ToString() + "has added to group");
    68                     }
    69                     else if (m_ReturnData.IndexOf("DEL") > -1)//退出组
    70                     {
    71                         DelMember(m_EndPoint);
    72                         Console.WriteLine(m_EndPoint.ToString() + "has delete from group");
    73                     }
    74                     else
    75                     {
    76                         if (mblist.Contains(m_EndPoint))//转发数据
    77                         {
    78                             SendToMember(m_ReturnData + "[" + m_EndPoint.ToString() + "]");
    79                             Console.WriteLine(m_ReturnData + "[" + m_EndPoint.ToString() + "]"
    80                                 + "has resented to member");
    81                         }
    82                     }
    83                 }
    84                 m_server.Close();
    85             }
    86         }
    87     }
     1 class UDPClient
     2     {
     3         static void Main(string[] args)
     4         {
     5             string m_hostIP = "127.0.0.1";
     6             int m_port = 6666;
     7             UdpClient m_client;
     8             bool rt = false;
     9             byte[] data;
    10             string m_SendData, m_ReturnData;
    11             IPEndPoint m_EndPoint;
    12 
    13             //从命令行提取服务器地址和侦听端口
    14             if (args.Length < 2)
    15             {
    16                 Console.WriteLine("Usage: UDPClient hostIP port");
    17             }
    18             else
    19             {
    20                 m_hostIP = args[0].ToString();
    21                 m_port = int.Parse(args[1].ToString());
    22                 rt = true;
    23             }
    24 
    25             if (rt)
    26             {
    27                 IPAddress m_ipA = IPAddress.Parse(m_hostIP);
    28                 m_EndPoint = new IPEndPoint(m_ipA, m_port);
    29 
    30                 IPAddress m_ipA1 = IPAddress.Parse("127.0.0.1");
    31                 IPEndPoint m_EndPoint1 = new IPEndPoint(m_ipA1, 8060);
    32                 //m_client = new UdpClient(); 不绑定本地终结点,系统将会随机分配一个终结点
    33                 m_client = new UdpClient(m_EndPoint1);
    34                 
    35                 m_client.Connect(m_EndPoint);
    36 
    37                 while (true)
    38                 {
    39                     Console.WriteLine("Input [ADD|DEL|REF|QUIT|Message]:");
    40                     m_SendData = Console.ReadLine();
    41 
    42                     if(m_SendData.IndexOf("QUIT")>-1)//退出
    43                     {
    44                         m_SendData = "DEL";
    45                     }
    46 
    47                     if(m_SendData.IndexOf("REF")<=-1)//刷新显示
    48                     {
    49                         data = UDPComm.EncodingASCII(m_SendData);
    50                         m_client.Send(data, data.Length);
    51                     }
    52                     if (m_SendData.IndexOf("QUIT") > -1)
    53                         break;
    54 
    55                     data = m_client.Receive(ref m_EndPoint);
    56                     Console.WriteLine(m_EndPoint.ToString());
    57                     m_ReturnData = UDPComm.DecodingASCII(data);
    58                     Console.WriteLine(m_ReturnData);
    59                 }
    60                 //退出
    61                 Console.WriteLine("Byte!");
    62                 m_client.Close();
    63             }
    64         }
    65     }
  • 相关阅读:
    [置顶] MySQL Cluster初步学习资料整理--安装部署新特性性能测试等
    ubuntu下设置开机自启动项
    【JSP】Cookie的使用及保存中文,并用Cookie实现购物车功能
    汉语-词语:笑面虎
    汉语-词语:阴险
    汉语-词语:奸猾
    汉语-词语:奸诈
    汉语-词语:厚道
    汉语-词语:忠厚
    汉语-词语:狡猾
  • 原文地址:https://www.cnblogs.com/LOGHG/p/9929838.html
Copyright © 2020-2023  润新知