Inconsistent 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: heatherlaine, Klaus, FourthWorld, kevinmiller

Post Reply
agraham147
Posts: 27
Joined: Thu Apr 19, 2018 6:18 am

Inconsistent sockets

Post by agraham147 » Thu Apr 19, 2018 6:44 am

Hi there,

I've written a program which is a snooker scoring system. I am making two software programs which connect to each other, a scoreboard (client) and the other program is the server. When something happens on a client (a button is pressed to update the score), it writes a single character to the server for what button was pressed. The server then receives the message, puts the character into an invisible field and then through if statements updates the scoreboard it has on it's program. Occasionally, the server doesn't receive the message and doesn't update but if you were to press the same button again it will send. Is this a recurring problem with liveCode sockets or is it just a script error? It doesn't strike me as a script error because it does work, just not every time. Help would be appreciated.

Thanks, Aaron

bogs
Posts: 2198
Joined: Sat Feb 25, 2017 10:45 pm

Re: Inconsistent sockets

Post by bogs » Thu Apr 19, 2018 7:06 am

agraham147 wrote:
Thu Apr 19, 2018 6:44 am
Is this a recurring problem with liveCode sockets or is it just a script error? It doesn't strike me as a script error because it does work, just not every time.
Up front I'm going to say I haven't messed with sockets (yet), so much of what I'm about to suggest won't be so I can help you, but so that you can (to some degree) help yourself.

The line above that I underlined is usually an indicator of a logic error, not a script or environment error, which you'd probably find in this part of the program.
and then through if statements
Usually you can track this down by setting a break point, and stepping through the code line by line to make sure your code is doing what you expect. Be sure to check all variable contents, etc at each step as well. Run through it more than one time, or, if you know what causes failure, start that situation and step through to see where it fails.

Lastly, I'd add that it is a bit hard to answer a question like this without some of the code attached :wink: One bonus to attaching it is that sometimes you'll receive advice on how to improve it, not just the answer to the question you asked.
Image

agraham147
Posts: 27
Joined: Thu Apr 19, 2018 6:18 am

Re: Inconsistent sockets

Post by agraham147 » Thu Apr 19, 2018 7:24 am

Ok thanks, I will try what you've suggested. :)

agraham147
Posts: 27
Joined: Thu Apr 19, 2018 6:18 am

Re: Inconsistent sockets

Post by agraham147 » Fri Apr 20, 2018 6:56 pm

Here is some of the code I have. At the bottom I have also included the first if statement for what to do when the message is put into the table1Receiver field to update the scoreboard. As I said before, it works 9/10 but once in a little while it doesn't work. To me it's like the newMessage handler doesn't work everytime a new message comes in because the new message doesn't show in the field and therefore doesn't update the scoreboard through the matchEvent handler. Do I need to do something with my read command?

Code: Select all

on someoneConnected theIP
   answer "Table Connected"
   read from socket theIP until return with message "newMessage"
end someoneConnected

on newMessage theIP theMessage
   put theMessage into field "table1Receiver" of card "multiScoreScreen1"
   matchEvent
   read from socket theIP until return with message "newMessage"
end newMessage

on matchEvent
   local redsRemaining, playerName
   
   --MATCH EVENT FOR "P"
   if character 1 of field "table1Receiver" of card "multiScoreScreen1" is "P" then
      if the visible field "match1Player1Break" of card "multiScoreScreen1" is true then
         set visible of field "match1Player2Break" of card "multiScoreScreen1" to true
         set visible of field "match1Player1Break" of card "multiScoreScreen1" to false
         put "0" into field "match1Player1Break" of card "multiScoreScreen1"
         set foregroundColor of field "match1Player1Points" of card "multiScoreScreen1" to 100,100,100
         set foregroundColor of field "match1Player2Points" of card "multiScoreScreen1" to 255,255,255
      else
         set visible of field "match1Player2Break" of card "multiScoreScreen1" to false
         set visible of field "match1Player1Break" of card "multiScoreScreen1" to true
         put "0" into field "match1Player2Break" of card "multiScoreScreen1"
         set foregroundColor of field "match1Player1Points" of card "multiScoreScreen1" to 255,255,255
         set foregroundColor of field "match1Player2Points" of card "multiScoreScreen1" to 100,100,100
      end if
   end if

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 5528
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Inconsistent sockets

Post by dunbarx » Fri Apr 20, 2018 8:47 pm

Before you do anything, test that the message is received.

What I mean by "before you do anything" is to make the server respond immediately after the message
"newMessage" is received. You have sort of done that if you can see if fld "tableReceiver" is updated. But you might do something more dramatic. Anyway, you must determine if the issue resides with the message not being received, and do that way before you process anything.

Craig Newman

agraham147
Posts: 27
Joined: Thu Apr 19, 2018 6:18 am

Re: Inconsistent sockets

Post by agraham147 » Sat Apr 21, 2018 11:11 am

Code: Select all

on someoneConnected theIP
   answer "Table Connected"
   read from socket theIP until return with message "newMessage"
end someoneConnected

on newMessage theIP theMessage
   put theMessage into field "table1Receiver" of card "multiScoreScreen1"
   #matchEvent
   read from socket theIP until return with message "newMessage"
end newMessage
I commented out the matchEvent handler so that it basically just receives the message and puts it into the tableReceiver field then contunes reading the message but it does it for the first message then fails to do it again. Am I misplacing the read command or have to put one in somewhere else so that it reads for future messages?

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 6154
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Inconsistent sockets

Post by FourthWorld » Sat Apr 21, 2018 5:34 pm

Which LC version?

There was a bug in a build or two that prevented "read from socket...until <char>" from working. It has been fixed in v9.0.
Richard Gaskin
Community volunteer LiveCode Community Liaison

LiveCode development, training, and consulting services: Fourth World Systems: http://FourthWorld.com
LiveCode User Group on Facebook : http://FaceBook.com/groups/LiveCodeUsers/

agraham147
Posts: 27
Joined: Thu Apr 19, 2018 6:18 am

Re: Inconsistent sockets

Post by agraham147 » Sat Apr 21, 2018 7:20 pm

I'm using LiveCode 8

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 6154
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Inconsistent sockets

Post by FourthWorld » Sat Apr 21, 2018 8:47 pm

Is the problem still evident in v9?
Richard Gaskin
Community volunteer LiveCode Community Liaison

LiveCode development, training, and consulting services: Fourth World Systems: http://FourthWorld.com
LiveCode User Group on Facebook : http://FaceBook.com/groups/LiveCodeUsers/

agraham147
Posts: 27
Joined: Thu Apr 19, 2018 6:18 am

Re: Inconsistent sockets

Post by agraham147 » Mon Apr 23, 2018 4:50 pm

I have now switched to LiveCode 9 and this is my situation at the minute. The client program looks like this:
Match Loaded.png
The server program looks like this:
Match Loaded MultiScore2.png
Now when the client presses P key it changes the player at the table and so this is what the client looks like:
Match Loaded.png
Last edited by agraham147 on Mon Apr 23, 2018 5:19 pm, edited 1 time in total.

agraham147
Posts: 27
Joined: Thu Apr 19, 2018 6:18 am

Re: Inconsistent sockets

Post by agraham147 » Mon Apr 23, 2018 5:04 pm

The client sends a message to the server (the button which was pressed in this case "P"), the server receives it and updates it's scoreboard so now after that the server looks like this:
Match Loaded MultiScore2.png
The little black P you see on the grey line is the invisible "receiverField" (visible for this image) which contains the sent message and then through IF statements in a handler called "matchEvent" the program checks to see what letter has been sent, then updates the scoreboard. This is the code for the server program:

Code: Select all

on preOpenStack
   set the fullscreenmode of me to "exactFit"
   set the fullscreen of this stack to true
   accept connections on port 8080 with message "someoneConnected"
end preOpenStack

on someoneConnected theIP
   answer "Table Connected"
   read from socket theIP until return with message "newMessage"
end someoneConnected

on newMessage theIP theMessage
   put theMessage into field "table1Receiver" of card "multiScoreScreen1"
   matchEvent
   read from socket theIP until return with message "newMessage"
end newMessage

on matchEvent
   local redsRemaining, playerName
   
   --MATCH EVENT FOR "P"
   if character 1 of field "table1Receiver" of card "multiScoreScreen1" is "P" then
      if the visible field "match1Player1Break" of card "multiScoreScreen1" is true then
         set visible of field "match1Player2Break" of card "multiScoreScreen1" to true
         set visible of field "match1Player1Break" of card "multiScoreScreen1" to false
         put "0" into field "match1Player1Break" of card "multiScoreScreen1"
         set foregroundColor of field "match1Player1Points" of card "multiScoreScreen1" to 100,100,100
         set foregroundColor of field "match1Player2Points" of card "multiScoreScreen1" to 255,255,255
      else
         set visible of field "match1Player2Break" of card "multiScoreScreen1" to false
         set visible of field "match1Player1Break" of card "multiScoreScreen1" to true
         put "0" into field "match1Player2Break" of card "multiScoreScreen1"
         set foregroundColor of field "match1Player1Points" of card "multiScoreScreen1" to 255,255,255
         set foregroundColor of field "match1Player2Points" of card "multiScoreScreen1" to 100,100,100
      end if
   end if
The next thing to do is to start the clock on the client by pressing the C key. So now the client looks like this:
Match Loaded.png
But the server still looks like this:
Match Loaded MultiScore2.png
As you can see the P is still in the field which means the new message hasn't been received and when I try to press P again it doesn't send that again either, but before I pressed C P worked as many times as I liked. Is this a problem with my read command? By the way, I do have an if statement for C and all the other possible messages just haven't included them in this forum message.

agraham147
Posts: 27
Joined: Thu Apr 19, 2018 6:18 am

Re: Inconsistent sockets

Post by agraham147 » Wed Apr 25, 2018 9:10 am

Does anyone know whats going wrong here, would be very much appreciated?

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”