Accurately update socket connection stauts?

Started by Yellownakji, April 03, 2019, 00:45:27

Previous topic - Next topic

Yellownakji

I've written a tool that checks if my raspberry PI is hosting a webpage on my network.   If successfull, it reports connected and if failure, it reports offline.   However, this tool does not seem to update in real time and i cannot seem to understand why?   I have the tool re-checking connection status every 10 seconds however, the tool never changes status and only reports the first status.  So, if the tool initially reported success, even  If i unplug the raspberry pi, it will still say successful even though it should be a failure. (and vice versa)

I'm using a UDP connection and i'm using SocketConnected() to report the connectivity status.  any ideas?

again, like in my previous posts, i'm new to sockets.


Derron

UDP does not have this kind of "peer is disconnected" information.

Both systems need to communicate on a regular base: A sends a heartbeat to B. B responds to A. If there is no response within time A knows that B dropped out.
UDP is not reliable so a heartbeat from A to B might have been lost inbetween. Either write your system in a way that it sends it again and again until the "package ID" got "confirmed" - or simply send multiple heartbeats and do not react on the first.
Proper confirmation is of course better.
-> each package has a unique ID and a byte saying "I need confirmation" (or not)
-> if confirmation is requested, receiver sends back a message "received package ID_x" (only once as sender resends if that package was lost)
-> if x ticks are gone (latency incorporated) and no confirmation was received then the sender sends this package again
-> if a confirmation is received the package is removed from the "toSend"-list
-> ...


bye
Ron

Yellownakji

Quote from: Derron on April 03, 2019, 06:51:01

...


This has nothing to do with packets though.   I'm trying to check if a webpage is hosting or not, meaning reachable... ping-able.  I'm not trying to send or receive anything, i just want to see if it's reachable, every few seconds.

Can't use TCP/IP because that requires confirmation which is not what i'm trying to do. So..

fielder

#3
Quote from: Yellownakji on April 03, 2019, 07:05:57
Quote from: Derron on April 03, 2019, 06:51:01

...


This has nothing to do with packets though.   I'm trying to check if a webpage is hosting or not, meaning reachable... ping-able.  I'm not trying to send or receive anything, i just want to see if it's reachable, every few seconds.

you have just to read a stream from a file at this address... usually i read a txt file...


Local fileU:TStream=ReadStream("http::www.site.site/index.html")
If fileU
     'check text to be sure that is the real index.html and not something generated by a server error.
     Local line:String=ReadLine(fileU)
     if line[..10]="xxxxxxxxxx" ... 'for example check the first 10 characters
     closefile (fileU)
else
     'there is no connection
endif




Yellownakji

Quote from: fielder on April 03, 2019, 08:21:48

...


Not quite.  ReadLine does not support IP addresses / port IPs.  You cannot do "192.169.1.99:1998", which i need.

Derron

#5
Quote from: Yellownakji on April 03, 2019, 07:05:57
This has nothing to do with packets though.   I'm trying to check if a webpage is hosting or not, meaning reachable... ping-able.  I'm not trying to send or receive anything, i just want to see if it's reachable, every few seconds.

a)
You have a webserver running somewhere ... and want to see if it is still serving a website?
-> bah.libcurl is your friend, just fetch the stuff you want. On timeout you know that something happened.

b)
you have programme running on a computer listening on some ports
-> connect via the offered protocol and handle accordingly


And of course it has to do with packets: if you use UDP then you do send unreliable packets. You have no guarantee that stuff is received or not. The same way it cannot determine if the opposite hanged up or is just not replying for a while.
In your case it returns true ... for what reason? Because it somewhere set a bool/int to true on connect and kept it that way.

As said the only reliable way to detect an UDP disconnect is by ping/pong or heartbeats. You send stuff and if X seconds are gone since that send command without a reply coming in: timeout/disconnect.


@ IP
Just tell your computer via an "hosts" file that IP XYZ resolves to "rasperry.local" or so.
Domains like "example.org/.net" or ".local/.localhost" are not registerable, so always "free to use".
at the end you have eg.
192.168.0.1 rasperry.example.org
192.168.0.2 yellownakji.example.org


That way you could even try out stuff locally instead of "live" by redirecting real domains
192.168.0.2 syntaxbomb.com


For ports you could try out the "hacks" there (if libcurl is not what you wanted):
https://stackoverflow.com/questions/8652948/using-port-number-in-windows-host-file
-> netsh


Quote from: Yellownakji on April 03, 2019, 07:05:57
Can't use TCP/IP because that requires confirmation which is not what i'm trying to do. So..
So ... if it does not confirm you know it is dead/no longe responding/timing out.
This is exactly what you want - so why not TCP/IP ?
As there is no little server-app running accepting connections (if it was alive)?


bye
Ron

Derron

Here is something I mocked up now:
Code (BlitzMax) Select

SuperStrict
Framework Brl.StandardIO
Import bah.libcurl


Local curl:TCurlEasy = TCurlEasy.Create()
'want some more output?
'curl.setOptInt(CURLOPT_VERBOSE, 1)

'follow redirects?
curl.setOptInt(CURLOPT_FOLLOWLOCATION, 1)

'wait 10 seconds to accept your request (eg it was in sleep mode?)
curl.setOptInt(CURLOPT_CONNECTTIMEOUT, 10)
'wait 10 seconds for a response/page building
curl.setOptInt(CURLOPT_TIMEOUT, 10)

curl.setOptInt(CURLOPT_PORT, 81)
curl.setOptString(CURLOPT_URL, "192.168.0.12")

'store content in an internal string instead of outputting it
curl.setWriteString()

'run it
Local res:Int = curl.perform()
if res <> CURLE_OK
print "error fetching the page: " + CurlError(res) +"  [code: " + res+"]"
endif

Rem
for error codes (read error, connection error, ...):
https://github.com/maxmods/bah.mod/blob/master/libcurl.mod/consts.bmx
look for "' ERROR CODES ....."
End Rem


curl.cleanup()

Print curl.toString()


Works as expected: altering the IP to something without a webserver here: timeout (error code 7), else I get the website output at that port (:81 is redirected to my publicIP:80, while the :80 internally is used by my NAS admin interface).


bye
Ron