如何确定 Windows 计算机的身份?
我有一个由服务器和客户端进程组成的程序。 两者都运行在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
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.
上一个有关生成唯一计算机 ID 的问题可能会有所帮助。
链接到之前提到 MachineGUID 的答案
添加 API 非常简单到报告其机器名称的服务器。 环境变量是计算机名。 客户可以检查一下,对吗?
您需要处理这些情况吗?
网卡将有一个唯一的 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?
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?
尝试建立环回 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.