我有一个 WCF 服务器库和客户端 [Winform],我使用自托管 winform 启动服务器。
我想让客户端发现 LAN 上的活动服务器,以另一种方式获取列表中的在线服务器 IPAddresses。我试过了DiscoveryClient
,UdpDiscoveryEndpoint
它起作用了,这是代码:
客户端[Winform]:
private void button1_Click(object sender, EventArgs e)
{
DiscoveryClient discoveryClient = new DiscoveryClient(new UdpDiscoveryEndpoint());
FindResponse findResponse = discoveryClient.Find(new FindCriteria(typeof(IFileManager)));
foreach (EndpointDiscoveryMetadata ep in findResponse.Endpoints)
{
listView1.Items.Add(ep.Address.ToString());
}
}
服务器[Winform]:
ServiceHost host = new ServiceHost(typeof(MainService));
private void button1_Click(object sender, EventArgs e)
{
if (button1.Text == "Start")
{
host.Description.Behaviors.Add(new ServiceDiscoveryBehavior());
host.AddServiceEndpoint(new UdpDiscoveryEndpoint());
host.Open();
button1.Text = "Stop";
}
else
{
host.Close();
button1.Text = "Start";
}
}
但它没有像我预期的那样工作!
更新:
在我使用 WCF 之前,我已经在 TCP 套接字上开发了一个服务器发现,我使用了 try/catch 的并行线程来连接和循环 255 个 IP 地址,例如:(192.168.1.X),我在 LAN 5 服务器上尝试过[ PC],结果是如此完美和快速,我坐了一个超时(3 秒)
,但在 WCF 上我不知道我将如何完成这个!