Socket Comm Sequence Issue

Ken Ray kray at sonsothunder.com
Fri Apr 15 02:43:40 EDT 2005


I'm trying to get two apps to talk to each other over sockets on OS X, which
means that both apps need to be in "listening" mode as well as "speaking"
mode. The relevant code I'm using for testing this right now is shown below,
and doesn't trap for errors or messages that I'm not interested in:

Button "Listen":
------------------
on mouseUp
  accept connections on port 60000 with message GotConnection
end mouseUp

on GotConnection pSocket
  read from socket pSocket with message GotMessage
end GotConnection

on GotMessage pSocket,pData
  put pData into fld 1
  read from socket pSocket with message GotMessage
end GotMessage

Button "Connect":
--------------------
on mouseUp
  open socket "127.0.0.1:59999"
end mouseUp

Button "Send":
-----------------
on mouseUp
  write fld 2 to socket "127.0.0.1:59999"
end mouseUp

Field 1 is the "incoming data" field, and field 2 is the "outgoing data"
field. I have a Disconnect button that closes off the ports. The "other" app
is identical, except that the ports are reversed (59999 to receive, 60000 to
send).

The issue I'm running into is that I need to click the Listen and Connect
buttons in a certain order, otherwise only one app will be able to receive
and process incoming data (although it can send just fine).

If I click "Listen", then "Connect" in App 1, and "Listen", then "Connect"
in App 2,  only App 2 can send data to App 1 (not the reverse). If I reverse
this (do App 2 first, then App 1), the result is the reverse (App 1 can send
to App 2, but not the other way around).

Same thing happens if I do "Connect" and then "Listen" in App 1, and
"Connect", and then "Listen" in App 2.

The only way I can get it to work is to click "Listen" in both Apps, and
then click "Connect" in both apps. But since I can't get a "GotConnection"
until the "other" app does a "Connect", how can I set this up properly? I
need to launch both apps and have them talk to each other somehow. My
original idea was to tell both apps to go into "Listen", then "Connect"
mode, one right after another, but as you can see from the above, this
doesn't work.

I also tried running this as two stacks in the MetaCard IDE and asked to get
the openSockets after each attempt - perhaps this will help someone out
there in figuring this out:

Stack 1 (listens on 59999, sends on 60000): Listen, then Connect
Stack 2 (listens on 60000, sends on 59999): Listen, then Connect
OpenSockets:
    59999
    60000
    127.0.0.1:59999
    127.0.0.1:50314

Stack 2 (listens on 60000, sends on 59999): Listen, then Connect
Stack 1 (listens on 59999, sends on 60000): Listen, then Connect
OpenSockets:
    60000
    59999
    127.0.0.1:60000
    127.0.0.1:50318

Stack 1 (listens on 59999, sends on 60000): Listen
Stack 2 (listens on 60000, sends on 59999): Listen
Stack 1 (listens on 59999, sends on 60000): Connect
Stack 2 (listens on 60000, sends on 59999): Connect
OpenSockets:
    59999
    60000
    127.0.0.1:60000
    127.0.0.1:50319
    127.0.0.1:59999
    127.0.0.1:50320

Stack 2 (listens on 60000, sends on 59999): Listen
Stack 1 (listens on 59999, sends on 60000): Listen
Stack 2 (listens on 60000, sends on 59999): Connect
Stack 1 (listens on 59999, sends on 60000): Connect
OpenSockets:
    60000
    59999
    127.0.0.1:59999
    127.0.0.1:50321
    127.0.0.1:60000
    127.0.0.1:50322

I also don't understand where these other sockets are coming from...

If anyone can help me, I would greatly appreciate it.

Thanks,

Ken Ray
Sons of Thunder Software
Web site: http://www.sonsothunder.com/
Email: kray at sonsothunder.com




More information about the use-livecode mailing list