|
<< Click to Display Table of Contents >> Navigation: 3. Script Language > Internet and Network > TCP-Client Commands > TCP-Client Operations |
SPR Script Language
TCP.Send
Sends data over an open TCP client connection.
Intention
The TCP.Send command transmits data through an established TCP client connection. You provide the socket handle ($$HND) obtained from TCP.Open and the string ($$DAT) containing the data to send. This command is essential for sending commands or information to a network server.
Illustration
[Client Script]
| |
v TCP/IP v
TCP.Send ---> [Data Stream] ---> [Network Server]
(Socket HND)
(Data String)
Syntax
TCP.Send|$$HND|$$DAT
Parameter Explanation
P1 - $$HND - (Variable, Numeric, Required)
The socket handle of the open TCP connection, obtained from TCP.Open.
P2 - $$DAT - (Variable, String, Required)
The string containing the data to be sent. The data is sent as raw bytes (assumed UTF-8 if text).
Return Value (on Stack)
The command pushes the number of bytes successfully sent onto the stack. Returns 0 if the send operation fails (check HTP.GetErr for details).
Examples
'***********************************
' TCP.Send - Sample 1: Send a JSON command to MCP Server
'***********************************
TCP.Open|127.0.0.1|5556|5000
POP.$$HND ' Get socket handle
JIZ.$$HND|Error_Handler
VAR.$$CMD_JSON={"action":"ping"}
TCP.Send|$$HND|$$CMD_JSON
POP.$$SENT_BYTES
'
'***********************************
' TCP.Send - Sample 2: Send HTTP GET request line
'***********************************
TCP.Open|example.com|80
POP.$$WEB_HND
JIZ.$$WEB_HND|Error_Handler
VAR.$$HTTP_REQ="GET / HTTP/1.1"$CRLF$"Host: example.com"$CRLF$"Connection: close"$CRLF$
TCP.Send|$$WEB_HND|$$HTTP_REQ
POP.$$SENT_BYTES
Remarks
- This command transmits the entire string as a byte stream. It does not append newlines unless they are part of the string itself.
- For reading responses, use TCP.Recv or TCP.LineInput.
- Ensure the connection is still open before sending data.
See also:
• TCP.Open
• TCP.Recv