Nishit Sharma wrote:you can see the code and the output, here en0 represents ethernet. Ethernet represents both LAN and internet. Now, how to distinguish whether pc is connected to LAN or Internet
The isUp() method doesn't seem to test for connectivity though. It tests to see if the adapter is functioning properly.
Seems to me that you need to test SOMETHING like pinging an IP Address but you don't want to do that.
A crazy idea would be to shell out and run ipconfig /all. When connected an adapter will have a lot more data like the DNS server, etc. In fact Media State Disconnected may be all you need. Seems like a hack but what the heck ....
There really is no such thing as "connected to the internet." Although people use it all the time informally, it's not well defined in a strict technical sense.
The only way to find out if you're "connected to the internet" in the way that you're probably thinking of is to do just what you don't want to do--try to hit a few well-known sites until one responds. That's the only way to do it because that matches the informal definition that people have in mind when they talk about being "connected to the internet." If you don't want to do that, then you will have to come back with a clear and precise definition of what you mean by "connected to the internet."
how to distinguish whether pc is connected to LAN or Internet
Wait a minute. What exactly do you mean here? We can be on a LAN and still have internet access, so your "or" doesn't go with what's on both sides of it.
Are you talking about
A) Having access to our LAN and the internet vs. access only to our LAN?
B) Having a public, and possibly publicly routable, IP address vs. a private, non-publicly-routable address?
C) Something else?
Joined: Oct 17, 2012
Jeff, i mean to say that sometimes it happens that our pc is on the network, we have access to our company's LAN but at the same time we cannot access internet may be due to some proxy settings. So, my question was in this case how can i come to know whether my pc can access internet using a java program.
Nishit Sharma wrote:Jeff, i mean to say that sometimes it happens that our pc is on the network, we have access to our company's LAN but at the same time we cannot access internet may be due to some proxy settings. So, my question was in this case how can i come to know whether my pc can access internet using a java program.
The only way you can know if you can access the internet is to try to do so. For instance, try to establish a connection to www.google.com, port 80. Of course, the results of that don't tell you about the whole internet, only about that one host. If it fails, it may be that your company's firewall has blocked google.com, but everything else will be fine. Or it may be that google is down, but other hosts will be reachable. On the other side, if it succeeds, it's 100% possible (though unlikely) that your company's firewall is blocking connections to everything except google.com, so attempting to connect to any other host will fail.
So, again, there really is no such thing as "connected to the interent," in the sense that you can't say, "I'm connected to the internet, so therefore I can communicate with any other host that is connected to the internet." The only way to know if you can communicate with any particular host is to try it. The best you can do for "connected to the internet" is pick one or a handful of hosts, establish connections, and assume that the rest of the internet either will or will not be reachable as those hosts are. There's no guarantee, but it's the best you can do.
In other words...
Nishit Sharma wrote:
1) You can try connecting to sites such as : google.com, Facebook.com, yahoo.com etc, if you are able to connect to any of these sites that means your pc is connected to internet.
My Windows 7 PC has a little icon on the task bar which tells me the name of the LAN it's connected to, and whether it has Internet access. At least that's what it claims to do, and it seems to be reliable when it claims it doesn't have Internet access.
And when you see something like that, it's reasonable to believe that it is actually possible to determine whether your computer has Internet access. But I don't know how Windows does that. Somehow I doubt that it's pinging some computer in the Microsoft domain. If I were going to solve the problem at a low level (for implementing in an operating system like Windows 7) I would try to connect to something which isn't in the LAN but is adjacent to it. That would usually be a machine in your ISP's domain, which is indeed in the Internet. And using something like tracert while connecting to a known site, you could find out what machine that was.
It's also true, I believe, that if you can connect to that machine then you can connect to anything in the Internet (firewalls aside), and if you can't connect to it then you can't connect to anything in the Internet. However I expect there could be more complicated network setups where that doesn't apply -- I am not a network expert.
And as you can see, I don't really care whether it's possible for my Java code to tell whether it's possible to "connect to the Internet" because I already have a tool which tells me that. And there's no need for a Java program to know that for its own use, because (as already pointed out) the program is not going to "connect to the Internet". It's going to connect to host X.