playing with sockets

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
josemv
Posts: 11
Joined: Mon Jan 05, 2009 4:51 pm

playing with sockets

Post by josemv » Sun Feb 19, 2012 5:35 pm

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

Klaus
Posts: 14194
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: playing with sockets

Post by Klaus » Sun Feb 19, 2012 6:33 pm

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

Post Reply