Socket Comm Sequence Issue

Brian Yennie briany at qldlearning.com
Fri Apr 15 13:23:02 EDT 2005


See if this helps- works well here with two stacks in the IDE. In the 
second stack, flip the port numbers (and the gotData test message). 
Everything here is non-blocking, which is important if both stacks are 
running together - you don't want one stacks script to block the 
other's.

You can just stick this in the stack script (be sure to make sure you 
trigger the openStack or reload the stack), and then break it down from 
there...

HTH...

### accept connections immediately
on openStack
   accept connections on port 8999 with message "gotConnect"
end openStack

### receive a new connection and begin reading data from it
on gotConnect s
   read from socket s for 1 line with message "gotData"
end gotConnect

### got the data, report it and send confirmation back
on gotData s,d
   answer "Application 1 says:"&&d
   write "OK"&cr to socket s
   close socket s
end gotData

### open a new socket to be the sender of data
on sendData
   open socket to "127.0.0.1:9000" with message "socketOpen"
end sendData

### once the socket is open, send some data
on socketOpen s
   put "Writing to socket"&&s
   write ("Hello my name is"&&(the long name of this stack))&cr to 
socket s with message "dataSent"
end socketOpen

### once the data is sent, read for a reply
on dataSent s,d
   read from socket s for 1 line with message "gotReply"
end dataSent

### display the confirmation message
on gotReply s,d
   answer d
end gotReply


> On 4/15/05 8:29 AM, "Frank D. Engel, Jr." <fde101 at fjrhome.net> wrote:
>
>> -----BEGIN PGP SIGNED MESSAGE-----
>> Hash: SHA1
>>
>> Sockets are already bidirectional, the trick is to know when which
>> program should read data.  Set up one to listen then connect from the
>> other.
>>
>> It turns out that 'read from socket' can be used asynchronously.  You
>> can have the handler which executes as a callback trigger another read
>> each time in order to set up pseudo-threading of a sort, allowing your
>> program to offer bidirectional communication over a single socket.
>> That should solve your problem.
>
> Can you show me an example? The reason I ask is that although I know I 
> can
> do a read (and sequential reads) after App 2 opens a connection with 
> "open
> socket", but the socket that it reads from is the same as the one that 
> was
> opened (127.0.0.1:59999 below), NOT the socket that is *created* by 
> App 1's
> "accept connections" (127.0.0.1:50013 below).
>
> So an example would really be appreciated...
>
> Ken Ray
> Sons of Thunder Software
> Web site: http://www.sonsothunder.com/
> Email: kray at sonsothunder.com
>
>
> _______________________________________________
> use-revolution mailing list
> use-revolution at lists.runrev.com
> http://lists.runrev.com/mailman/listinfo/use-revolution
>
>



More information about the use-livecode mailing list