This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Murach's Python Programming and have Michael Urban and Joel Murach on-line!
See this thread for details.
Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Java code to test whether connection to internet can be established  RSS feed

 
Hendra Kurniawan
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
  • 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.
 
Hauke Ingmar Schmidt
Rancher
Posts: 436
2
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!