Sockets! Confused..

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
hoburne
Posts: 40
Joined: Tue Oct 04, 2011 11:13 am

Sockets! Confused..

Post by hoburne » Fri Feb 03, 2012 4:19 pm

Hi,

I am looking for help using sockets. I have read the documentation and posts on this fourm and got a good start but I am now stuck and confused.
I have a network appliance that spits out a string of text for every transaction it handles. My aim is to write a simple stack to connect to it and read every string it throws out and add it a MySQL db for analysis later.

The stack I have connects, waits until it gets a string then displays it (for debug) then does nothing. This naybe a problem with my miss understanding of handlers or something simple! i just need pointing in the right direction please.

My Code Below:

Code: Select all

on mouseUp
   if the openSockets contains fld "fldSockets" then
      put "#" & the long system time & "> already connected" & cr before fld "fldData"
      send "gotSocketData" to me
   else
      open socket to "10.16.2.254:1752" with message "gotSocketData" 
      put the openSockets into fld "fldSockets"
   end if
end mouseUp

on gotSocketData pSocket
   read from socket pSocket until return with message "processData"
   wait for messages
end gotSocketData 

on processData pSocket, pData
   put pData & cr before fld "fldData"
   --Insert to MySQL
end processData 

on socketError
   put "##Socket Error##" before fld "fldData"
end socketError

on socketTimeout
   put "#" & the long system time & "> Time out.." & cr before fld "fldData"
end socketTimeout
Ok so basically this stack has a fld called "fldData" that displays: "#14:32:01> Time out.." every 10 seconds until a transaction is completed then a string is read and displayed in fldData then nothing happens. No more time out messages, no socketError etc..

Any pointers?

Thanks, And sorry if this is a newb question!

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: Sockets! Confused..

Post by sturgis » Fri Feb 03, 2012 5:36 pm

Ok, if I read your code correctly if the socket isn't open you open it, and on success it calls your gotSocketData 1 time. Then you don't call it again. Also, i've never seen "wait for messages" before so am not sure thats a valid way to use wait.
I adjusted your code so that gotsocketdata is called the first time, it sends the message to process data, processdata checks to see if its empty or not then acts accordingly. Then set your gotSocketData to send "gotsocketData" to itself. I'm using the "read for 1 line" method. Though read until return or read until empty should work. I think read until return is blocking until a line of data is actually recieved but not sure. (sockets are new to me too) I believe that "read... for 1 line" is not blocking.

Due to the way your mouseup is implemented, I added a pending message check to avoid the possibility of multiple concurrent loops running.

If you ONLY want to grab new data when you click, (which might have been what you were going for?) THen the problem is most likely with the "wait for messages" in your gotsocketdata handler.

This is not tested, is just off the top of my head so it may require tweaking.

Code: Select all

on mouseUp
   if the openSockets contains fld "fldSockets" then
      put "#" & the long system time & "> already connected" & cr before fld "fldData"
      send "gotSocketData" to me
   else
      open socket to "10.16.2.254:1752" with message "gotSocketData" 
      put the openSockets into fld "fldSockets"
   end if
end mouseUp

on gotSocketData pSocket
   read from socket pSocket for 1 line with message "processData" -- changed it to a form i'm more familiar with, that works well with a read loop.
##do a check to make sure multiple clicks haven't caused concurrent looping leading to hang up
if "gotSocketData" is not among the lines of the pending messages then
send "gotSocketData" to me in 10 milliseconds -- give breathing room for other stuff to happen
end if
end gotSocketData 

on processData pSocket, pData
if pData is not empty then -- it does the read from above. If there was nothing there this should still fire so check for empty
   put pData & cr before fld "fldData"
   --Insert to MySQL
end if
end processData 

on socketError
   put "##Socket Error##" before fld "fldData"
end socketError

on socketTimeout
   put "#" & the long system time & "> Time out.." & cr before fld "fldData"
end socketTimeout
hoburne wrote:Hi,

I am looking for help using sockets. I have read the documentation and posts on this fourm and got a good start but I am now stuck and confused.
I have a network appliance that spits out a string of text for every transaction it handles. My aim is to write a simple stack to connect to it and read every string it throws out and add it a MySQL db for analysis later.

The stack I have connects, waits until it gets a string then displays it (for debug) then does nothing. This naybe a problem with my miss understanding of handlers or something simple! i just need pointing in the right direction please.

My Code Below:


Ok so basically this stack has a fld called "fldData" that displays: "#14:32:01> Time out.." every 10 seconds until a transaction is completed then a string is read and displayed in fldData then nothing happens. No more time out messages, no socketError etc..

Any pointers?

Thanks, And sorry if this is a newb question!

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: Sockets! Confused..

Post by mwieder » Fri Feb 03, 2012 6:56 pm

What sturgis said.

I think the main issue is that you're not queueing up another read after getting the first line's data. But the handler name "gotSocketData" is also maybe a bit confusing. Your processData routine is what gets fired when a line of data has come in - gotSocketData is the callback that comes into play when a socket connection is made and sits there waiting for data to arrive. The code might be easier to follow if you named gotSocketData something like "waitForData" or "wakeMeWhenDataHasComeIn" or "loopHere" or something.

btw: there's nothing newb about socket questions - it's an advanced topic and one that's rife with pitfalls and lots of dumb errors you can make. Whenever I do socket code I end up having to look at code I've previously written to remind myself how it works.

hoburne
Posts: 40
Joined: Tue Oct 04, 2011 11:13 am

Re: Sockets! Confused..

Post by hoburne » Mon Feb 06, 2012 11:14 am

Thanks for your input guys, just goes to show helpful nature of this community! :D

@sturgis - thanks, the code now makes more sense! The goal is to have the button labeld as 'start' which kicks off the data collection process and then disabled. The data should only stop collecting when a 'stop' button is clicked.

With your advice and guidance I have adjusted the code as recommended and I am beginning to understand this a little better, although the problem still remains. I have attached an image of the stack.
As you can see the stack connects the sockets and displays it with "put the openSockets",
-it then fires "waitForData" (thanks mwieder)
- WaitForData times out waiting for a transaction to complete and fires socketTimout
- SocketTimeOut puts the long system time at the top of a field with the i've timed out message
** this is where i start to get unsure, I think the following occurs**
- process returns to waitForData
- read from socket gets a line and fires processData
- processData checks pData is not empty and puts the system time and pData at the top of the field (to be replaced with a MySQL insert)
- process returns to waitForData
- checks to see if there is a pending waitForData message and send if not..
*** then the stack just sits there, its not hung and is responsive to clicks, drags etc.. but it seems its not waiting to receive data anymore ***

I'm not sure if I have the above logic correct, so your comments and corrections would be appreciated.

interestingly, at this point

Code: Select all

put the pendingMessages
returns 2 instances of "waitForData" along with a bunch of "checkForAltKey" in the revdictionary stack.

Thanks again.
Attachments
smdr-socket-issue.png
smdr-socket-issue.png (4.46 KiB) Viewed 5463 times

bangkok
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 937
Joined: Fri Aug 15, 2008 7:15 am

Re: Sockets! Confused..

Post by bangkok » Mon Feb 06, 2012 1:42 pm

Due to the way your mouseup is implemented, I added a pending message check to avoid the possibility of multiple concurrent loops running.
Very interesting.

By experience, sometimes, users like to click several times on the button, leading to problems with the read/write socket process.

So now I use a simple trick : at the begining of the mouseup or mousedoubleup script... i use :

Code: Select all

disable me

hoburne
Posts: 40
Joined: Tue Oct 04, 2011 11:13 am

Re: Sockets! Confused..

Post by hoburne » Mon Feb 06, 2012 2:04 pm

Thanks to all who have replied, I have this working now.

@bangkok - I have added the disable me to stop multiple clicks, I will - in time - enable a stop button at the same time.

I also added a check in the mouseUp to cancel any waitforData messages still haninging around from before which also writes to the screen so I can see if was finding any. I also added an empty check on pSocket inside waitForData and processData which indicated that pSocket emptied after a successful read. So I put the socket info (IP:PORT) into a global and referenced that from inside the handlers, and it works :D

i'm sure theres a much more elegant way of doing this - but it works which will do for now. I can revisit to tidy up later.

Thanks to all who replied; sturgis, mwieder and bankok.

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: Sockets! Confused..

Post by sturgis » Mon Feb 06, 2012 3:56 pm

Glad you got it going!

Post Reply