- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Checking internet connectivity in Javan
Internet connectivity can be checked using java.net.URL and java.net.URLConnection class. Following are the required steps.
Create a URL object and pass it the URL say Google
Call URL.openConnection() method to get a URLConnection object.
Call URLConnection.connect() method to check the internet connectivity. connect() method opens a communications link to the resource referenced by the passed URL if a connection has not already been established.
Example
import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; public class Tester { public static void main(String[] args) { try { URL url = new URL("http://www.google.com"); URLConnection connection = url.openConnection(); connection.connect(); System.out.println("Internet is connected"); } catch (MalformedURLException e) { System.out.println("Internet is not connected"); } catch (IOException e) { System.out.println("Internet is not connected"); } } }
Output
Internet is connected
- Related Articles
- How to Check Internet Connectivity in Java?
- Challenges in Connectivity in IoT
- Connectivity of Graph
- Connectivity in a directed graph
- Checking for Null or Empty in Java.
- Database Connectivity using C/C++
- Connectivity, Distance, and Spanning Trees
- Checking for a Leap Year using GregorianCalendar in Java
- Checking if a HashSet contains certain value in Java
- Fix Connectivity error in Java MySQL connection for connector to be set to class path?
- Checking for valid email address using regular expressions in Java
- How to test WinRM connectivity using PowerShell?
- Checking for Null or Empty or White Space Only String in Java.
- Checking memory_limit in PHP
- How to test remote computer connectivity using PowerShell?

Advertisements