Arbortext Command Language > Functions by Alphabetical Listing > open_connect
  
open_connect
open_connect (host, port)
 
This function opens a network channel to a TCP service on the specified host. host is the name of the host to connect to or its IP address. port is the integer port number to connect to (for example, 25), or the name of the service desired, such as, “smtp”.
open_connect returns a channel identifier on success that can be used in subsequent calls to read, write, close and other channel functions. It returns -1 on failure and sets the predefined variable api_error to an error message describing the error.
By default, the channel is opened in a blocking mode. This means that a read from the channel blocks until the operation completes. channel_set_callback can be used to change the channel so that subsequent reads (and writes) can be non-blocking.
The sample function that follows uses open_connect to fetch an HTML document from an HTTP server (well known port 80). For clarity, it does minimal error checking and does not deal with the MIME headers that are normally transmitted preceding the actual document. This example also reads from the channel using blocking reads. See channel_set_callback for an example using asynchronous reads, the preferred method to use.
Examples
function http_get(host, remotepath, lclfile)
{
local of = open(lclfile, "wb")
if (of < 0) {
message "http_get: couldn't open $lclfile for write"
return 0;
}
local ch = open_connect(host, 80)
if (ch < 0) {
message "http_get: $main::api_error"
return 0;
}
write(ch, "GET " . remotepath . " HTTP/1.0\r\n\r\n")
local buf, len
while ((len = read(ch, buf, 512)) > 0) {
write(of, buf, len)
}
close(ch);
close(of)
return len == 0
}
Related Topics
channel_set_callback built-in function
close built-in function
read built-in function
write built-in function