Socket Comm Sequence Issue
Alex Tweedly
alex at tweedly.net
Fri Apr 15 15:52:58 EDT 2005
Ken Ray wrote:
As others have said - the connections are bidirectional, so it's usually
easiest to have one app which starts listening (i.e. accept
connections") as soon as it starts up, and the other one simply opens a
connection to it. So if you have the situation where there are exactly
two app that need to talk to each other, then simply do this.
If there can be more than 2 apps (or more than 2 instances of apps)
where any 2 might need to talk for a while, then you'll need to the more
complex "two-way" app style). I'll do an example of that later tonight;
read on for the simple case.
>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).
>
>
It's somewhat confusing (until you get it, then it's obvious :-) :-)
There is 1 (bi-directional) connection, but it has a different "name" on
each end.
Here's a snippet of code from "app 2"
> -- open socket:
> open socket to lHostPort with message "gotConnOpen"
> end reStart
>
> on gotConnOpen pSocket
> put "Success: opened" && psocket into line 1 of field "My Status"
> read from socket pSocket until CR with message "gotPacket"
> end gotConnOpen
When the connection has successfully opened, the callback is called, and
passed the name of the socket which just opened; this will in fact be
the same as the name used in the "open socket" command. (It is passed to
the callback because you may be opening multiple sockets
simultaneously). app2 can read (or write) from (to) that same socket.
In app1, you accept connections, and when the incoming connection
arrives, the callback is given the name of a NEW socket. app1 can then
read (write) from (to) this new socket. [So app1 is using the other
name for the same connection]
> -- accept incoming connections
> put "Accepting connections on port 6000" into line 1 of field "My
> Status"
> accept connections on port 6000 with message "gotConn"
> end reStart
>
> function talkToApp2 pData
> if lSock is empty then return false
> write pData & CR to socket lSock
> -- for simplicity,
> -- wait until write is done (instead of using the callback)
> -- ignore possibility we time-out
> return true
> end talkToApp2
>
> on gotConn pSocket
> -- this is not a classic "server" that wold accept multiple incoming
> connections
> -- it expects a single connection (from "app2") and rejects any
> subsequent attempts
> if lSock is not empty then
> put "Attempted connection from" && pSocket && "rejected."
> close socket pSocket
> end if
> put pSocket into lSock
> read from socket lSock until CR with message "gotPacket"
> end gotConn
>
>
> on gotPacket pHostPort, pData, pSock
> local tHostPort
>
> mylog "Packet arrived from" && pHostPort && "on my socket" && pSock
> mylog " contents:" && pData
> open datagram socket to pHostPort
> write "reply with " & pData to socket pHostPort
> read from socket pHostPort until CR with message "gotPacket"
> end gotPacket
The actual stacks are now on RevOnline, under my name (alextweedly) in
category "Programming" with the imaginative names "TCP App 1" and "TCP
App 2"
If this still seems confusing - try (if possible) doing it on two
different machines, and looking at the "opensockets" on each.
And if it's still confusing - please ask more questions.
--
Alex Tweedly http://www.tweedly.net
-------------- next part --------------
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