异步客户端广播接收器

编程入门 行业动态 更新时间:2024-10-19 16:39:37
本文介绍了异步客户端广播接收器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

I would appreciate any help/feedback on this issue. I'm developing an Asynchronous socket connection in C#, i would like to set a broadcast client receiver such that it broadcast local network servers and then it receives the messages from the local servers. the main issue is that first i want to broadcast to different servers from one client and then retrieve the ip addresses from all the servers. here is part of the client code. also the server side works fine.

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 是基于Apple Bounjour的完整.NET实现.利用此框架,您可以启动应用并查询所有可用服务及其IP地址.

One option would be to use an existing service discovery project. ZeroConf is a full .NET implementation that is based on Apple's Bounjour. Utilizing this framework will allow you to start your app and query for all available services and their IP addresses.

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

Code is a direct copy of project documentation but posted to show ease-of-use.

发现(客户端)

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();

更多推荐

异步客户端广播接收器

本文发布于:2023-11-25 23:46:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1631822.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:接收器   客户端

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!