Socket, Process, and Stream Input and Output
SenseTalk provides a set of commands for reading and writing sockets, processes, and the standard input and output streams (i.e., stdin, stdout, stderr). Fundamentally, these forms can all be treated as streams of data. The read and write commands include many options to make it easy to read or write any type of data including numbers in many formats, text, and raw binary data. HTTP and XML-RPC messages are also supported directly to further simplify communication with many servers on the internet.
Although some details differ between the different types of streams, the basic process of working with all of them follows the same sequence: open, read and/or write, close. The main exception to this sequence is that the standard input, standard output, and standard error streams don't require opening or closing because they are considered to be open at all times.
The commands and functions that you use to work with sockets, processes, and stream data are similar to the commands and functions you can use for working with files and file system objects. Unlike a file connection, you cannot seek to a specified location on a socket, process, or stream because these connections are just an open communication channel between two processes. For information about working with files and file systems, see File and Folder Interaction.
The socket input and output commands (open socket, close socket, read from socket, and write … to socket) permit you to open a connection to a socket provided by another process and read and write data through that connection.
Similarly, the process input and output commands (open process, close process, read from process, and write … to process) allow you to launch an external process and communicate with it by writing to the standard input and reading from the standard output of that process.
The read and write commands can also be used to read from standard input and to write to the standard output or standard error streams (read from input, write … to output, write … to error).
The openSockets and openProcesses functions return a list of all of the currently open sockets or processes, respectively.
The DefaultStringEncoding global property controls the encoding format that is used when reading or writing text from a file or socket.
Open Socket Command
Behavior: The open socket command must be used to open a socket before anything can be read from or written to that socket using the read or write commands. When you are finished with a socket, it should be closed using the close socket command.
Open socket establishes a TCP socket connection to another process (program). The other process may be running on the same computer, or on some other computer on the network. It must already be running, and have registered a socket to which SenseTalk can connect. Once the connection is established, data may be transmitted in either direction in whatever manner both sides understand.
Syntax:
open socket socketIdentifier
The socketIdentifier must be of the form host:port where host is the name or IP address of the machine, and port is the port number on that machine of the socket to be connected to.
The socketIdentifier may optionally end with a pound sign # (or a vertical bar |) character followed by an arbitrary number or identifier string. This serves the purpose of allowing you to create multiple identifiers to establish more than one connection to the same host and port, and identify each connection uniquely – just use the appropriate socketIdentifier with the read, write, and close commands to identify the correct connection.
If the socket connection cannot be established within the time specified by the readTimeout global property, an exception will be thrown. Use the openSockets function to get a list of all sockets which are currently open.
Example:
open socket remoteListener
Example:
open socket "192.168.1.4:22"
Example:
open socket "localhost:5900#2"
Open Process Command
Behavior: Launches an external process and opens a connection through which the script may interact with that other process. If all that is needed is to run an external process and receive any output from that process when it completes, the shell function provides a much simpler way to achieve that. The open process mechanism, on the other hand, provides much greater flexibility, allowing the script to conduct complex interactions with another process, or to start a lengthy operation without blocking the script and retrieve the results of that operation at a later point in the script.
Open process launches another process (program) which may be (and most commonly is) a shell through which still other programs may be executed. Once the other process is launched, a connection is established and text may be transmitted in either direction – the script may write to the standard input and read from the standard output of the other process.
If the process cannot be launched, the result function will be set to an exception (or the exception will be thrown, if the ThrowExceptionResults global property is set to true). The openProcesses function can be used to get a list of all processes which are currently open.
Syntax: open process processIdentifier {with {options} options}
The processIdentifier should be in the form processPath#identifier where processPath is the full path of the process to run. If processPath is omitted, a shell process will be launched (as specified by the shellCommand global property). The #identifier portion is also optional – it merely serves as a way to make a processIdentifier unique, so that a script can open and interact with multiple processes at once that use the same processPath.
If options is used, it should be a property list that may include any of these properties:
parameters | a list of values to be passed as parameters to the process when it is launched |
folder or directory | the current directory where the process will be run |
environment | a property list specifying environment variables and their values |
Example:
open process preferredShell & "#myshell"
Example:
open process "/bin/sh" with options myOptions
Example:
open process "/usr/local/bin/mysql#2"
Close Socket, Close Process, Close All Commands
Behavior: Closes a socket; or process; or all open files, sockets, or processes. The close all sockets command closes all currently open sockets, regardless of which script or handler opened the socket. This behavior could be potentially problematic if sockets have been opened by other scripts, and are still in use. Use the openSockets() function to get a list of all open sockets. The same applies to the close all processes command.
The close socket command closes a socket that was previously opened with the open socket command. The socketIdentifier should be identical to the identifier used when opening the socket.
The close process command closes a process that was previously opened with the open process command. The processIdentifier should be identical to the identifier used when opening the process.
The close all sockets and close all processes commands can be used to close all of the currently open sockets or processes. SenseTalk automatically closes all open sockets and processes whenever it stops executing your scripts, but it is good practice to for your script to close them when it is done working with them.
Syntax:
close socket socketIdentifier
close process processIdentifier
close all sockets
close all processes
Example:
close all processes -- close all open processes