当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - .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)?

I did come across some links on stackoverflow but none seem to have been answered.

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:

  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.

  2. Something like http://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.

  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). 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.x.y ip addresses as part of the 255.255.0.0 subnet, so this should work.

  4. Probably some other option that I haven't thought of yet, but you get the point...

分享给朋友:
您可能感兴趣的文章:
随机阅读: