Page 1 of 1

Using "Wait For Messages" with "Open Socket"

Posted: Sat Jan 04, 2014 10:48 am
by Simon Knight
I am writing some code that gathers data from my adsl router using its telnet interface. The code uses that open socket command to trigger the log on to the router. It is important that the log on completes before any attempt is made to read data.

My development code had the log on and request for data being called following a button press. When I attempted to place the log on and request data in a single handler things started to go wrong. The problem is the log on handler returns and allows the call for data to be made before the log on is complete.

The documentation on sockets suggests that the "wait for messages" command be used to prevent this problem, but using it just prevented the log on completing. I have a work around of having my log on routine call the first data request routine but its a little ugly. My aim is/was to have the call to handler "ReadStatistics" made from the same routine that calls "OpenTelnetSession". I don't understand what the wait for messages is doing but suspect it is preventing the read and writes to the socket that are called by handler "connected".

Is there a way of preventing "OpenTelnetSession" from returning before the log on is complete?

Code: Select all

on OpenTelnetSession
   try
   // opens a standard socket for telnet
   put "192.168.1.1:23" into sTelnetSocket
   
   if sTelnetSocket is among the lines of opensockets() then
      close socket sTelnetSocket
   else
      open socket sTelnetSocket with message "connected"
      -- wait for messages -- does not solve problem and causes the log on to fail
   end if
   
catch tErr
   answer "Error: OpenTelnetSession " & tErr
   end try
end OpenTelnetSession

On connected pSocket
   try
   //calledby the action of openning the socket for telnet to the vigor
   Read from socket pSocket until ":" with message "ReadLogIn"
   --now log in
   -- first with account name
  put fld"Account" into tAccountData
  write tAccountData & cr to socket pSocket
  Read from socket pSocket until ":" with message "ReadLogIn"
  
  --next the password
  put fld"Password" into tAccountData
  write tAccountData & cr to socket pSocket
  Read from socket pSocket until ">" with message "ReadLogIn"
  
  -- Should be logged in to the router at this point.
  -- Safe to call the data request routine
  ReadStatistics
  catch tErr
   answer "Error: connected " & tErr
   end try
  
end connected

On ReadLogIn pPort pMessage
   try
   put pMessage   after fld"console"
   if the last word of pMessage is ">" then
      put "Log On Complete" & cr after fld "console"
   end if
   catch tErr
   answer "Error: ReadLogIn " & tErr
   end try
end ReadLogIn

Re: Using "Wait For Messages" with "Open Socket"

Posted: Sat Jan 04, 2014 9:31 pm
by Mark
Hi Simon,

Telnet always sends a message back. Just read form socket with messages and you'll be fine. I wrote two blog posts about this a long time ago. These articles explain how a basic telnet client and server work.

Kind regards,

Mark