Page 1 of 1
socketdata and if?
Posted: Thu Jul 11, 2013 10:58 pm
by trenatos
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.
Re: socketdata and if?
Posted: Thu Jul 11, 2013 11:40 pm
by trenatos
the number of chars in theMessage counts as 16, but the message is only 14 characters.
Re: socketdata and if?
Posted: Thu Jul 11, 2013 11:55 pm
by trenatos
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.
Re: socketdata and if?
Posted: Fri Jul 12, 2013 12:22 am
by bn
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
Re: socketdata and if?
Posted: Fri Jul 12, 2013 12:54 am
by trenatos
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.
Re: socketdata and if?
Posted: Fri Jul 12, 2013 1:33 am
by Newbie4
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.)
Re: socketdata and if?
Posted: Fri Jul 12, 2013 2:07 am
by trenatos
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.
Re: socketdata and if?
Posted: Fri Jul 12, 2013 3:13 am
by Newbie4
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
Re: socketdata and if?
Posted: Fri Jul 12, 2013 3:24 am
by trenatos
Ah but see if I strip the characters regardless, it won't matter what format the server sends (What platform it's running on)
Re: socketdata and if?
Posted: Fri Jul 12, 2013 3:30 am
by Newbie4
There you go. Good job
Re: socketdata and if?
Posted: Fri Jul 12, 2013 7:11 pm
by jacque
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