April 25, 2019, 04:20:13 AM

Author Topic: Accurately update socket connection stauts?  (Read 124 times)

Offline Yellownakji

  • Full Member
  • ***
  • Posts: 171
  • Whoop-de-doo-ba-dooo!
Accurately update socket connection stauts?
« on: April 03, 2019, 12:45:27 AM »
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.


Offline Derron

  • Hero Member
  • *****
  • Posts: 1953
Re: Accurately update socket connection stauts?
« Reply #1 on: April 03, 2019, 06:51:01 AM »
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

Offline Yellownakji

  • Full Member
  • ***
  • Posts: 171
  • Whoop-de-doo-ba-dooo!
Re: Accurately update socket connection stauts?
« Reply #2 on: April 03, 2019, 07:05:57 AM »

...


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..

Offline fielder

  • Jr. Member
  • **
  • Posts: 65
Re: Accurately update socket connection stauts?
« Reply #3 on: April 03, 2019, 08:21:48 AM »

...


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...

Code: [Select]
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



Offline Yellownakji

  • Full Member
  • ***
  • Posts: 171
  • Whoop-de-doo-ba-dooo!
Re: Accurately update socket connection stauts?
« Reply #4 on: April 03, 2019, 08:43:49 AM »

...


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

Offline Derron

  • Hero Member
  • *****
  • Posts: 1953
Re: Accurately update socket connection stauts?
« Reply #5 on: April 03, 2019, 09:22:55 AM »
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


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

Offline Derron

  • Hero Member
  • *****
  • Posts: 1953
Re: Accurately update socket connection stauts?
« Reply #6 on: April 03, 2019, 10:02:00 AM »
Here is something I mocked up now:
Code: BlitzMax
  1. SuperStrict
  2. Framework Brl.StandardIO
  3. Import bah.libcurl
  4.  
  5.  
  6. Local curl:TCurlEasy = TCurlEasy.Create()
  7. 'want some more output?
  8. 'curl.setOptInt(CURLOPT_VERBOSE, 1)
  9.  
  10. 'follow redirects?
  11. curl.setOptInt(CURLOPT_FOLLOWLOCATION, 1)
  12.  
  13. 'wait 10 seconds to accept your request (eg it was in sleep mode?)
  14. curl.setOptInt(CURLOPT_CONNECTTIMEOUT, 10)
  15. 'wait 10 seconds for a response/page building
  16. curl.setOptInt(CURLOPT_TIMEOUT, 10)
  17.  
  18. curl.setOptInt(CURLOPT_PORT, 81)
  19. curl.setOptString(CURLOPT_URL, "192.168.0.12")
  20.  
  21. 'store content in an internal string instead of outputting it
  22. curl.setWriteString()
  23.  
  24. 'run it
  25. Local res:Int = curl.perform()
  26. if res <> CURLE_OK
  27.         print "error fetching the page: " + CurlError(res) +"  [code: " + res+"]"
  28. endif
  29.  
  30. Rem
  31. for error codes (read error, connection error, ...):
  32. https://github.com/maxmods/bah.mod/blob/master/libcurl.mod/consts.bmx
  33. look for "' ERROR CODES ....."
  34. End Rem
  35.  
  36.  
  37. curl.cleanup()
  38.  
  39. Print curl.toString()
  40.  

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