• C#广播消息收发


    发送消息:

     static void Main(string[] args)
            {
                UdpClient udpClient = new UdpClient(AddressFamily.InterNetwork);
                IPEndPoint broadcastIp = new IPEndPoint(IPAddress.Broadcast, 4534);
                int i = 0;
                while (true)
                {
                    i++;
                    byte[] b = Encoding.UTF8.GetBytes("我在广播消息" + i);
                    udpClient.Send(b, b.Length, broadcastIp);
                    Console.WriteLine(i);
                    System.Threading.Thread.Sleep(1000);
                }
                //udpClient.Close();
                //Console.WriteLine("Done");
                //Console.Read();
            }

    接收消息:

    static void Main(string[] args)
            {
                try
                {
                    UdpClient receiveUdpClient = new UdpClient(1);
                    IPEndPoint localIP = new IPEndPoint(IPAddress.Any, 0);
                    while (true)
                    {
                        byte[] r = receiveUdpClient.Receive(ref localIP);
                        Console.WriteLine(Encoding.UTF8.GetString(r));
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message + ex.StackTrace);
                }
            }
  • 相关阅读:
    闭包
    this
    函数声明,表达式,构造函数
    算法学习_栈
    LeetCode刷题_140
    2020/3/20 刷题
    2020/3/19 刷题
    2020/3/13_C++实验课
    刷题(主要是DFS) 2020年3月12日
    DFS的一些题2020/3/11
  • 原文地址:https://www.cnblogs.com/langu/p/3125902.html
Copyright © 2020-2023  润新知