Socket class: Difference between revisions

From m204wiki
Jump to navigation Jump to search
m (Created page with "The <var>Socket</var> object is a coding alternative to using the $Sock_xxxx functions. An application written with the <var>Socket</var> object does not need to manage or refer ...")
 
m (add link)
 
(8 intermediate revisions by 2 users not shown)
Line 1: Line 1:
The <var>Socket</var> object is a coding alternative to using the $Sock_xxxx
The <var>Socket</var> object is a coding alternative to using the [[Janus Sockets $functions]].
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>
is essentially an object style reference to a <var>Socket</var> number.
is essentially an object style reference to a <var>Socket</var> number.
Both client and server applications may be written using the <var>Socket</var>
Both client and server applications may be written using the <var>Socket</var> object.
object.


'''Note:'''
<p class="note">'''Note:'''
To use the <var>Socket</var> object, you must have licensed <var class="product">Janus Sockets</var> '''and''' <var class="product">Janus SOAP</var>.
To use the <var>Socket</var> object, you must have licensed <var class="product">Janus Sockets</var>. </p>
The Sirius object-oriented alternative to $functions is designed for
<var class="product">Janus SOAP</var> applications only, and information about using Sirius objects is
provided in the <var class="product">[[Janus SOAP User Language Interface|"Janus SOAP User Language Interface"]]</var>.
   
   
This article is primarily concerned with how to use the individual
There are some restrictions on the order in which the <var>Socket</var> object methods are
<var>Socket</var> object methods.
There are some restrictions on the order in which the methods are
used: for example, a socket port must be connected before data is sent using it,
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.
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|"Janus Sockets User Language coding considerations"]].
see [[Janus Sockets User Language coding considerations]].
   
   
__TOC__
==Using Socket methods and $Sock_xxx functions==
Most of the <var>Socket</var> methods have analogous $functions,
Most of the <var>Socket</var> methods have analogous $functions,
and <var>Socket</var> objects may be used in code intermixed
and <var>Socket</var> objects may be used in code intermixed
Line 33: Line 28:
To simplify the conversion of existing <var class="product">Janus Sockets</var> code, two transition methods
To simplify the conversion of existing <var class="product">Janus Sockets</var> code, two transition methods
are provided:
are provided:
<var>[[GetSocketNumber (Socket function|GetSocketNumber]]</var>)
<ul>
gives the socket number for an object instance;
<li><var>[[GetSocketNumber (Socket function)|GetSocketNumber]]</var>
<var>[[GetSocketObject (Socket function|GetSocketObject]]</var>)
gives the socket number for an object instance.
<li><var>[[GetSocketObject (Socket function)|GetSocketObject]]</var>
creates an object instance for a given socket number.
creates an object instance for a given socket number.
These transition methods let
</ul>
you gradually convert an existing application to use <var>Socket</var> objects.
These transition methods let you gradually convert an existing application to use <var>Socket</var> objects.
   
   
==Creating and destroying Socket objects==
==Creating and destroying Socket objects==
The <var>Socket</var> object constructor, the <var>[[New (Socket function)|New]]</var> method,
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 54: 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
$Statusd is set to what would be the return code value if
<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 66: Line 62:
End If
End If
</p>
</p>
:Note.
<p class="note">'''Note:''' An explicit <var>Discard</var> of the <var>Socket</var> object is not permitted.
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==
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:

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