Sockets are a way to open a direct communication channel between processes different computers or between different processor on the same computer. In learning the rudiments of socket communications, I found the following tips useful.
Tip #1 comes from Brian Yennie, from the use-livecode email list. It gives a very basic description of how to set up a socket connection on a client and server, and so is useful as a first attempt.
The second reference is from Dave Cragg, a networking expert in LiveCode. It also appeared on the use-livecode list in response to another developers questions about sockets. Dave wrote some of the networking libraries that come with LiveCode.
There are several commands, functions and properties in the LiveCode scripting language for working with sockets. The readings here refer to several of them. Below is a concise listing. More information can be found in the LiveCode Dictionary.
open socket
command - opens a connection to another computer
close socket
command - closes the connection
accept
command - accepts a connection request from another computer requesting a socket
read from socket
command - accepts data from a socket, puts the data intoit
write to socket
command - sends data to a socket
resetAll
command - closes all open sockets
socketError
message - message that is sent when an error occurs; allows you to write a handler to handle the error+ various functions and properties that allow for fine control of the communication between computers
socketTimeoutInterval
function - returns the timeout interval for an inactive connection
openSockets
function - returns a list of open sockets
hostNameToAddress
function - when given a host name, returns an IP address
hostAddressToName
function - when given an IP address, returns a host name
hostAddress
function - returns the IP address of a local system a socket is connected to
peerAddress
function - returns the IP address of the system at the other end of a socket
You can download a pair of stacks that demonstrate the basics of socket communication. There is a client stack and a server stack. You will need two computers, one to run the client stack and another to run the server stack. Note the IP address of the server stack, and enter it into the IP address field of the client. These stacks are more experiments than finished products. I invite you to play with them, modify them and experiment as you try to master socket communications.
go stack URL "http://livecode.byu.edu/internet/socketclient.rev" go stack URL "http://livecode.byu.edu/internet/socketServer.rev"
For a more fully-developed example of a real LiveCode application that uses socket communications, see Björnke von Gierke's excellent ChatRev stack. You can download it from inside the LiveCode environment. Make sure you have a good internet connection, then just click on the Rev Online icon on the toolbar. Then click on "All" then "Internet". Select ChatRev from the list that appears. In the right-hand panel, click Download. The stack should open and you can experiment with it.