Created
January 22, 2019 04:32
-
-
Save umpteenthdev/d09c56a279a49c9be00c338b837c0857 to your computer and use it in GitHub Desktop.
How to check internet connection fastly
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.net.InetSocketAddress | |
| import java.net.Socket | |
| class SocketConnectionChecker( | |
| private val timeout: Int = 1000, | |
| private val hostname: String = "8.8.8.8", | |
| private val port: Port = Port.DNS | |
| ) { | |
| fun isConnected(): Boolean = | |
| try { | |
| val address = InetSocketAddress(hostname, port.value) | |
| Socket().use { | |
| it.connect(address, timeout) | |
| true | |
| } | |
| } catch (th: Throwable) { | |
| false | |
| } | |
| enum class Port(val value: Int) { | |
| DNS(53), HTTP(80) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment