Socket questions

Douglas Ruisaard dougr at telus.net
Sat Aug 18 16:38:57 EDT 2018


Experimenting with sockets in LC .. Windows 7 Pro, LC v8.1.9.  I have tsnet working fine in another project but tsnet appears to be primarily for "sending" stuff (sorry for the technical jargon).  Of course, tsnet can read after it sends a request.  However, sockets let me simply open and READ messages, which is what I need in this particular setting.

I am using a very simple stack with one card and one button on that card with my code at the end of this message.  The "strange" URL ("Kitchen.local:80") is the assigned ID for an ESP8266 WiFi module.

This script works fine if the module is "on-line" and responding .. returns the following from the code running on the ESP8266, as expected:

Data read from socket Kitchen.local:80
HTTP/1.0 200 OK
Content-Type: Kitchen.local/getinterrupt ... NO interrupts ... |2018 8 18|10 49 14|Saturday
Content-Length: 0
Connection: close
going to sleep

ps.. I am "mis-using" some of the HTML fields for different "content" than normal.. but this is OK and works according to my needs.. not the issue here.

If I substitute "www.google.com:80" as the socket ID, the "socketTimeout" messages gets generated.  That's fine.  I'm certain Google does not generate an output with "sleep" in it.

However, if I substitute "www.googleXXX.com:80" as the socket ID (or distort *my* ESP8266 ID to a non-existent one), I would have expected some sort of error message... trapped by the "socketError" handler ... but I get nothing.

I can't seem to get the "socketError" message (or "socketClosed" message for that matter) to trigger at all.  What I'm REALLY looking for is a "timeout" message, ideally... but a "no connect" or "no such socket" would be fine too.  But "crickets"...

I know that the "socketTimeout" only gets generated after a successful socket open.

Any help would be greatly appreciated!



local thisSocket, timeoutCount

on mouseUp
     put empty
      put "Kitchen.local:80" into thisSocket
      //put "www.google.com:80" into thisSocket
      put 5 into timeoutCount
     
     set the socketTimeoutInterval to 2000
     open socket to thisSocket
     read from socket thisSocket until "sleep" with message "socketFinishedReading"
     
end mouseUp

on socketFinishedReading pSocketID pDataIn
     put "Data read from socket" && pSocketID & return & pDataIn
     close socket thisSocket
end socketFinishedReading

on socketTimeout theID
     put empty
     wait 100 milliseconds with messages
     put  "The connnection: " & theID & " is not responding."
     subtract 1 from timeoutCount
     if timeoutCount < 1 then
          close Socket theID
     end if     
end socketTimeout

on socketError theID, theError
     answer error "There is a problem with connection: " & theID & "Error: " & theError
     close socket theID
end socketError

on socketClosed theID
     answer warning "connection: " & theID & " has been closed"
end socketClosed


Douglas Ruisaard
Trilogy Software







More information about the use-livecode mailing list