简体   繁体   English

.Net类以检测通过电缆连接的计算机

[英].Net classes to detect machines connected via cable

I need to connect two machines via cable and do some work with these once they are connected. 我需要通过电缆连接两台机器,并在它们连接后进行一些处理。 Are there any .Net classes which would let me detect two machines connected via cable (when I say cable I do not mean computers on same network but machines which are physically connected)? 是否有任何.Net类可让我检测通过电缆连接的两台计算机(当我说电缆时,我不是指同一网络上的计算机,而是物理连接的计算机)?

I did come across some links on stackoverflow but none seem to have been answered. 我确实遇到了一些关于stackoverflow的链接,但似乎都没有得到解决。 Hence any links to previous relevant posts are welcome. 因此,欢迎链接到以前的相关职位。

There isn't a .NET class that will just give you this but there are probably some techniques you could use, to varying degrees of success: 没有一个.NET类可以为您提供此功能,但是您可以使用某些技巧来取得不同程度的成功:

  1. If you have access to both machines as part of your process, set a pre-defined static IP address on each device, then if you make sure that only the Ethernet port is active on both devices just wait until you can ping your predefined static IP address and you know they're connected. 如果在处理过程中可以同时访问两台计算机,请在每个设备上设置一个预定义的静态IP地址,然后确保在两个设备上只有以太网端口处于活动状态,请等待直到可以ping通您的预定义静态IP地址,并且您知道它们已连接。

  2. Something like https://stackoverflow.com/a/12659133/1742551 , running packet sniffing code on one machine to detect when activity is happening on the other end of the cable... not sure how you'd then tell what the IP address of the other machine is though and I guess you'd need this in order to do the work you describe. https://stackoverflow.com/a/12659133/1742551之类的东西,在一台机器上运行数据包嗅探代码,以检测电缆另一端何时发生活动……不知道该如何判断IP虽然是另一台机器的地址,但我想您需要用它来完成您描述的工作。

  3. You could use the TcpListener class to listen for connections on a specific port number, then from the other machine scan all other available IPs on the same subnet as itself for a device which is listening on this port (assuming that you can ensure both machines are on the same subnet). 您可以使用TcpListener类侦听特定端口号上的连接,然后从另一台计算机扫描与该端口本身相同的子网上的所有其他IP,以查找正在侦听此端口的设备(假设您可以确保两台计算机都在在同一子网中)。 Assuming this connection is the only connection available, and both devices have no static ip address set, Windows will autoconfigure the IP address due to the lack of DHCP server then they'll both be assigned 169.254.xy ip addresses as part of the 255.255.0.0 subnet, so this should work. 假设此连接是唯一可用的连接,并且两个设备都没有设置静态IP地址,则Windows将由于缺少DHCP服务器而自动配置IP地址,然后它们都将被分配169.254.xy IP地址作为255.255的一部分。 0.0子网,所以这应该工作。

  4. Probably some other option that I haven't thought of yet, but you get the point... 我可能还没有想到的其他选择,但是您明白了……

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM