[英]How do I determine the identity of a Windows machine?
I have a program consisting of a server and a client processes. 我有一个包含服务器和客户端进程的程序。 Both run on Windows systems - Windows 2000 or later versions. 两者都在Windows系统-Windows 2000或更高版本上运行。 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? 我听说每台Windows机器都有一个UUID-我可以获取它并将其用于此目的吗?
Previous question about generating a unique machine id that might help. 上一个有关生成唯一机器ID的问题,该ID可能会有所帮助。 Link to previous answer which mentions MachineGUID 链接到前面提到MachineGUID的答案
It is straightforward to add an API to the server that reports its machine name. 向报告其机器名称的服务器添加API很简单。 The environment variable is COMPUTERNAME. 环境变量是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. 网卡将具有唯一的MAC。 If both server and client report the same MAC then they are using the same network card. 如果服务器和客户端都报告相同的MAC,则它们正在使用相同的网卡。 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? 如果客户端和服务器都在不同的虚拟机上运行,但使用相同的网卡,那么您是否认为它们在同一台计算机上运行?
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. Windows网络要求计算机名称必须唯一,因此调用GetComputerName api并具有客户端和服务器交换名称(并将接收到的名称与他们看到的名称进行比较)就足够了。 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. 但是我对所有的虚拟化技术没有丰富的经验,因此不能肯定地说。
how about trying to establish a loopback tcp connection? 如何尝试建立回送TCP连接? 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. 我不知道具体如何,但是可以肯定的是Win32中与Unix中的/ proc文件系统等效(我认为Windows Taskmanager有免费的替代品,也许您可以看一下它们的源代码),您可以在其中搜索服务器处理。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.