问题
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);
}
}
回答1:
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.
Discovery (Client Side)
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);
}
Publishing (Server Side)
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();
来源:https://stackoverflow.com/questions/7842443/asynchronous-client-broadcast-receiver