Blocking Action Until Socket Responds

Bringing the internet highway into your project? Building FTP, HTTP, email, chat or other client solutions?

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
SirWobbyTheFirst
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 246
Joined: Tue Jun 30, 2009 11:15 pm

Blocking Action Until Socket Responds

Post by SirWobbyTheFirst » Fri Aug 27, 2010 8:43 pm

Hi everyone, I was wondering how I could make rev wait for an acknowledgement from a socket before continuing, I ask because I'm revisiting an old application which uses a remote login interface and I was wondering how I could get Revolution to pause until a response is received from the server. I cannot use the wait command because that seems to block the entire application.

Any help would be much appreciated. Thank you.

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: Blocking Action Until Socket Responds

Post by Mark » Fri Aug 27, 2010 9:03 pm

Hi mickpitkin92,

It seems you're doin' it wrong. You shouldn't use a wait command or other methods to halt your script until a socket responds. You need to read from a socket with a message:

Code: Select all

read from socket mySock with message "messageReceived"
The messageReceived message will be accompanied by a few parameters, if I'm not mistaken the correct form is:

Code: Select all

on messageReceived theIP,theMsg
  -- handle the message here
  put theMsg into fld "Message"
  -- write to socket theIP again
  write "got message" to socket theIP
  -- and finally read again
  if theMsg contains "stop" then
    close socket theIP
  else
    read from socket theIP with message "messageReceived"
  end if
end messageReceived
This is just a very basic fictional example but I hope you get the idea.

Best regards,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

SirWobbyTheFirst
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 246
Joined: Tue Jun 30, 2009 11:15 pm

Re: Blocking Action Until Socket Responds

Post by SirWobbyTheFirst » Fri Aug 27, 2010 11:47 pm

Ah perfect, I'll keep that in mind. Thank you mark. :)

Janschenkel
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 977
Joined: Sat Apr 08, 2006 7:47 am
Contact:

Re: Blocking Action Until Socket Responds

Post by Janschenkel » Sun Aug 29, 2010 10:24 am

There are two ways to use sockets: synchonously and asynchronously. Using the variants 'with message' at the end, means you're working asynchronously: the engine will get back to you later and send that message to your control, along with a few parameters, such as the socket and the data. If you don't use 'with message' at the end, then the engine will sit there and wait until the operation completes.
Handling your communication asynchronously is more complicated, but provides a better user experience, as the interface isn't locked. Obviously, if you go for the asynchronous method, it's your job to prevent the user from doing something wrong - by disabling buttons and menu items, for instance.

Jan Schenkel.
Quartam Reports & PDF Library for LiveCode
www.quartam.com

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

Re: Blocking Action Until Socket Responds

Post by mwieder » Fri Jan 28, 2011 12:47 am

Here's a twist: I just came up with a need for *both* asynchronous and synchronous socket communication. I've normally got asynchronous reads queued up, but there are some situations where I explicitly want to wait for a packet to come in. Since I'm already waiting for asynchronous reads, that's where my packets go instead of into my synchronous read that eventually ends up timing out. And then, of course, my data has gone out into the asynchronous loop instead.

So has anyone got a good way of dealing with this? I could open a secondary channel on a different socket for the synchonous reads, but I'm trying to get away without doing that. I tried closing the socket, then reopening it, but I get an error 111 on the socket when I do that. And I don't want to kill the asynchronous read permanently because I rely on that for normal communications.

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

Re: Blocking Action Until Socket Responds

Post by mwieder » Fri Jan 28, 2011 9:53 pm

[Note to self:] Actually mixing the two works fine. What messed me up was thinking that I needed to queue up another asynchronous read after I did the synchronous one. No worries. But do note that queueing a second "read with messages" when you've already got one queued up waiting for messages will result in timeouts and mess with your head.

Post Reply