Socket Comm Sequence Issue

Alex Tweedly alex at tweedly.net
Fri Apr 15 07:00:48 EDT 2005


Ken - I'm in a huge rush now, and won't get to email again until tonight 
..... so this is very brief - good luck.
If it doesn't work, or you can wait 12 hours from now, I'll send a more 
complete working example.....

Ken Ray wrote:

>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:
>
>  
>

>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:
>  
>
<snip>

>I also don't understand where these other sockets are coming from...
>  
>
That's the key - to both this problem, and the "right" (IMHO) answer.

 From the docs on accept connection

> Comments:
> When a connection is made or a datagram is received, the accept 
> command creates a new socket that can be used to communicate with the 
> other system (or process).

i.e. when the accept is triggered, it supplies you with a new socket, 
and it's usual to use that new  socket to reply to the other host.

In each app, have an accept connection - if that happens, then you 
receive on that socket, and send on the socket supplied by the system 
when the connection is accepted. (i.e. this app is acting like a 
"server" for the purpose of the comms).

In the app which does the first "send", you send to the other app's 
known socket, and receive from it. (i.e. act like a client)

That's pretty brief - good luck !!
I will do a two-way "peer" stack tonight and announce it when it's done.

-- 
Alex Tweedly       http://www.tweedly.net



-- 
No virus found in this outgoing message.
Checked by AVG Anti-Virus.
Version: 7.0.308 / Virus Database: 266.9.7 - Release Date: 12/04/2005



More information about the use-livecode mailing list