Socket class: Difference between revisions
mNo edit summary |
m (add link) |
||
(6 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
The <var>Socket</var> object is a coding alternative to using the $ | The <var>Socket</var> object is a coding alternative to using the [[Janus Sockets $functions]]. | ||
An application written with the <var>Socket</var> object does not need to manage or refer to socket numbers. | An application written with the <var>Socket</var> object does not need to manage or refer to socket numbers. | ||
An instance of a <var>Socket</var> | An instance of a <var>Socket</var> | ||
Line 5: | Line 5: | ||
Both client and server applications may be written using the <var>Socket</var> object. | Both client and server applications may be written using the <var>Socket</var> object. | ||
'''Note:''' | <p class="note">'''Note:''' | ||
To use the <var>Socket</var> object, you must have licensed <var class="product">Janus Sockets | To use the <var>Socket</var> object, you must have licensed <var class="product">Janus Sockets</var>. </p> | ||
There are some restrictions on the order in which the <var>Socket</var> object methods are | There are some restrictions on the order in which the <var>Socket</var> object methods are | ||
Line 15: | Line 12: | ||
and data may not be sent after the FIN indicator is sent. | and data may not be sent after the FIN indicator is sent. | ||
For more information about <var class="product">Janus Sockets</var> programming, | For more information about <var class="product">Janus Sockets</var> programming, | ||
see [[Janus Sockets User Language coding considerations | see [[Janus Sockets User Language coding considerations]]. | ||
__TOC__ | __TOC__ | ||
Line 24: | Line 21: | ||
with $Sock_xxx functions and socket numbers ("in peaceful coexistence"). | with $Sock_xxx functions and socket numbers ("in peaceful coexistence"). | ||
In the individual <var>Socket</var> method description pages (see [[List of Socket methods | In the individual <var>Socket</var> method description pages (see [[List of Socket methods]]), | ||
if a <var>Socket</var> method is described as equivalent to a $Sock_xxx function, | if a <var>Socket</var> method is described as equivalent to a $Sock_xxx function, | ||
the method has the same argument list as the $funtion, with one exception: the method has no socket number argument, | the method has the same argument list as the $funtion, with one exception: the method has no socket number argument, | ||
Line 40: | Line 37: | ||
==Creating and destroying Socket objects== | ==Creating and destroying Socket objects== | ||
The <var>Socket</var> object constructor, the <var>[[New (Socket | The <var>Socket</var> object constructor, the <var>[[New (Socket constructor)|New]]</var> method, | ||
creates an instance of a <var>Socket</var> object. | creates an instance of a <var>Socket</var> object. | ||
The object created with <var>New</var> is a reference to | The object created with <var>New</var> is a reference to | ||
Line 53: | Line 50: | ||
If the connect fails, the object variable is set to null, and | If the connect fails, the object variable is set to null, and | ||
$ | <var>[[$StatusD]]</var> is set to what would be the return code value if | ||
the same error had happened with <var>[[$Sock_Conn]]</var>, the <var class="product">Janus Sockets</var>. $function | the same error had happened with <var>[[$Sock_Conn]]</var>, the <var class="product">Janus Sockets</var>. $function | ||
that creates a socket connection. | that creates a socket connection. | ||
Line 65: | Line 62: | ||
End If | End If | ||
</p> | </p> | ||
'''Note:''' An explicit <var>Discard</var> of the <var>Socket</var> object is not permitted. | <p class="note">'''Note:''' An explicit <var>Discard</var> of the <var>Socket</var> object is not permitted. | ||
You use the <var>[[Close (Socket function)|Close]]</var> method | You use the <var>[[Close (Socket function)|Close]]</var> method | ||
to close the socket and discard the <var>Socket</var> object. | to close the socket and discard the <var>Socket</var> object. </p> | ||
==List of Socket methods== | ==List of Socket methods== | ||
The [[List of Socket methods | The [[List of Socket methods]] shows all the class methods. | ||
==Sample code== | |||
Here is a code fragment that fetches the Sirius home page using a <var>Socket</var> object. | |||
You may want to contrast this with the $function sample in [[Sample Janus Sockets programs#Simple echo of HTTP/HTML|Simple echo of HTTP/HTML]]. | |||
<p class="code">%socko object <var>Socket</var> | |||
%socko = New('HTTPCLIENT', 'www.sirius-software.com', 80) | |||
If %socko Is Null Then | |||
Print 'Error:' And $STATUSD | |||
Stop | |||
End If | |||
%socko:SendWithLineEnd('GET / HTTP/1.0') | |||
%socko:SendWithLineEnd( '' ) | |||
%socko:Set('PRSTOK','AMBIG|0D0A|0A|0D') | |||
Repeat While ( %socko:ReceiveAndParse(%s) ) | |||
Print %s | |||
End Repeat | |||
Print %(Socket):Num('RESET') | |||
Print %(Socket):ErrInfo('CODE') | |||
%socko:Close | |||
</p> | |||
==See also== | |||
<ul> | |||
<li>[[Janus Sockets User Language coding considerations]] | |||
<li>[[Sample Janus Sockets programs]] | |||
<li>Socket-level interfaces: | |||
<ul> | |||
<li>[[Janus Sockets $functions|$functions]] | |||
<li>[[Socket class]] | |||
</ul> | |||
<li>Higher-level interfaces: | |||
<ul> | |||
<li>[[Email class]] | |||
<li>[[HTTP Helper|HTTP Helper classes]] | |||
<li>[[LDAP class]] | |||
<li>[[Janus FTP Server]] | |||
<li>[[Janus Telnet Server]] | |||
</ul> | |||
[[Category:Janus Sockets]] | [[Category:Janus Sockets]] |
Latest revision as of 20:22, 2 February 2016
The Socket object is a coding alternative to using the Janus Sockets $functions. An application written with the Socket object does not need to manage or refer to socket numbers. An instance of a Socket is essentially an object style reference to a Socket number. Both client and server applications may be written using the Socket object.
Note: To use the Socket object, you must have licensed Janus Sockets.
There are some restrictions on the order in which the Socket object methods are used: for example, a socket port must be connected before data is sent using it, and data may not be sent after the FIN indicator is sent. For more information about Janus Sockets programming, see Janus Sockets User Language coding considerations.
Using Socket methods and $Sock_xxx functions
Most of the Socket methods have analogous $functions, and Socket objects may be used in code intermixed with $Sock_xxx functions and socket numbers ("in peaceful coexistence").
In the individual Socket method description pages (see List of Socket methods), if a Socket method is described as equivalent to a $Sock_xxx function, the method has the same argument list as the $funtion, with one exception: the method has no socket number argument, since the socket is implied by the object instance.
To simplify the conversion of existing Janus Sockets code, two transition methods are provided:
- GetSocketNumber gives the socket number for an object instance.
- GetSocketObject creates an object instance for a given socket number.
These transition methods let you gradually convert an existing application to use Socket objects.
Creating and destroying Socket objects
The Socket object constructor, the New method, creates an instance of a Socket object. The object created with New is a reference to a connection to a remote host using a Janus client socket. The ServerSocket method obtains a reference to a Socket object for a server socket.
The following is an example of the New constructor:
%socko is object Socket %socko = New(clientportname, host, port [, ssl])
If the connect fails, the object variable is set to null, and $StatusD is set to what would be the return code value if the same error had happened with $Sock_Conn, the Janus Sockets. $function that creates a socket connection.
To obtain information about any errors that occur while attempting to construct a Socket object instance, here is an error handler that can be used after the New constructor:
If %socko Is Null Then Print 'Error:' And $Statusd Stop End If
Note: An explicit Discard of the Socket object is not permitted. You use the Close method to close the socket and discard the Socket object.
List of Socket methods
The List of Socket methods shows all the class methods.
Sample code
Here is a code fragment that fetches the Sirius home page using a Socket object. You may want to contrast this with the $function sample in Simple echo of HTTP/HTML.
%socko object Socket %socko = New('HTTPCLIENT', 'www.sirius-software.com', 80) If %socko Is Null Then Print 'Error:' And $STATUSD Stop End If %socko:SendWithLineEnd('GET / HTTP/1.0') %socko:SendWithLineEnd( ) %socko:Set('PRSTOK','AMBIG|0D0A|0A|0D') Repeat While ( %socko:ReceiveAndParse(%s) ) Print %s End Repeat Print %(Socket):Num('RESET') Print %(Socket):ErrInfo('CODE') %socko:Close
See also
- Janus Sockets User Language coding considerations
- Sample Janus Sockets programs
- Socket-level interfaces:
- Higher-level interfaces: