zoukankan      html  css  js  c++  java
  • .NET 简单实现广播

    代码
        class Program
        {

            
    static bool connecting = true;
            
    static void Main()
            {
                Received();
                
    while (connecting)
                {
                    
    string content = Console.ReadLine();
                    
    if (content.Length > 0)
                    {
                        
    if (string.Compare(content, "<Stop>"true== 0)
                        {
                            Console.WriteLine(
    "关闭...");
                            connecting 
    = false;
                        }
                        
    else
                        {
                            Send(content);
                        }
                    }
                }
                Console.ReadKey();
            }

            
    public static void Send(string content)
            {
                Socket sock 
    = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                
    //IPEndPoint iep1 = new IPEndPoint(IPAddress.Broadcast, 9050);//255.255.255.255 
                IPEndPoint iep2 = new IPEndPoint(IPAddress.Parse("192.168.100.255"), 9050);
               
    // string hostname = Dns.GetHostName();
                byte[] data = Encoding.ASCII.GetBytes(content);
                sock.SetSocketOption(SocketOptionLevel.Socket,
                SocketOptionName.Broadcast, 
    1);
                
    //sock.SendTo(data, iep1);
                sock.SendTo(data, iep2);
                sock.Close();
            }

            
    public static void Received()
            {
                ThreadPool.QueueUserWorkItem((x) 
    =>
                {
                    Socket sock 
    = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                    IPEndPoint iep 
    = new IPEndPoint(IPAddress.Any, 9050);
                    sock.Bind(iep);
                    EndPoint ep 
    = (EndPoint)iep;

                    
    byte[] data;
                    
    int recv;
                    
    string stringData;
                    Console.WriteLine(
    "接听开启...");
                    
    while (connecting)
                    {
                        data 
    = new byte[1024];
                        recv 
    = sock.ReceiveFrom(data, ref ep);
                        stringData 
    = Encoding.ASCII.GetString(data, 0, recv);
                        Console.WriteLine(
    "信息: {0} 来自: {1}", stringData, ep.ToString());
                    }
                    Console.WriteLine(
    "接听关闭...");
                    sock.Close();
                });
            }

        }
  • 相关阅读:
    stringstream用法
    C# WinForm设置窗口无边框、窗口可移动、窗口显示在屏幕中央、控件去边框
    C# WinForm设置窗口大小不可调,取消最大、最小化按键
    LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal 由前序和中序遍历建立二叉树 C++
    LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序遍历建立二叉树 C++
    LeetCode 112. Path Sum 二叉树的路径和 C++
    LeetCode 101. Symmetric Tree 判断对称树 C++
    LeetCode 100. Same Tree 判断两棵二叉树是否相等 C++
    C# winform三种方法判断文本框textBox内容是否为空
    LeetCode 145. Binary Tree Postorder Traversal 二叉树的后序遍历 C++
  • 原文地址:https://www.cnblogs.com/sofire/p/1738627.html
Copyright © 2011-2022 走看看