TCP-Client Operations

<< Click to Display Table of Contents >>

Navigation:  3. Script Language > Internet and Network > TCP-Client Commands >

TCP-Client Operations

TCP.Recv

Previous Top Next


SPR Script Language

 

TCP.Recv

Receives a specified number of bytes from an open TCP client connection.

 

Intention

 

The TCP.Recv command retrieves data from an active TCP connection. You must provide the socket handle ($$HND) from TCP.Open and the maximum number of bytes to receive ($$MAXB). The received data is stored in the specified variable ($$DAT). This command is crucial for receiving responses from servers after sending commands or requests.

 

Illustration

 

            [Network Server]
                |
                v   TCP/IP
TCP.Recv <--- [Data Stream] <--- (Response Data)
(Socket HND)
(Max Bytes)
(Result Var)

 

Syntax

 

TCP.Recv|$$HND|$$MAXB|$$DAT

 

Parameter Explanation

 

P1 - $$HND - (Variable, Numeric, Required)

The socket handle of the open TCP connection, obtained from TCP.Open.

 

P2 - $$MAXB - (Variable, Numeric, Required)

The maximum number of bytes to attempt to receive. This should be large enough to hold the expected response.

 

P3 - $$DAT - (Variable, String, Required)

A string variable to store the received data. If fewer than $$MAXB bytes are available, it will receive whatever is available.

 

Return Value (on Stack)

 

The command pushes the number of bytes actually received onto the stack. Returns 0 if no data is received or an error occurs (check HTP.GetErr for details).

 

Examples (symbolic only can not run!)

 

'***********************************

' TCP.Recv - Sample 1: Receive MCP Server Response

'***********************************

VAR.$$RES_DATA=

TCP.Recv|$$HND|4096|$$RES_DATA

POP.$$BYTES_RCVD ' Get number of bytes received

JIZ.$$BYTES_RCVD|Error_Handler

PRT.Server response: $$RES_DATA

'

'***********************************

' TCP.Recv - Sample 2: Receive HTTP Header (Loop)

'***********************************

VAR.$$HEADER_BUFFER=

VAR.$$TOTAL_RCVD=0

:Recv_Loop

TCP.Recv|$$WEB_HND|1024|$$CHUNK

POP.$$CHUNK_BYTES

 ' No more data or error

JIZ.$$CHUNK_BYTES|End_Recv_Loop

VAR.$$HEADER_BUFFER=$$HEADER_BUFFER & $$CHUNK

CAL.$$TOTAL_RCVD=$$TOTAL_RCVD + $$CHUNK_BYTES

VAR.$$CRLF_POS=INSTR($$HEADER_BUFFER,$CRLF$$CRLF$)

 ' Found end of headers

JNZ.$$CRLF_POS|End_Recv_Loop

GTO.Recv_Loop

:End_Recv_Loop

PRT.HTTP Headers: $$HEADER_BUFFER

ENR.

 

Remarks

 

- This command reads raw byte data. For line-by-line text, consider TCP.LineInput.

- The timeout for this operation is inherited from the TCP.Open command used to establish the connection.

- The command pushes 0 to the stack if no data is available before the timeout, or if the connection is closed.

 

See also:

 

TCP.Open

TCP.Send

TCP.LineInput

TCP.Close