bangkok,
No, you keep the socket open until you don't need it anymore. In some cases, you might want to keep the socket open forever and check whether the client is still on-line once in a while.
For a chat application, it is useful to keep the connection open, because opening and closing the socket each time might be too slow. If you just want to transfer one file, you might want to keep the socket open until the data transfer has finished.
In
Clipboard Link I open a datagram socket to send a message and close it after writing to it. The other copies of Clipboard Link on the same network receive the message and and close the socket too. They return a reply over another datagram socket, which is also closed immediately. This way, I keep the needed network capacity minimised, while it isn't a problem if the message doesn't always arrive.
Whenever I want to transfer a clipboard to another computer, I open a normal socket, because I want it to be stable and I want to be sure that the data is transferred correctly (TCP assures this, if using a normal socket, but not if using a datagram socket). I close the socket after the transfer, because I don't need it anymore.
You should write your message "connected" in quotes. You also need a handler "connected", which does the writing. Then you follow with a read command with another message. On the client side, it should look like this:
Code: Select all
on foo
put "10.0.0.27:8808" into mySock
open socket to mySock with message "connected"
end foo
on connected theSock
write "Hello World" & cr to socket theSock
read from socket theSock until cr with message "bar"
end connected
on bar theSock,theData
put theData into fld "Some Field"
close socket theSock
end bar
Best regards,
Mark