• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java code to test whether connection to internet can be established

 
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've made a small program to test whether the current TCP/IP settings is good to establish an internet connection.



The working IP settings should be auto obtain (DHCP) IP. So Naturally the first HTTPURLConnection try block (line 17-24) should return 200, while the second one (line 28-35) should return 404. But somehow it still returns 200. I suspect that the IP is still in the middle of changing (the change is not complete yet) when the second try block runs. Any way to solve this?
And while we're at it, is there a way to set the timeout limit? because when connection cannot be established, the con.connect() takes quite a while before throwing an exception.
FYI, I've also tried InetAddress.getByName(host).isReachable(int TimeOut), but it always returns false eventhough i can access google.com in the browser.
 
Rancher
Posts: 436
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What happens if you wait for the process you started to finish?
 
Hendra Kurniawan
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How do I do that? what's the syntax? Can you give me the snippet? Thanks.
 
Hauke Ingmar Schmidt
Rancher
Posts: 436
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have to use the appropriate method in the Process object returned from the exec method.
 
Hendra Kurniawan
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
After some googling, using Process.waitfor() is not recommended. So I tried Apache's commons-exec



Line 23-25 gave an error : "The following command was not found: "interface ip" "set address" dhcp."
Helps please?
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic