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