Define if a pc is in LAN using Java

2

I'm searching for a method to determine if a pc (whose IP address I know) is or not in LAN connection with mine. I've tried asking subnet mask of both and comparing each bit of IP addresses, but I'm asking if there's a better and more direct way to do that. Sorry for my bad english :S

Wallkan

Posted 2011-07-08T14:12:13.533

Reputation: 121

1What could be more direct than looking if both IPs inside the same subnet? – Victor Sorokin – 2011-07-08T14:15:17.620

1Oh, you want to know if both computers (yours and the other) are in the same subnet, in direct connection, without going through a router? – woliveirajr – 2011-07-08T14:16:14.617

Maybe my networking basics are a bit rusty, but what would being in the same subnet or not have to do with having a LAN connection? Anyway if you want to check if they're in the same subnet then yes checking subnet masks is obviously the way to go. – Voo – 2011-07-08T14:18:32.000

Not all IPs in your LAN will have the same subnet mask, at least when we talk about large enterprises and complex routing infrastructure. I can think of only one solution: maintain a whitelist (e.g. a file) that contains the IP address ranges you're interested in. – None – 2011-07-08T14:24:50.817

I wonder if we might be missing the bigger picture here. Why is it that you want to know whether there's a direct LAN connection? Perhaps there's an alternative check/piece of information that will serve the same purpose. – Andrzej Doyle – 2011-07-08T14:39:56.280

Sorry if maybe my question can be misunderstood! I'm looking for a method to check if a pc is not in LAN because I've to read a lot of Gbytes from it. If it's not on LAN it can take long time to complete the reading operation, so I'm trying to add a choice for users to esclude pc not in LAN connection from reading operations – Wallkan – 2011-07-08T16:10:10.383

Answers

1

It's hard to get a good answer if you're not defining the problem clearly: how do YOU tell if they're in the same LAN? Same switch, same subnet, same physical building?

Checking subnet should do the trick for most situations.

Other approach (if the two PCs have been "talking" to each other) is to check out ARP table to see if the IP is there: arp -a from command prompt. This means you have Layer 2 connectivity with it.

Vlad

Posted 2011-07-08T14:12:13.533

Reputation: 768