spec

Software for Diffraction

sock_io() (deprecated)

Connect to sockets

IMPORTANT NOTE

The sock_io() functions are deprecated as of spec release 5.01.01. Please see the sockets help file for a description of the sock_get(), sock_put() and sock_par() replacement functions.

BUILT-IN FUNCTIONS

sock_io(host:port, cmd [, arg])

Connects and communicates with sockets created by other processes on the local or a remote host. The host can be specified by a symbolic name or by an IP address. The string cmd is one of the following:

"?"
Lists the command options available.
"show"
Lists the current open sockets and their status.
"info"
Returns a string that lists the current open sockets and their status.
"connect"
Opens the socket to the specified host and port. Returns true for success and false for failure.
"listen"
Sets up a socket for listening, allowing another instance of spec or some other program to make a connection.
"close"
Closes the socket associated with the specified host and port.
"send"
Sends the string arg to the specified socket, opening it if not already connected.
"read"
Reads from the specified socket, opening it if not already connected. Currently, the message to be read can be no longer than 1,024 bytes. With no argument, reads until a newline or until the timeout expires. An optional string argument can specify an end-of-string (EOS) delimiter, in which case the socket will be read until characters matching the EOS sequence are read or until the timeout expires. Trailing newlines and carriage returns will be removed from the returned string. Also, if the optional argument is a number, it specifies the number of bytes to be read. That number of bytes (or less, if the timeout expires) will be read and returned.
"timeout"
With no argument, returns the current read-timeout value in seconds. Otherwise, sets the read timeout to arg. A negative timeout value causes the socket read to block until it is satisfied. The default timeout value is 5 seconds.

Note, the "connect" command isn't required, as the connection will be made if needed with the other commands. However, the return value from the "connect" command may be used to test if a socket can be created. The connection remains open until the "close" option is used. Simultaneous connections to multiple ports are allowed.

The following example connects to the echo service on port 7 of the local host.

SPEC.1> sock_io("localhost:7", "send", "This is a test.\n")

SPEC.2> print sock_io("localhost:7", "read")
This is a test.