Page 1 of 1

playing with sockets

Posted: Sun Feb 19, 2012 5:35 pm
by josemv
Hello,

am trying to do a basic socket client for teaching purposes to show how http protocol works: send a request to host, get and print response.

Based on some forum example I did this, sending a string like GET /index.php HTTP/1.1

Code: Select all

local testServer

put "tontos.com:80" into testServer

command writeit
   write field "Message" & format("\r\n") to socket testServer
   put empty into field "Message"
end writeit

command connectIt
   put "entering into connectIt" & cr after field "logField"
   if testServer is among the lines of the opensockets then
      close socket testServer
      set the label of button "Connect" to "Connect"
   else
      open socket to testServer
      set the label of button "Connect" to "Disconnect"
      if the Result <> "" then
         put "result:" && the result
      else
         readLoop
      end if
   end if
end connectIt

command readLoop
   put "entering into readLoop2" & cr after field "logField"
   if testServer is among the lines of the opensockets then  
      put "readLoop in opensockets 2" & cr after field "logField"
      read from socket testServer until empty
      put it after field "logField"                                   --this never returns anything
      send "readLoop" to me in 50 milliseconds
   end if
end readLoop
What's wrong?

Another simple question when run code using browse tool, program never restarts, continue in the same state left in previous run, why?

Thanks
Jose

Re: playing with sockets

Posted: Sun Feb 19, 2012 6:33 pm
by Klaus
Hola Jose,

I am no socket expert, but you should decarae the local variable inside of some handler and not let it stand "solo" in the script! :)

Like this:

Code: Select all

local testServer

command writeit
   write field "Message" & format("\r\n") to socket testServer
   put empty into field "Message"
end writeit

command connectIt

   ### Here looks like a good place:
   put "tontos.com:80" into testServer

   put "entering into connectIt" & cr after field "logField"
 ...
Best

Klaus