55
\$\begingroup\$

Write a program which, according to whether the script has access to the internet, produces an output which is "Truthy/Falsey". You may try and connect to any existing site, at your own discretion (don't use a shady site which only has 10% uptime - try to keep to above 80% annual uptime). If the site is down, your program does not have to work.

It must be a standalone program or a function. You may use libraries outside of the standard library to achieve this. Standard loopholes are forbidden. This is code golf, so the code with the shortest byte-count wins.

Example pseudocode:

function a:
    try:
        connect to internet 
        return 1
    catch error:
        return 0

This is my first post on code golf, so if this violates any rules in any way or is a dupe, please alert me.

EDIT: Due to numerous suggestions, I have removed the UTF-8 byte count restriction

\$\endgroup\$
21
  • 5
    \$\begingroup\$ Instead of true and false, I recommend allowing any of our defaults for truthy and falsiness. Also, by internet, do you mean the network outside your local network? Do programs still have to work if say google is down or any other large site? \$\endgroup\$
    – Blue
    Commented Jan 13, 2017 at 13:34
  • 5
    \$\begingroup\$ Byte count is usually done in the language's native or most convenient encoding, which is not always UTF-8. Unless you a have a good reason to enforce UTF-8, I think the encoding should be left at the programmer's choice \$\endgroup\$
    – Luis Mendo
    Commented Jan 13, 2017 at 14:04
  • 5
    \$\begingroup\$ I see almost everyone is using g.gl / http://g.gl/, but to. / http://to./ seems to be one byte shorter (not all languages see it as a valid url through). \$\endgroup\$ Commented Jan 13, 2017 at 14:28
  • 12
    \$\begingroup\$ Commodore Basic: PRINT "0" \$\endgroup\$
    – Mark
    Commented Jan 13, 2017 at 19:24
  • 3
    \$\begingroup\$ The very machine I'm typing this at, is technically a part of the "Internet", as it can be accessed from the outside (via NAT and port forwarding). So, if you think of it, the "internet detection" script can probably be reduced to "true" :) \$\endgroup\$
    – zeppelin
    Commented Jan 13, 2017 at 19:55

32 Answers 32

1
2
0
\$\begingroup\$

Javascript (Nashorn), 61 bytes

print(new java.net.InetSocketAddress("to.",80).getAddress())

Based on Huntro's Java answer.

\$\endgroup\$
0
\$\begingroup\$

Lua, 90 bytes

_,c=require('socket.http').request('http://g.gl')if string.find(c,'not')then os.exit(1)end

Sends a request to g.gl - exits with code 1 if can't connect

\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.