$Sock Recv: Difference between revisions

From m204wiki
Jump to navigation Jump to search
(Automatically generated page update)
(Automatically generated page update)
 
(2 intermediate revisions by one other user not shown)
Line 1: Line 1:
{{DISPLAYTITLE:$Sock_Recv}}
{{DISPLAYTITLE:$Sock_Recv}}
<span class="pageSubtitle">Receive string based on count</span>                             
<span class="pageSubtitle">Receive string based on count</span>                             
<p class="warn"><b>Note: </b>Most Sirius $functions have been deprecated in favor of Object Oriented  
<p class="warn"><b>Note: </b>Many $functions have been deprecated in favor of Object Oriented  
methods. There is currently no direct OO equivalent for this $function.</p>                 
methods. The OO equivalent for <var>$Sock_Recv</var> is the <var>[[Receive (Socket function)|Receive]]</var> method.</p>                 


<var>$Sock_Recv</var> receives a string of bytes on a <var class="product">Janus Sockets</var> connection, typically
<var>$Sock_Recv</var> receives a string of bytes on a <var class="product">Janus Sockets</var> connection, typically
Line 15: Line 15:
<table class="syntaxTable">
<table class="syntaxTable">
<tr><th>%num</th>
<tr><th>%num</th>
<td>A numeric value that is the number of bytes received, or, if the operation could not be performed as requested, a 0 value. For more details, see [[#$Sock_Recv return values|"$Sock_Recv return values"]]. </td></tr>
<td>A numeric value that is the number of bytes received, or, if the operation could not be performed as requested, a 0 value. For more details, see [[#$Sock_Recv return values]]. </td></tr>
<tr><th>sockNum</th>  
<tr><th>sockNum</th>  
<td>The socket number. </td></tr>  
<td>The socket number. </td></tr>  


<tr><th>%recv_targ</th>  
<tr><th>%recv_targ</th>  
<td>A %variable or an <var>IMAGE</var> item (and '''never''' a <var class="product">[[Janus SOAP User Language Interface|Janus SOAP ULI]]</var> class variable). This argument is the target of the receive operation, and is referred to as the "receive target." You may request that some bytes from the socket stream can be discarded, so the length of the string stored in this target may be less than the value returned by <var>$Sock_Recv</var>. </td></tr>  
<td>A %variable or an <var>IMAGE</var> item (and '''never''' a <var class="product">SOUL</var> class variable). This argument is the target of the receive operation, and is referred to as the "receive target." You may request that some bytes from the socket stream can be discarded, so the length of the string stored in this target may be less than the value returned by <var>$Sock_Recv</var>. </td></tr>  


<tr><th>maxbytes</th>  
<tr><th>maxbytes</th>  
<td>This optional argument, the maximum number of bytes of data to receive, must be from 0 to 2,147,483,647. The default is 0, which means limit the received data to the larger of the value of the <var class="term">minbytes</var> argument and the declared length of the receive target.
<td>This optional argument, the maximum number of bytes of data to receive, must be from 0 to 2,147,483,647. The default is 0, which means limit the received data to the larger of the value of the <var class="term">minbytes</var> argument and the declared length of the receive target.
<p>
<p>
Under <var class="product">Sirius Mods</var> version 6.3 and later, if this argument is 0 and the receive argument is a <var>Longstring</var>, the received data is limited to 2,147,483,647 bytes. </p></td></tr>  
If this argument is 0 and the receive argument is a <var>Longstring</var>, the received data is limited to 2,147,483,647 bytes. </p></td></tr>  


<tr><th>minbytes</th>  
<tr><th>minbytes</th>  
Line 34: Line 34:
<tr><th>opts</th>  
<tr><th>opts</th>  
<td>This optional argument is an option string which can contain any of the following:  
<td>This optional argument is an option string which can contain any of the following:  
<table class="syntaxTable">  
<table>  
 
<tr><th><var>BINARY</var> </th>  
<tr><th><var>BINARY</var> </th>  
<td>This indicates that regardless of the socket's <var>[[BINARY (JANUS DEFINE parameter)|BINARY]]</var> or <var>[[CHAR (JANUS DEFINE parameter)|CHAR]]</var> parameter, data '''is not translated''' when saved in the receive target. The received string can be translated later in the program using the <var>[[$Sock_Tran_In]]</var> function. </td></tr>   
<td>This indicates that regardless of the socket's <var>[[BINARY (JANUS DEFINE parameter)|BINARY]]</var> or <var>[[CHAR (JANUS DEFINE parameter)|CHAR]]</var> parameter, data '''is not translated''' when saved in the receive target. The received string can be translated later in the program using the <var>[[$Sock_Tran_In]]</var> function. </td></tr>   
<tr><th><var>CHAR</var> </th>  
<tr><th><var>CHAR</var> </th>  
<td>This indicates that regardless of the socket's <var>[[BINARY (JANUS DEFINE parameter)|BINARY]]</var> or <var>[[CHAR (JANUS DEFINE parameter)|CHAR]]</var> parameter, data '''is translated''' when saved in the receive target. The translation is specified by the input table defined by the socket's <var>[[XTAB (JANUS DEFINE parameter)|XTAB]]</var> parameter. </td></tr></table>
<td>This indicates that regardless of the socket's <var>[[BINARY (JANUS DEFINE parameter)|BINARY]]</var> or <var>[[CHAR (JANUS DEFINE parameter)|CHAR]]</var> parameter, data '''is translated''' when saved in the receive target. The translation is specified by the input table defined by the socket's <var>[[XTAB (JANUS DEFINE parameter)|XTAB]]</var> parameter. </td></tr></table>
Line 53: Line 55:
and <var class="term">minbytes</var> arguments and other values affecting the received string.
and <var class="term">minbytes</var> arguments and other values affecting the received string.


In <var class="product">Sirius Mods</var> version 6.3 and later, the return value from <var>$Sock_Recv</var>
The return value from <var>$Sock_Recv</var>
can be less than the value of <var class="term">minbytes</var>.
can be less than the value of <var class="term">minbytes</var>.
<li>Before <var class="product">Sirius Mods</var> version 6.3, <var>$Sock_Recv</var> would return 0 to indicate
<li>Formerly, <var>$Sock_Recv</var> would return 0 to indicate that the length of the received
that the length of the received
string is less than <var class="term">minbytes</var>. [[#Less than minbytes available|Less than minbytes available]] explains the 0 return code.
string is less than <var class="term">minbytes</var>. [[#Less than minbytes available|"Less than minbytes available"]] explains the 0 return code.
One case of this is if there is no data remaining to be received on
One case of this is if there is no data remaining to be received on
the connection, that is, if <code>FIN</code> has been received.
the connection, that is, if <code>FIN</code> has been received.
 
<p>
In <var class="product">Sirius Mods</var> version 6.3 and later, <var>$Sock_Recv</var> returns 0 to indicate
<var>$Sock_Recv</var> now returns 0 to indicate
that there is no more data to be received.
that there is no more data to be received.
Calling <var>$Sock_Recv</var> (or <var>[[$Sock_RecvPrs]]</var>) again in this situation, without
Calling <var>$Sock_Recv</var> (or <var>[[$Sock_RecvPrs]]</var>) again in this situation, without
at the very least setting <var>RECVLIM</var> to a new value, will result in request cancellation.
at the very least setting <var>RECVLIM</var> to a new value, will result in request cancellation.</p>
</ul>
</ul>


Line 71: Line 72:
length of a data item before it appears in the socket stream.
length of a data item before it appears in the socket stream.
The simplest case is receiving the next <i><b>n</b></i> bytes, where <i><b>n</b></i>
The simplest case is receiving the next <i><b>n</b></i> bytes, where <i><b>n</b></i>
is the size of the target, as shown in the following example to receive
is the size of the target, as shown in the following example to receive 5 strings of 100 bytes each.
5 strings of 100 bytes each.
You use the alternate receive operation,
You use the alternate receive operation,
<var>$Sock_RecvPrs</var>, for a data item whose length is unknown beforehand,
<var>$Sock_RecvPrs</var>, for a data item whose length is unknown beforehand,
Line 101: Line 101:
The "string received by <var>$Sock_Recv</var>," is a reference to the
The "string received by <var>$Sock_Recv</var>," is a reference to the
string stored in the target plus any subsequent bytes that are discarded.
string stored in the target plus any subsequent bytes that are discarded.
Under <var class="product">Sirius Mods</var> 6.3 and later, if the target is a <var>Longstring</var>, no bytes
If the target is a <var>Longstring</var>, no bytes
would ever be discarded, since the maximum size of a <var>Longstring</var> is the
would ever be discarded, since the maximum size of a <var>Longstring</var> is the
same as the greatest number of bytes <var>$Sock_Recv</var> will ever receive.
same as the greatest number of bytes <var>$Sock_Recv</var> will ever receive.
Line 107: Line 107:
The several values that control the length of the string received
The several values that control the length of the string received
are discussed in this section, using the following terms:
are discussed in this section, using the following terms:
<table class="syntaxTable">
<table>
<tr><th><var>RECVLIM</var> </th>
<tr><th><var>RECVLIM</var> </th>
<td>This specifies the number of bytes remaining in the "receive window." A value of zero means there is no limit to the window. The purpose of this window is to allow you to limit a series of receive operations (using, if you want, a mixture of <var>$Sock_Recv</var> and <var>$Sock_RecvPrs</var>) to a predetermined total number of bytes.   
<td>This specifies the number of bytes remaining in the "receive window." A value of zero means there is no limit to the window. The purpose of this window is to allow you to limit a series of receive operations (using, if you want, a mixture of <var>$Sock_Recv</var> and <var>$Sock_RecvPrs</var>) to a predetermined total number of bytes.   
Line 114: Line 114:
A good example of the use of <var>RECVLIM</var> is obtaining a web page, whose length is (usually) specified in the "Content-length" field of the HTTP response header.
A good example of the use of <var>RECVLIM</var> is obtaining a web page, whose length is (usually) specified in the "Content-length" field of the HTTP response header.
</td></tr>
</td></tr>
<tr><th>len_to_end </th>
<tr><th>len_to_end </th>
<td>When a receive operation is performed, the received string is of course limited to the total remaining bytes in the stream that the remote end sent before issuing a close operation. Although this value is not known ahead of time, when describing the operation of receive, the <var class="term">len_to_end</var> term is used to indicate various cases.
<td>When a receive operation is performed, the received string is of course limited to the total remaining bytes in the stream that the remote end sent before issuing a close operation. Although this value is not known ahead of time, when describing the operation of receive, the <var class="term">len_to_end</var> term is used to indicate various cases.
Line 136: Line 137:


<table>
<table>
<tr><th><i>Item</i></th>
<tr class="head"><th>Item</th>
<th><i>Value when argument is 0</i></th></tr>
<th>Value when argument is 0</th></tr>
 
<tr><th>max_recv</th>
<tr><th>max_recv</th>
<td>larger of <var class="term">min_recv</var> and <var class="term">targ_size</var></td></tr>
<td>larger of <var class="term">min_recv</var> and <var class="term">targ_size</var></td></tr>
<tr><th>min_recv</th>
<tr><th>min_recv</th>
<td>the effective value of <var class="term">max_recv</var></td></tr>
<td>the effective value of <var class="term">max_recv</var></td></tr>
Line 150: Line 153:
is only possible if <var class="term">min_recv</var> differs from <var class="term">max_recv</var>; therefore, you will
is only possible if <var class="term">min_recv</var> differs from <var class="term">max_recv</var>; therefore, you will
generally not code the <var class="term">minbytes</var> argument of <var>$Sock_Recv</var>.
generally not code the <var class="term">minbytes</var> argument of <var>$Sock_Recv</var>.
===Less than minbytes available===
===Less than minbytes available===
Before <var class="product">Sirius Mods</var> version 6.3, if <var>$Sock_Recv</var> was called and the number of bytes
Formerly, if <var>$Sock_Recv</var> was called and the number of bytes
remaining on the connection within the remaining <var>RECVLIM</var> was
remaining on the connection within the remaining <var>RECVLIM</var> was
less than the effective value of the <var class="term">minbytes</var> argument,
less than the effective value of the <var class="term">minbytes</var> argument,
Line 157: Line 161:
was set to any partial string remaining, or to the null string if there
was set to any partial string remaining, or to the null string if there
were none remaining (that is, <code>FIN</code> has been received).
were none remaining (that is, <code>FIN</code> has been received).
The 0 return code indicated unsuccessful completion because the
The 0 return code indicated unsuccessful completion because the
function wasn't able to do all it was asked to do (that is, return a
function wasn't able to do all it was asked to do (that is, return a
Line 165: Line 170:
bytes received by the length of the string stored in the receive target.
bytes received by the length of the string stored in the receive target.


In <var class="product">Sirius Mods</var> version 6.3 and later, if <var>$Sock_Recv</var> is called and the number of
It is now the case that if <var>$Sock_Recv</var> is called and the number of
bytes remaining on the connection within the remaining <var>RECVLIM</var> is
bytes remaining on the connection within the remaining <var>RECVLIM</var> is
less than the effective value of the <var class="term">minbytes</var> argument,
less than the effective value of the <var class="term">minbytes</var> argument,
<var>$Sock_Recv</var> returns the number of bytes read.
<var>$Sock_Recv</var> returns the number of bytes read.
In such a case, if the application determines that the number of bytes
In such a case, if the application determines that the number of bytes
returned is less than <var class="term">minbytes</var>,
returned is less than <var class="term">minbytes</var>,
Line 175: Line 181:
Alternatively, the application can forego checking for this case and
Alternatively, the application can forego checking for this case and
simply wait until <var>$Sock_Recv</var> returns a zero.
simply wait until <var>$Sock_Recv</var> returns a zero.
This allows a simple receive loop structure like:
This allows a simple receive loop structure like:
<p class="code">Do While $Sock_Recv(%sock, %str)
<p class="code">Do While $Sock_Recv(%sock, %str)

Latest revision as of 00:00, 21 September 2018

Receive string based on count

Note: Many $functions have been deprecated in favor of Object Oriented methods. The OO equivalent for $Sock_Recv is the Receive method.

$Sock_Recv receives a string of bytes on a Janus Sockets connection, typically to obtain a fixed-length string, and also to allow receipt of additional "already-buffered" bytes.

$Sock_Recv is also a callable function.

Syntax

[%num =] $Sock_Recv(sockNum, %recv_targ, [opts])

Syntax terms

%num A numeric value that is the number of bytes received, or, if the operation could not be performed as requested, a 0 value. For more details, see #$Sock_Recv return values.
sockNum The socket number.
%recv_targ A %variable or an IMAGE item (and never a SOUL class variable). This argument is the target of the receive operation, and is referred to as the "receive target." You may request that some bytes from the socket stream can be discarded, so the length of the string stored in this target may be less than the value returned by $Sock_Recv.
maxbytes This optional argument, the maximum number of bytes of data to receive, must be from 0 to 2,147,483,647. The default is 0, which means limit the received data to the larger of the value of the minbytes argument and the declared length of the receive target.

If this argument is 0 and the receive argument is a Longstring, the received data is limited to 2,147,483,647 bytes.

minbytes This optional argument is the minimum number of bytes of data to receive before the operation completes, unless the input stream ends (a close operation by the remote) or RECVLIM is encountered. The argument only needs to be specified for a variable length receive, which is fairly unusual.

The default is 0, which means use the effective value of the maxbytes argument. Valid values must be from 0 to 2,147,483,647, and must not exceed the effective value of maxbytes.

opts This optional argument is an option string which can contain any of the following:
BINARY This indicates that regardless of the socket's BINARY or CHAR parameter, data is not translated when saved in the receive target. The received string can be translated later in the program using the $Sock_Tran_In function.
CHAR This indicates that regardless of the socket's BINARY or CHAR parameter, data is translated when saved in the receive target. The translation is specified by the input table defined by the socket's XTAB parameter.

$Sock_Recv return values

  • $Sock_Recv returns the value -1 if the socket is not OPEN and ONRESET CONTINUE is in effect for the socket.
  • $Sock_Recv returns a number greater than 0 (which is the number of bytes received from the socket stream) if at least minbytes are stored in the target. Lengths: maximum, minimum, truncation, RECVLIM explains how the term number of bytes received is used, and it discusses the maxbytes and minbytes arguments and other values affecting the received string. The return value from $Sock_Recv can be less than the value of minbytes.
  • Formerly, $Sock_Recv would return 0 to indicate that the length of the received string is less than minbytes. Less than minbytes available explains the 0 return code. One case of this is if there is no data remaining to be received on the connection, that is, if FIN has been received.

    $Sock_Recv now returns 0 to indicate that there is no more data to be received. Calling $Sock_Recv (or $Sock_RecvPrs) again in this situation, without at the very least setting RECVLIM to a new value, will result in request cancellation.

Usage notes

In general, you use $Sock_Recv when the protocol in use establishes the length of a data item before it appears in the socket stream. The simplest case is receiving the next n bytes, where n is the size of the target, as shown in the following example to receive 5 strings of 100 bytes each. You use the alternate receive operation, $Sock_RecvPrs, for a data item whose length is unknown beforehand, but rather is terminated by a known string (or one of a known set of strings).

%GRABIT IS STRING LEN 100 ARRAY(5) For %x From 1 To 5 %len = $Sock_Recv(%socket, %grabit(%x)) If %len Ne 100 Then Print 'Invalid number of bytes received.' Print 'Last string received=' %grabit(%x) Print 'Partial length=' With $Len(%grabit(%x)) End If End For

Lengths: maximum, minimum, truncation, RECVLIM

The $Sock_Recv function allows you to receive either a fixed length string of bytes, or to receive some minimum length and also any additional bytes that may already be available in the socket's TCP/IP input buffer; this latter capability is called a "variable length receive."

It is important to understand that a string referred to as "the string that is received in a call to $Sock_Recv" may be more than the string that $Sock_Recv stores in the receive target. Therefore, data in the input stream that exceeds the size of the target can be discarded (or "truncated"). The "string received by $Sock_Recv," is a reference to the string stored in the target plus any subsequent bytes that are discarded. If the target is a Longstring, no bytes would ever be discarded, since the maximum size of a Longstring is the same as the greatest number of bytes $Sock_Recv will ever receive.

The several values that control the length of the string received are discussed in this section, using the following terms:

RECVLIM This specifies the number of bytes remaining in the "receive window." A value of zero means there is no limit to the window. The purpose of this window is to allow you to limit a series of receive operations (using, if you want, a mixture of $Sock_Recv and $Sock_RecvPrs) to a predetermined total number of bytes.

RECVLIM can be set in a $Sock_Set call, and subsequent receive operations decrement from RECVLIM the number of bytes received, until it reaches zero (at which point a$Sock_Recv call will return a 0, just as i When RECVLIM has been set, no receive operation will use any bytes beyond the window. A good example of the use of RECVLIM is obtaining a web page, whose length is (usually) specified in the "Content-length" field of the HTTP response header.

len_to_end When a receive operation is performed, the received string is of course limited to the total remaining bytes in the stream that the remote end sent before issuing a close operation. Although this value is not known ahead of time, when describing the operation of receive, the len_to_end term is used to indicate various cases.
max_recv This corresponds to the maxbytes argument of the $Sock_Recv function call. It needs to be specified only if you want to allow truncation, or if you want to limit the received string to a length shorter than the size of the receive target. The number of bytes received by $Sock_Recv will not exceed the effective value of this argument. The effective value of max_recv if argument 3 is 0 (its default), is discussed below.
min_recv This corresponds to the minbytes argument of the $Sock_Recv function call. You only need to provide this argument for a variable length receive. The receive initiated by $Sock_Recv will only complete if min_recv bytes are received, or if the limit to the stream (RECVLIM or len_to_end) is reached. The effective value of min_recv, if minbytes is 0 (its default), is discussed below.
targ_size The size of the target is used in calculating various defaults of the other values here, and it also obviously limits the number of bytes stored in the target and so affects the number of bytes discarded.
discard_len This is the number of bytes received minus targ_size. It will always be zero, unless max_recv is greater than targ_size.

Arguments max_recv and min_recv both default to 0; if they are omitted, or if they are coded as 0, their effective values are shown here:

Item Value when argument is 0
max_recv larger of min_recv and targ_size
min_recv the effective value of max_recv

Truncation can only occur if max_recv > targ_size, which cannot happen if the target is a Longstring.

A variable length receive, which is a fairly unusual operation, is only possible if min_recv differs from max_recv; therefore, you will generally not code the minbytes argument of $Sock_Recv.

Less than minbytes available

Formerly, if $Sock_Recv was called and the number of bytes remaining on the connection within the remaining RECVLIM was less than the effective value of the minbytes argument, $Sock_Recv returned 0 to indicate this, but the receive target was set to any partial string remaining, or to the null string if there were none remaining (that is, FIN has been received).

The 0 return code indicated unsuccessful completion because the function wasn't able to do all it was asked to do (that is, return a fixed number of bytes or a specified minimum number of bytes). Depending on your application, this may be an error or the partial string may be valid data. If no bytes were discarded, you could determine the number of bytes received by the length of the string stored in the receive target.

It is now the case that if $Sock_Recv is called and the number of bytes remaining on the connection within the remaining RECVLIM is less than the effective value of the minbytes argument, $Sock_Recv returns the number of bytes read.

In such a case, if the application determines that the number of bytes returned is less than minbytes, it knows that either RECVLIM must have been hit or a FIN must have been received. Alternatively, the application can forego checking for this case and simply wait until $Sock_Recv returns a zero.

This allows a simple receive loop structure like:

Do While $Sock_Recv(%sock, %str) ... End Do

However, with the use of Longstrings, an application can simply receive everything up to RECVLIM or the end of a stream with a simple call like:

%rc = $Sock_Recv(%sock, %longstr)