aspose file tools*
The moose likes Sockets and Internet Protocols and the fly likes How to check whether PC is connected to Internet programmatically using java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "How to check whether PC is connected to Internet programmatically using java" Watch "How to check whether PC is connected to Internet programmatically using java" New topic
Author

How to check whether PC is connected to Internet programmatically using java

Nishit Sharma
Greenhorn

Joined: Oct 17, 2012
Posts: 5
I have 2 approaches through which we can find out whether our pc is connected to internet or not :

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.

2) You can use java.net.NetworkInterface to find out various network interfaces that are connected.

I want to use the 2nd approach as it doesn't depend upon any websites to check for internet connection. So, can anybody tell me how to do it.
Pushkar Choudhary
Rancher

Joined: May 21, 2006
Posts: 425

Nishit Sharma wrote:
I want to use the 2nd approach as it doesn't depend upon any websites to check for internet connection. So, can anybody tell me how to do it.


So, what have you tried so far? Are you facing any problems with your code?
Nishit Sharma
Greenhorn

Joined: Oct 17, 2012
Posts: 5
Nishit Sharma
Greenhorn

Joined: Oct 17, 2012
Posts: 5
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
Paul Witten
Ranch Hand

Joined: Oct 10, 2012
Posts: 86
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 ....


When Connected:

Ethernet adapter Local Area Connection:

Connection-specific DNS Suffix . :
Description . . . . . . . . . . . : Realtek RTL8102E Family PCI-E Fast Ethernet NIC (NDIS 6.0)
Physical Address. . . . . . . . . : 00-1F-16-7F-69-D0
DHCP Enabled. . . . . . . . . . . : Yes
Autoconfiguration Enabled . . . . : Yes
Link-local IPv6 Address . . . . . : fe80::9116:51e9:d392:bb77%10(Preferred)
IPv4 Address. . . . . . . . . . . : 192.168.5.100(Preferred)
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Lease Obtained. . . . . . . . . . : Tuesday, October 09, 2012 12:54:44 PM
Lease Expires . . . . . . . . . . : Friday, October 19, 2012 7:06:21 PM
Default Gateway . . . . . . . . . : 192.168.5.1
DHCP Server . . . . . . . . . . . : 192.168.5.1
DHCPv6 IAID . . . . . . . . . . . : 167780118
DHCPv6 Client DUID. . . . . . . . : 00-01-00-01-11-AD-B9-66-00-24-2C-89-10-ED
DNS Servers . . . . . . . . . . . : 192.168.25.1
NetBIOS over Tcpip. . . . . . . . : Enabled


When disconnected:

Ethernet adapter Local Area Connection:

Media State . . . . . . . . . . . : Media disconnected
Connection-specific DNS Suffix . :
Description . . . . . . . . . . . : Realtek RTL8102E Family PCI-E Fast Ethernet NIC (NDIS 6.0)
Physical Address. . . . . . . . . : 00-1F-16-7F-69-D0
DHCP Enabled. . . . . . . . . . . : Yes
Autoconfiguration Enabled . . . . : Yes
Nishit Sharma
Greenhorn

Joined: Oct 17, 2012
Posts: 5
I asked some network people, as per them media state : active represents that your pc is on the network or not. It doesn't tell you whether your pc is connected to internet or not.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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."
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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?
Nishit Sharma
Greenhorn

Joined: Oct 17, 2012
Posts: 5
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.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36508
    
  16
Question too difficult for “beginning”. Moving discussion.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36508
    
  16
I hope I have put it in the right place.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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.

2) You can use java.net.NetworkInterface to find out various network interfaces that are connected.

I want to use the 2nd approach as it doesn't depend upon any websites to check for internet connection. So, can anybody tell me how to do it.

...#1 is the only way to do it. Not possible with #2. (Even if #2 does provide some 'isConnectedToInternet()' method, it can only work by doing #1).
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18135
    
    8

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.
 
Don't get me started about those stupid light bulbs.
 
subject: How to check whether PC is connected to Internet programmatically using java
 
Similar Threads
check that whether our PC is connected with internet
Configuring Oracle 10 G to Work with Java Hibernate
How to check internet connection is alive or not.
check that whether our PC is connected with internet
can we use ServerSocket and Socket for Wide area network?