如何确定 Windows 计算机的身份?

发布于 2024-07-20 21:43:15 字数 239 浏览 7 评论 0原文

我有一个由服务器和客户端进程组成的程序。 两者都运行在 Windows 系统上 - Windows 2000 或更高版本。 这两个进程可以运行在同一台机器上,也可以运行在两台不同的机器上。

客户端如何判断是否与服务器运行在同一台机器上? 如果服务器没有运行,客户端无论如何都无法工作,并且不关心服务器可能在哪里 - 所以这种情况是不可能的。 我听说每台 Windows 计算机都有一个 UUID - 我可以获取它并用于此目的吗?

I have a program consisting of a server and a client processes. Both run on Windows systems - Windows 2000 or later versions. The two processes can run on the same machine or on two different machines.

How can the client determine if it is run on the same machine as the server? If the server is not running the client can't work anyway and doesn't care where the server possibly is - so this case is out of the question. I've heard that each Windows machine has an UUID - can I obtain it and use for that purpose?

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

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

发布评论

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

评论(3

但可醉心 2024-07-27 21:43:15

Windows 网络要求计算机名称是唯一的,因此调用 GetComputerName api 并让客户端和服务器交换名称(并将收到的名称与它们看到的名称进行比较)就足够了。 如果客户端和服务器可以彼此独立启动,那么您将需要某种协议来完成此过程。 客户端发起交换,而服务器仅在从客户端接收到名称时才发送其名称,这似乎是合乎逻辑的。 如果客户端看到相同的名称,则可以中止连接。

我相信大多数虚拟机系统都会允许虚拟机有自己的名称,所以你应该仍然可以在虚拟机上进行测试。 然而,我对所有虚拟化技术都没有丰富的经验,所以不能肯定。

Windows networking requires computer names to be unique, so calling the GetComputerName api and having the client and server swap names (and compare the received name to the name they see) should suffice. If the client and server can start up independently of one another then you'll need some sort of protocol for this process. It seems logical for the client to initiate the exchange, and the server to only send its name when it has received a name from a client. The client can then abort the connection if it sees the same name.

I believe most virtual machine systems will allow the virtual machine to have its own name, so it should still be possible for you to test on virtual machines. However I don't have extensive experience of all the virtualisation technologies out there, so can't say for sure.

爱殇璃 2024-07-27 21:43:15

上一个有关生成唯一计算机 ID 的问题可能会有所帮助。
链接到之前提到 MachineGUID 的答案

添加 API 非常简单到报告其机器名称的服务器。 环境变量是计算机名。 客户可以检查一下,对吗?

您需要处理这些情况吗?

  1. 客户端正在运行,但服务器没有响应,您想知道无响应的服务器是否位于远程计算机上。
  2. 客户端和服务器在同一主机上的两个不同虚拟机中运行,并且您希望将其报告为“在同一台计算机上运行”。
  3. 客户端在与运行服务器的同一台计算机托管的虚拟机中运行,并且您希望将其报告为“在同一台计算机上运行”。
  4. 客户端和服务器在不协调的网络上运行,并且两者可能被分配了相同的名称。
  5. 服务器可能是有敌意的,并且会尝试欺骗客户端。

网卡将有一个唯一的 MAC。 如果服务器和客户端报告相同的 MAC,则它们使用相同的网卡。 如果客户端和服务器都运行在不同的虚拟机中但使用相同的网卡,你认为它们运行在同一台机器上还是不同的机器上?

Previous question about generating a unique machine id that might help.
Link to previous answer which mentions MachineGUID

It is straightforward to add an API to the server that reports its machine name. The environment variable is COMPUTERNAME. The client could check that, right?

Do you need to deal with any of these cases?

  1. The client is running, but the server is not responding, and you want to know whether the unresponsive server is on a remote machine.
  2. The client and the server are running in two distinct virtual machines on the same host machine, and you want to report that as "running on the same machine."
  3. The client is running in a virtual machine hosted by the same machine as the server is running on, and you want to report that as "running on the same machine."
  4. The client and the server are running on uncoordinated networks and both might have been assigned the same name.
  5. The server is possibly hostile, and will attempt to deceive the client.

The network card will have a unique MAC. If both server and client report the same MAC then they are using the same network card. If both client and server are running in different virtual machines but using the same network card, do you consider them running on the same machine or different machines?

深海夜未眠 2024-07-27 21:43:15

尝试建立环回 tcp 连接怎么样? 或者也许检查服务器在预定义文件夹中创建的某些锁定文件...

我不知道具体如何操作,但在 Win32 中肯定有与 Unix 中的 /proc 文件系统等效的东西(我认为有免费的替代品) windows任务管理器,也许你可以看看他们的来源),你可以在其中搜索你的服务器进程。

how about trying to establish a loopback tcp connection? or maybe checking for some lock file created by the server in a predefined folder...

I don't know exactly how, but there's for sure something equivalent in Win32 to the /proc filesystem in Unix (I think there are free replacements for the windows taskmanager, maybe you could look at their sources) where you could search for your server process.

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