sopen()
Open a socket channel with a remote host.
Syntax
Parameter
Parameter | Definition |
---|---|
host | host name or IP address of remote host |
port | port on which to open TCP socket |
IPPreference | IP type communication preference, the following values are permitted:
|
Description
The sopen() function opens a TCP socket channel to the specified combination of host and port and provides the access to the socket from within a PSL process. The read(), readln(), write(), get_chan_info(), share(), and close()functions apply to channels that have been opened to sockets in the same manner that file and pipe channels are manipulated via these functions. This function only supports text protocols such as http, telnet, and echo.
If the IPPreference parameter is not used, then the default connection preference is IPv4 and then IPv6 .
If the connection is successful, a channel identifier is returned. Otherwise the empty string is returned. Errno is set to E_PSL_FOPEN_CANNOT_CREATE_CHANNEL.
Example
The following PSL statements use sopen() to connect to an SMTP server and determine the maximum message size that the server will accept.
mailhost="ppat5814";
SIZE_supported = 0;
#Open socket channel
chan = sopen (mailhost, "2222","IPv4,IPv6");
if (chan=="") { #Failed to open channel
print ("Could not establish connection to SMTP server.\n");
} else { #Opened channel
#send command to return extension information
write(chan, "EHLO\r\n");
#read first line of reply
ret = readln (chan);
#Did EHLO command succeed?
switch (substr(ret,1,1)) {
case 5: {
print ("SMTP server ". mailhost." does not support EHLO command.\n");
}
case 4: {
print ("Transient failure attempting EHLO on SMTP server ".mailhost.".
Try again later.\n");
}
case 2: { #EHLO succeeded
#Loop until end of EHLO reply, reading lines
while ( ret =~ "-" ) {
if ( ret =~ "SIZE" ) {
size_ret =ret;
SIZE_supported=1;
}
ret = readln (chan);
}
#Did server reply with a SIZE line?
if (SIZE_supported) {
size = nthargf (size_ret, 2);
print ("Maximum message size for SMTP server ".mailhost ." is ".size."
bytes.\n");
} else {
print ("SMTP server ".mailhost." does not support SIZE extension.");
}}
default: {
print ("Failure attempting EHLO command on SMTP server
".mailhost."\n");
}}
#Tell server goodbye
write(chan, "quit");
#Close channel
close(chan);
}