Question Internetconnection?

Hello Guys, This must me a basic question.

I need a code where it detects if the computer unit HAVE or CONNECTED to internet connections.

Any Reply is so much appreciated.
-Caloykoko
 

GregTomkins

Active Member
You should probably think in terms of a specific host, not "The Internet". Here is some code (lightly edited, apologies if I missed anything) that we use to verify connection to a third party service. You'd have to replace the -H parameter with some known host and 'Server is alive' with what you expect its response to a POST to be (this is easy to test with a browser debugger or a tool like Postman). You could this with a public site such as google.com, if you just want a general check of Internet health. It's probably safe to say that is google.com isn't accessible, a nuclear war is underway and your lights are about to go out permanently.

Code:
DEF VAR p_socket AS HANDLE NO-UNDO.
CREATE SOCKET p_socket.
p_socket:CONNECT("-H server -S 80") NO-ERROR.
DEF VAR h_expected_output AS CHAR N-UNDO INIT "Server is alive":U.
DEF VAR h_expected_length AS INT NO-UNDO.
DEF VAR h_request AS CHAR NO-UNDO.
ASSIGN h_request = "POST\n".
DEF VAR h_outbuf  AS MEMPTR NO-UNDO.
SET-SIZE(h_outbuf) = LENGTH(h_request) + 1.
PUT-STRING(h_outbuf, 1) = h_request.
p_socket:WRITE(h_outbuf, 1, LENGTH(h_request)).
h_expected_length = 20.
SET-SIZE(h_buf) = h_expected_length + 1.
p_socket:READ(h_buf, 1, h_expected_length).
h_reply = GET-STRING(h_buf, 1).
IF h_reply <> h_expected_output
THEN p_error = "invalid reply during startup: ":U + h_reply.
p_socket:DISCONNECT().
 
Top