异步客户端广播接收器

本文关键字:接收器 广播 客户端 异步 | 更新日期: 2023-09-27 18:12:08

我将非常感谢任何关于这个问题的帮助/反馈。我正在开发c#中的异步套接字连接,我想设置一个广播客户端接收器,使其广播本地网络服务器,然后从本地服务器接收消息。主要问题是,首先我想从一个客户端广播到不同的服务器,然后从所有服务器检索IP地址。下面是客户端代码的一部分。服务器端也可以正常工作。

public void ButtonConnectOnClick()
    {
        // Init socket Client
        newsock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
        newsock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
        IPAddress ipAddress = IPAddress.Broadcast;  //Parse(txtServerIP.Text);
        IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, BROADCASTPORT);
        epServer = (EndPoint)ipEndPoint;
        string tmp = "hello";
        byteData = Encoding.ASCII.GetBytes(tmp);
        newsock.BeginSendTo(byteData, 0, byteData.Length, SocketFlags.None, epServer, new AsyncCallback(OnSend), null);
        byteData = new byte[1024];
        newsock.BeginReceiveFrom(byteData, 0, byteData.Length, SocketFlags.None, ref epServer, new AsyncCallback(OnReceive), null);
    }
    private void OnSend(IAsyncResult ar)
    {
        try
        {
            newsock.EndSend(ar);
        }
        catch (ObjectDisposedException)
        { }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, null, MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
    private void OnReceive(IAsyncResult ar)
    {
        try
        {
            newsock.EndReceive(ar);              
            byteData = new byte[1024];
            //Start listening to receive more data from the user
            newsock.BeginReceiveFrom(byteData, 0, byteData.Length, SocketFlags.None, ref epServer, new AsyncCallback(OnReceive), null);
        }
        catch (ObjectDisposedException)
        { }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, null, MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

异步客户端广播接收器

一种选择是使用现有的服务发现项目。ZeroConf是一个完整的。net实现,基于苹果的Bounjour。使用这个框架将允许你启动你的应用程序并查询所有可用的服务和它们的IP地址。

代码是项目文档的直接副本,但发布是为了显示易用性。

发现(客户端)

    static void Main(string[] args)
    {
        BonjourServiceResolver bsr = new BonjourServiceResolver();
        bsr.ServiceFound += new Network.ZeroConf.ObjectEvent<Network.ZeroConf.IService>(bsr_ServiceFound);
        bsr.Resolve("_daap._tcp");
        Console.ReadLine();
    }
    static void bsr_ServiceFound(Network.ZeroConf.IService item)
    {
        // Here goes the code for what you want to do when a service is discovered
        Console.WriteLine(item);
    }

发布(服务器端)

Service s = new Service();
s.AddAddress(ResolverHelper.GetEndPoint());
s.Protocol = "_touch-able._tcp.local.";
s.Name = "MyName";
s.HostName = s.Addresses[0].DomainName;
//The indexer on the service enables to set metadatas
s["DvNm"] = "PC Remote";
s["RemV"] = "10000";
s["DvTy"] = "iPod";
s["RemN"] = "Remote";
s["txtvers"] = "1";
s["Pair"] = "0000000000000001";
//After setting all this, the only thing left to do is to publish your service
s.Publish();
Thread.Sleep(3600000);
s.Stop();