Socket Comm Sequence Issue

Brian Yennie briany at qldlearning.com
Fri Apr 15 14:15:33 EDT 2005


> When do you trigger "sendData"? When I put this into two stacks and 
> open
> them, it puts them both into "accept" mode, but neither sends anything 
> to
> the other.

I was just triggering it from the message box, but it should work from 
anywhere once both stacks are open.

> What I'm trying to accomplish is that App 1 launches, it in turn 
> launches
> App 2, and both of them are in a two-way communication session...
>
> Any suggestions on scripting the critical "who talks first" step? 
> Because if
> someone talks before the other one is listening, you're hosed...

Well, if App 1 is launching App 2, wouldn't App 1 do the first talking?

Why are you hosed? I guess that's the crux- it shouldn't hose anything 
(other than not being able to make the connection). Can you lower the 
socketTimeoutInterval and have App 1 keep trying after it launches app 
2?

Something like this maybe? The idea would be that either application 
would look for the other when it was launched. If it was able to 
connect, it knows it is application #2. Otherwise, if the connection 
times out, it is app #1 and it launches application #2. In turn, that 
second application should be able to connect back. If you set the 
timeout value low and make asynchronous calls, it should be pretty 
snappy.

Is this along the right lines?

global myAppNumber

## look for an existing application 1 and open our own listener
on openStack
    lookForFirstApplication
    accept connections on port 9000 with message "gotConnect"
end openStack

## try to connect to the other app
on lookForFirstApplication
   set the socketTimeoutInterval to 5000
   open socket to port 8999 with message "foundApplication1"
end lookForFirstApplication

## found it, we must be app #2.. we could safely start talking after 
this
on foundApplication1 s
    close socket s
    put 2 into myAppNumber
end foundApplication1

## couldn't connect, so we must be application #1
## now launch the second app, which will connect back to us
on socketTimeout s
   close socket s
   put 1 into myAppNumber
   launchApplication2
end socketTimeout



More information about the use-livecode mailing list