检测连接到 LAN 的主机

发布于 2024-07-15 18:42:23 字数 98 浏览 6 评论 0原文

为了在 C# 中创建 lan Messenger,我需要使用相同的 lan Messenger 软件在线检测主机。 我试图寻找这样做的起点,但徒劳无功。 我需要知道如何开始工作。

In order to create a lan messenger in c# I need to detect the host computers online obviously using the same lan messenger software.
I tried searching for a starting point to do so, but in vain.
I need to know how to start the work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

哑剧 2024-07-22 18:42:23

我过去有两种方法做到这一点:让所有客户端连接到预定义的主机(简单,但需要一些客户端配置)并让主机(或客户端)通过“广播”地址广播它们的存在(例如,10.0.255.255)(硬,防火墙/NAT 会让生活变得痛苦,客户端不需要配置)。

但是,是的——如果 WCF 实现了发现协议,那就采用它。 如果它能满足您的需求,它可能比您(或大多数人)编写的任何内容都要好。

There are two ways I've done this in the past: having all the clients connect to predefined host (easy, but requires some client configuration) and having the host (or client) broadcast their existence via the the 'broadcast' address (eg, 10.0.255.255) (hard, firewalls/NATs can make life painful, clients require no configuration).

But, yes -- if WCF implements a discovery protocol, go with that. Provided it does what you want, it's probably better than anything you (or most people, for that matter) could write.

蓦然回首 2024-07-22 18:42:23

您可以使用 Windows Connection Foundation 吗? 如果是这样,您可以使用 WCF 来实现 WS-Discovery 协议。 这是简要说明

Can you use Windows Connection Foundation? If so, you could use WCF to implement the WS-Discovery protocol. Here's a brief howto.

眼睛会笑 2024-07-22 18:42:23

根据您是否计划依赖集中式服务器,您有以下选择:

1) 无服务器:当客户端上线时,他会广播其身份,要求其他客户端发送其身份。

2)集中服务器:新客户端连接,他注册到服务器并下载客户端列表。 然后,每个客户端都会收到新客户端的通知(如果使用 WCF,则通过轮询、双工合同,或通过基本套接字连接)。

第一个版本将基于 UDP 套接字。 请注意,这在本地网络之外不起作用,因为我相信这些数据包不会通过路由器。 这也可能是一个糟糕的设计,因为大量的客户端只会用数据包淹没网络。 但是,嘿,这对你来说可能就足够了。

此外,WCF 还有点对点支持,您可能会感兴趣。 这里是一篇关于它的文章。

Depending on if you plan on relying on a centralized server or not, you have options:

1) No Server: When a client comes online, he broadcasts its identity, asking for other clients to send theirs.

2) Centralized server: A new client connects, he registers to the server and downloads the list of clients. Each client is then notified (either through polling, a duplex contract if using WCF, or through basic socket connection) of the new client.

The first version would be based on UDP sockets. Notice that this does not work out of the local network as I believe those packets will not pass through routers. It is also probably a bad design because a large number of clients will just swamp the network with packets. But hey, it might just be enough for you.

Also WCF has a peer to peer support, it might be interesting for you. Here's is an article about it.

千秋岁 2024-07-22 18:42:23

您可能喜欢使用 ZeroConf(又名 Bonjour、Rendevous,有时也称为 Avahi)。

http://www.mono-project.com/Mono.Zeroconf

http://craz.net/programs/ZeroconfNetServices/

You might like using ZeroConf aka Bonjour, Rendevous, or sometimes Avahi.

http://www.mono-project.com/Mono.Zeroconf

http://craz.net/programs/ZeroconfNetServices/

睫毛上残留的泪 2024-07-22 18:42:23

使用我在 LAN Messenger 中使用的 GetComputers() 方法

using System.Diagnostics;

public static string[] GetComputers()
{
    //Process that retrieves the net view >> list of computers on the network
    Process proc = new Process();
    proc.StartInfo.FileName = "net.exe";
    proc.StartInfo.CreateNoWindow = true;
    proc.StartInfo.Arguments = "view";
    proc.StartInfo.RedirectStandardOutput = true;
    proc.StartInfo.UseShellExecute = false;
    proc.Start();

    //Reads the output of the net view.
    StreamReader sr = new StreamReader(proc.StandardOutput.BaseStream);
    string line = "";

    List<string> names = new List<string>();
    while ((line = sr.ReadLine()) != null)
    {
        if (line.StartsWith(@"\\"))
            names.Add(line.Substring(2).Split(' ')[0].TrimEnd());
    }

    sr.Close();
    proc.WaitForExit();

    //Array that contains computer names
    string[] computerNames = new string[names.Capacity];
    int i = 1;

    //Adds computers names to the list view
    foreach (string name in names)
    {
        computerNames[i] = name;
        i++;
    }

    return computerNames;
}

Use this GetComputers() method I used in my LAN Messenger

using System.Diagnostics;

public static string[] GetComputers()
{
    //Process that retrieves the net view >> list of computers on the network
    Process proc = new Process();
    proc.StartInfo.FileName = "net.exe";
    proc.StartInfo.CreateNoWindow = true;
    proc.StartInfo.Arguments = "view";
    proc.StartInfo.RedirectStandardOutput = true;
    proc.StartInfo.UseShellExecute = false;
    proc.Start();

    //Reads the output of the net view.
    StreamReader sr = new StreamReader(proc.StandardOutput.BaseStream);
    string line = "";

    List<string> names = new List<string>();
    while ((line = sr.ReadLine()) != null)
    {
        if (line.StartsWith(@"\\"))
            names.Add(line.Substring(2).Split(' ')[0].TrimEnd());
    }

    sr.Close();
    proc.WaitForExit();

    //Array that contains computer names
    string[] computerNames = new string[names.Capacity];
    int i = 1;

    //Adds computers names to the list view
    foreach (string name in names)
    {
        computerNames[i] = name;
        i++;
    }

    return computerNames;
}
萌吟 2024-07-22 18:42:23

您可以让您的应用程序侦听预定义的端口,当有人连接到该端口时发回某种确认,例如应用程序名称和 IP。

因此,当新客户端上线时,它会发送广播 UDP 数据包来通告其 IP。 其他客户端将在相关端口上侦听这些“公告”,并将其自己的 IP(通过其他端口)发送回发送者。 因此,现在 LAN 上所有正在运行的客户端都将使用其 IP 来联系初始发送者。 其他客户端还可以记录“公告”发送者的 IP - 这将使他们也能够保留网络上其他客户端的最新列表。

为了保持列表最新,您可以让所有客户端以半随机间隔(例如每分钟 + rand(10) 秒)在网络上广播它们的存在。

以上所有内容都假设您正在谈论同一网络上的客户端。 如果您在互联网上执行此操作,则需要一些中心点来跟踪登录客户端的 IP 地址。

You could have your application listen on a predefined port, and when somebody connects to that port send back some kind of acknowledgement like the application name and IP.

So when a new client comes online, it would send out a broadcast a UDP packet advertising it's IP. Other clients would listen for these "announcements" on the relevant port, and send back to the sender their own IP (via some other port). So now the initial sender will be contacted by all running clients on the LAN with their IPs. The other clients could also record the IP of the sender of the "announcement" - this will let them keep an up to date list of other clients on the network too.

To keep the lists current, you could have all the clients broadcast their existance on the network at semi-random intervals (eg every minute + rand(10) seconds).

All of the above is assuming you're talking about clients on the same network. If you're doing this on the internet, you'll need some central point that will keep track of the ips of logged in clients.

空气里的味道 2024-07-22 18:42:23

我觉得可以ping一下广播ip,得到大家的答复。 或类似的东西。 可以从这里开始,然后检查客户端是否正在侦听某个端口或其他端口。

另一种方法是让客户端告诉服务器“我们还在这里!” 每隔一段时间。

I think it is possible to ping the broadcast ip and get an answer from everyone. Or something like that. Could start with that, and then check if the clients are listening on some port or something.

Alternative is to let the clients tell the server that "we are still here!" every so often.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文