socketdata and if?

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
trenatos
Posts: 189
Joined: Wed Jul 03, 2013 6:46 am
Location: Virginia, USA
Contact:

socketdata and if?

Post by trenatos » Thu Jul 11, 2013 10:58 pm

Continuing my playing around with sockets, I'm starting to pass data, or trying to, back and forth.

Here's what I can do: I can pass data from the client to a server, I can respond and send data back.
Now I'm trying to figure out how to respond to the data in LC.

Here's my code:

on newMessage theirSocket theMessage
put theMessage into field "fld"
if theMessage = "login_accepted" & return then
put "ACCEPTED" into field "statusField"
beep
end if
end newMessage

However, the if doesn't get triggered when theMessage is "login_accepted", even though that's what's being output to the fld field (For debugging)
It looks to me like there's a linebreak being added, which is odd since my server should only be sending "login_accepted" and nothing more, though that could have to do with sockets and signaling the end of the current stream, in any case I'm hoping that someone who knows what's going on could help me out.
I tried using if item 1 and a space for delimiter but that didn't work.
Marcus

trenatos
Posts: 189
Joined: Wed Jul 03, 2013 6:46 am
Location: Virginia, USA
Contact:

Re: socketdata and if?

Post by trenatos » Thu Jul 11, 2013 11:40 pm

the number of chars in theMessage counts as 16, but the message is only 14 characters.
Marcus

trenatos
Posts: 189
Joined: Wed Jul 03, 2013 6:46 am
Location: Virginia, USA
Contact:

Re: socketdata and if?

Post by trenatos » Thu Jul 11, 2013 11:55 pm

Ok, I've figured out a workaround for now, until I can figure out what's going on.
I'm using replaceText with regex to simply strip non-alphanumeric characters and comparing the resulting string.
Marcus

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4002
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Re: socketdata and if?

Post by bn » Fri Jul 12, 2013 12:22 am

Hi trenatos,

sorry I can't help with sockets.

You could test for

Code: Select all

if theMessage contains "login_accepted" then
or

Code: Select all

if word 1 to - 1 of  theMessage is "login_accepted" then
and to find out what those extra characters are you could say

Code: Select all

put cr & charToNum(char - 2 of theMessage) &&  charToNum(char - 1 of theMessage) after field "statusField" 
which gives you the ascii value of the last 2 characters

Kind regards
Bernd

trenatos
Posts: 189
Joined: Wed Jul 03, 2013 6:46 am
Location: Virginia, USA
Contact:

Re: socketdata and if?

Post by trenatos » Fri Jul 12, 2013 12:54 am

I considered using if contains, but it's going to be expanded to handle messaging later on, and if a user then places login_accepted (Or any of the other keywords) into a message, it will cause problems.
I'm going to implement a multi-stage process to make it sturdy as well as secure though, so I might end up using contains after all, but not until later on.
Marcus

Newbie4
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 327
Joined: Sun Apr 15, 2012 1:17 am
Location: USA
Contact:

Re: socketdata and if?

Post by Newbie4 » Fri Jul 12, 2013 1:33 am

Are you sending from a Windows computer? The extra 2 chars could be a lf+cr (line feed + carriage return)
Important! Sockets are always opened in binary mode. This means that LiveCode does not automatically convert between the other system's end-of-line convention and the line feed character (ASCII 10) that LiveCode uses internally to mark the end of a line. If you are reading or writing data one line at a time, be sure you know whether the other system uses line feed, return (ASCII 13), or both to mark the end of each line; if necessary, you will need to convert end-of-line markers yourself, after receiving or before sending the data. (The usual end-of-line marker on Mac OS and OS X systems is a return character; on Unix, a line feed; on Windows, a CRLF.)
Cyril Pruszko
https://sites.google.com/a/pgcps.org/livecode/
https://sites.google.com/a/setonhs.org/app-and-game-workshop/home
https://learntolivecode.com/

trenatos
Posts: 189
Joined: Wed Jul 03, 2013 6:46 am
Location: Virginia, USA
Contact:

Re: socketdata and if?

Post by trenatos » Fri Jul 12, 2013 2:07 am

Yes, the testserver is a local server running on my dev-laptop, Windows 7.
I figured it was lf/cr, but not sure how to strip those characters through regex, or compare including them.

When finished it'll run on a linux server.
Marcus

Newbie4
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 327
Joined: Sun Apr 15, 2012 1:17 am
Location: USA
Contact:

Re: socketdata and if?

Post by Newbie4 » Fri Jul 12, 2013 3:13 am

Try

if theMessage = ("login_accepted" & lf & cr) then

see if that works. The parentheses may be needed. Otherwise put that into a single variable and test it
Cyril Pruszko
https://sites.google.com/a/pgcps.org/livecode/
https://sites.google.com/a/setonhs.org/app-and-game-workshop/home
https://learntolivecode.com/

trenatos
Posts: 189
Joined: Wed Jul 03, 2013 6:46 am
Location: Virginia, USA
Contact:

Re: socketdata and if?

Post by trenatos » Fri Jul 12, 2013 3:24 am

Ah but see if I strip the characters regardless, it won't matter what format the server sends (What platform it's running on)
Marcus

Newbie4
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 327
Joined: Sun Apr 15, 2012 1:17 am
Location: USA
Contact:

Re: socketdata and if?

Post by Newbie4 » Fri Jul 12, 2013 3:30 am

There you go. Good job
Cyril Pruszko
https://sites.google.com/a/pgcps.org/livecode/
https://sites.google.com/a/setonhs.org/app-and-game-workshop/home
https://learntolivecode.com/

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7237
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: socketdata and if?

Post by jacque » Fri Jul 12, 2013 7:11 pm

trenatos wrote:Yes, the testserver is a local server running on my dev-laptop, Windows 7.
I figured it was lf/cr, but not sure how to strip those characters through regex, or compare including them.
A fast way to remove extra padding spaces, returns, linefeeds, and other non-text stuff is like this:

put word 1 to -1 of theMessage into theMessage
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”